peppa/apps/web/public/sw.js

89 lines
3.3 KiB
JavaScript

// Peppa service worker (PEP-55): shows Web Push notifications and opens the right page
// on click. Payload (core/webpush.ts PushPayload): { title, body, url, tag?, sound?, count? }.
// PEP-179: the app shell works without a network — fonts, built assets and the icon come from
// the cache first, the /team page from the network first (the cached copy only when offline).
const CACHE = "peppa-shell-v1";
const SHELL = ["/icon.svg", "/fonts/press-start-2p/cyrillic.woff2", "/fonts/press-start-2p/cyrillic-ext.woff2", "/fonts/press-start-2p/latin.woff2", "/fonts/press-start-2p/latin-ext.woff2"];
self.addEventListener("install", (event) => {
event.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL)).catch(() => {}));
self.skipWaiting();
});
self.addEventListener("activate", (event) =>
event.waitUntil(
(async () => {
// A new version drops the caches of the old one.
for (const k of await caches.keys()) if (k !== CACHE) await caches.delete(k);
await self.clients.claim();
})(),
),
);
self.addEventListener("fetch", (event) => {
const req = event.request;
if (req.method !== "GET") return;
const url = new URL(req.url);
if (url.origin !== self.location.origin) return;
const cacheFirst = url.pathname.startsWith("/fonts/") || url.pathname.startsWith("/_next/static/") || url.pathname === "/icon.svg";
const teamPage = url.pathname === "/team" && req.mode === "navigate";
if (!cacheFirst && !teamPage) return;
event.respondWith(
(async () => {
const cache = await caches.open(CACHE);
if (cacheFirst) {
const hit = await cache.match(req);
if (hit) return hit;
const res = await fetch(req);
if (res.ok) cache.put(req, res.clone()).catch(() => {});
return res;
}
try {
const res = await fetch(req);
if (res.ok && !res.redirected) cache.put(req, res.clone()).catch(() => {});
return res;
} catch (e) {
const hit = await cache.match(req);
if (hit) return hit;
throw e;
}
})(),
);
});
self.addEventListener("push", (event) => {
let p = {};
try {
p = event.data ? event.data.json() : {};
} catch {
p = { title: "Peppa FACTORY", body: event.data ? event.data.text() : "" };
}
const shown = self.registration.showNotification(p.title || "Peppa FACTORY", {
body: p.body || "",
tag: p.tag,
renotify: Boolean(p.tag),
silent: p.sound === false,
icon: "/icon.svg",
badge: "/icon.svg",
data: { url: p.url || "/" },
});
const badge = typeof p.count === "number" && self.navigator.setAppBadge ? self.navigator.setAppBadge(p.count).catch(() => {}) : Promise.resolve();
event.waitUntil(Promise.all([shown, badge]));
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const url = new URL((event.notification.data && event.notification.data.url) || "/", self.location.origin).href;
event.waitUntil(
(async () => {
const tabs = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
const same = tabs.find((c) => c.url === url) || tabs.find((c) => new URL(c.url).origin === self.location.origin);
if (same) {
await same.focus();
if (same.url !== url && "navigate" in same) await same.navigate(url).catch(() => {});
return;
}
await self.clients.openWindow(url);
})(),
);
});