stepwise / static /sw.js
Ashish-K's picture
Reminders: web push when she leaves a quiz or the tutor is waiting; app manifest and icons
95be3b8 verified
Raw History Blame Contribute Delete
1.3 kB
// StepWise service worker: notifications only. It does not cache pages, so the app is always fresh.
self.addEventListener("install", () => self.skipWaiting());
self.addEventListener("activate", (e) => e.waitUntil(self.clients.claim()));
self.addEventListener("push", (e) => {
let d = {};
try { d = e.data ? e.data.json() : {}; } catch { d = { body: e.data ? e.data.text() : "" }; }
e.waitUntil(self.registration.showNotification(d.title || "StepWise", {
body: d.body || "", tag: d.tag || "stepwise", renotify: true, requireInteraction: d.tag === "quiz",
icon: "icon-192.png", badge: "icon-192.png", vibrate: [250, 120, 250], data: { url: d.url || "/" },
}));
});
self.addEventListener("notificationclick", (e) => {
e.notification.close();
const url = new URL((e.notification.data && e.notification.data.url) || "/", self.location.origin).href;
e.waitUntil((async () => {
const wins = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
for (const w of wins) {
if (new URL(w.url).origin === self.location.origin) {
await w.focus();
if (w.url !== url && "navigate" in w && !url.endsWith("/")) { try { await w.navigate(url); } catch {} }
return;
}
}
await self.clients.openWindow(url);
})());
});