Files
cup26/public/push-sw.js
T
NilsBriggen 03b0fa734e Web Push: goal, kickoff and full-time alerts for a followed team
Fully progressive: without VAPID keys in the environment the API says
404, the bell never renders, and the service worker addition (a push
handler via workbox importScripts — no SW strategy change) is inert.
With keys: a bell on each team page requests permission, subscribes
(one team per device) and the server diffs fixture states on every
broadcast to push Kickoff / Goal / Full time to that team's followers.
Dead endpoints (404/410) self-clean. Round-trip verified locally with
dev VAPID keys; disabled mode verified too.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 08:21:55 +02:00

30 lines
954 B
JavaScript

/* Imported into the generated service worker (workbox importScripts).
Handles Web Push for goal/kickoff alerts — additive only. */
self.addEventListener('push', (event) => {
if (!event.data) return;
let data = {};
try { data = event.data.json(); } catch { return; }
if (!data.title) return;
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body || '',
icon: '/pwa-192x192.png',
badge: '/pwa-192x192.png',
data: { url: data.url || '/' },
}),
);
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
const url = (event.notification.data && event.notification.data.url) || '/';
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((list) => {
for (const c of list) {
if ('focus' in c) { c.navigate(url); return c.focus(); }
}
return clients.openWindow(url);
}),
);
});