demo · v132
Service worker integration — subscribe, dispatch, decode
Register a service worker; the SW logs back what PushEvent.data.bytes() would do. The page can't actually push a real Web Push message (that requires a server), but it can postMessage the same payload into the SW and call the equivalent decode path. Buttons surface support live.
serviceWorker: —
PushMessageData.bytes: —
PushManager: —
service worker log (mirrored back to this page)
sw.js shape
self.addEventListener("push", (e) => {
// v132+ ergonomic path
const u8 = e.data.bytes();
const text = new TextDecoder().decode(u8);
e.waitUntil(self.registration.showNotification(text));
});
self.addEventListener("message", (e) => {
// demo channel: same code path
const u8 = new Response(e.data).bytes
? new Response(e.data).bytes()
: new Response(e.data).arrayBuffer().then((ab) => new Uint8Array(ab));
Promise.resolve(u8).then((bytes) => e.source.postMessage("decoded " + bytes.length + " bytes"));
});