Files
dickendock/frontend/public/sw.js

47 lines
1.5 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 v4 garantiert frische index.html, Auto-Reload bei neuem Build
const VERSION = 'v4';
const CACHE_NAME = 'dickendock-' + VERSION;
self.addEventListener('install', () => {
self.skipWaiting(); // neuen SW sofort übernehmen
});
self.addEventListener('activate', e => {
e.waitUntil((async () => {
// alte Caches löschen
const keys = await caches.keys();
await Promise.all(keys.map(k => caches.delete(k)));
await self.clients.claim();
// allen offenen Clients sagen: neue Version aktiv → reload
const clients = await self.clients.matchAll({ type: 'window' });
clients.forEach(c => c.postMessage({ type: 'SW_ACTIVATED', version: VERSION }));
})());
});
self.addEventListener('fetch', e => {
const url = new URL(e.request.url);
// API immer direkt ans Netz
if (url.pathname.startsWith('/api/')) {
e.respondWith(fetch(e.request));
return;
}
// HTML-Navigation: IMMER frisch vom Netzwerk, niemals aus Cache
// (Vite-Bundles haben Hash im Namen → neue index.html lädt neue Bundles)
if (e.request.mode === 'navigate') {
e.respondWith(
fetch(e.request, { cache: 'reload' }) // 'reload' = HTTP-Cache komplett umgehen
.catch(() => caches.match('/index.html'))
);
return;
}
// Restliche Assets: einfach durchreichen
e.respondWith(fetch(e.request).catch(() => caches.match(e.request)));
});
self.addEventListener('message', e => {
if (e.data === 'SKIP_WAITING') self.skipWaiting();
});