demo · v132
PushMessageData: all four reads compared
Synthesise a push payload and drain it four ways. .bytes() is the v132 addition — Uint8Array view, no double allocation.
methods on PushMessageData
| method | returns | note |
|---|---|---|
.text() | string | UTF-8 decode |
.json() | parsed JS | decode + JSON.parse |
.arrayBuffer() | ArrayBuffer | raw bytes, needs typed-array wrap |
.blob() | Blob | octet-stream |
.bytes() (v132) | Uint8Array | already a view |
synthesise & drain
click "drain"
// in your service worker
self.addEventListener("push", async (e) => {
const data = e.data;
if (!data) return;
// pick the right one for your payload:
const view = data.bytes(); // Uint8Array (v132)
const text = data.text(); // string
const json = data.json(); // parsed
const blob = data.blob(); // Blob
const buf = data.arrayBuffer(); // ArrayBuffer
// ...
});