Download this file
| 1 | // The app's worker, as SKILL.md shows it. Lifecycle is the app's: |
| 2 | // nothing here calls skipWaiting or clients.claim either, because this |
| 3 | // example has nothing to claim. |
| 4 | importScripts("/static/aviso/aviso-sw.js"); |
| 5 | |
| 6 | const post = (body) => fetch("/aviso/subscribe", { |
| 7 | method: "POST", credentials: "same-origin", mode: "same-origin", redirect: "error", |
| 8 | headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), |
| 9 | }); |
| 10 | |
| 11 | self.addEventListener("push", (e) => e.waitUntil(AvisoSW.handlePush(e, { |
| 12 | fallback: () => ({ title: "aviso example", options: { body: "Something happened.", data: { url: "/" } } }), |
| 13 | }))); |
| 14 | |
| 15 | self.addEventListener("notificationclick", (e) => e.waitUntil(AvisoSW.handleClick(e, { fallbackURL: "/" }))); |
| 16 | |
| 17 | self.addEventListener("pushsubscriptionchange", (e) => e.waitUntil(AvisoSW.handleSubscriptionChange(e, { |
| 18 | // Fetched on demand: a terminated worker forgets its variables. |
| 19 | publicKey: () => fetch("/aviso/public-key").then((r) => r.json()).then((j) => j.publicKey), |
| 20 | save: post, |
| 21 | }))); |
| 22 | |