I'm using the web push API in a webapp to listen for updates from my server. This update data was in plain JSON, but got too big for the web push API, so I decided to compress. However, when I do so the event.data
parameter on the push event in the serviceworker is always null. This is odd, as the app is fully functional without this compression, and the browser receives the push without error.
here's my TS code, using pako for decompressing (although it never gets to that point):
(self as any).addEventListener('push', async (event: PushEvent) => {
//This should be the DEFLATE compressed JSON
const raw = pako.inflate(event.data.arrayBuffer()) //errors here due to data null
const payload = JSON.parse(raw) as rideTime[]
console.debug("received push from server")
event.waitUntil(handlePush(payload, event))
});
here's chrome accepting the push. after this the push event is successfully dispatched.
From what I can find the web push API should be compatible with binary messages, yet any compression algorithm I use causes this issue. Any leads are appreciated. Thanks.
It turns out that data sent as a web push payload must be text. This of course means that to send binary data, one must base64 encode the payload. Failing to do this to the compressed data was the cause of my issue, not anything to do with the compression.