tippspiel2027/tippspiel/public/sw.js

114 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Service Worker für Hoschis Tippspiel
// Cacht nur statische Assets (App-Shell) für schnellen Start.
// HTML-Seiten werden bewusst NICHT gecacht: Sie enthalten den Login-Status und
// würden nach Ablauf der Session eine veraltete, eingeloggte Ansicht zeigen.
const CACHE_NAME = 'tippspiel2027-v12';
const STATIC_ASSETS = [
'/css/style.css',
'/img/icon-192.png',
'/img/icon-512.png',
'/img/icon-512-maskable.png',
'/manifest.json',
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
if (event.request.method !== 'GET' || url.origin !== self.location.origin) {
return;
}
// Versionsauskunft für die Diagnose in den Einstellungen.
// Ein alter Service Worker kennt diese Route nicht und lässt sie ans Netz
// durch – dort gibt es sie nicht (404).
if (url.pathname === '/sw-version') {
event.respondWith(new Response(CACHE_NAME, {
headers: {
'Content-Type': 'text/plain; charset=UTF-8',
'Cache-Control': 'no-store'
}
}));
return;
}
// Statische Assets: Cache-First (schnell), im Hintergrund aktualisieren
if (STATIC_ASSETS.includes(url.pathname)) {
event.respondWith(
caches.match(event.request).then((cached) => {
const network = fetch(event.request).then((response) => {
if (response && response.status === 200) {
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, response.clone()));
}
return response;
});
return cached || network;
})
);
return;
}
// Alle anderen Anfragen (insbesondere HTML) immer direkt ans Netz –
// der Server entscheidet anhand der Session, ob eingeloggt oder nicht.
event.respondWith(fetch(event.request));
});
// Smartphone-Erinnerungen (Web-Push mit VAPID)
// Die Nutzlast kommt als JSON vom Server (siehe Roundmail::sendReminder).
self.addEventListener('push', (event) => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch (e) {
data = {};
}
const title = data.title || 'Hoschis Tippspiel';
const options = {
body: data.body || '',
icon: '/img/icon-192.png',
badge: '/img/icon-192.png',
tag: data.tag || undefined,
data: { url: data.url || '/' },
};
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
const target = (event.notification.data && event.notification.data.url) || '/';
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((list) => {
for (const client of list) {
if (new URL(client.url).origin !== self.location.origin) {
continue;
}
return client.focus().then(() => {
if ('navigate' in client) {
return client.navigate(target);
}
return client;
});
}
return clients.openWindow(target);
})
);
});