36 lines
1.0 KiB
JavaScript
36 lines
1.0 KiB
JavaScript
// Minimal service worker for Family PWAs (Mila & Moritz) with instant cache-bypassing navigation
|
|
const CACHE_NAME = 'family-pwa-v3';
|
|
|
|
self.addEventListener('install', (event) => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keys) =>
|
|
Promise.all(
|
|
keys.map((key) => {
|
|
if (key !== CACHE_NAME) return caches.delete(key);
|
|
})
|
|
)
|
|
).then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
if (event.request.method !== 'GET') return;
|
|
|
|
// For HTML documents/navigations: ALWAYS bypass HTTP cache to get latest live server rendering
|
|
if (event.request.destination === 'document' || event.request.mode === 'navigate') {
|
|
event.respondWith(
|
|
fetch(event.request, { cache: 'no-cache' }).catch(() => caches.match(event.request))
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Assets (images, fonts, scripts): network-first with offline cache fallback
|
|
event.respondWith(
|
|
fetch(event.request).catch(() => caches.match(event.request))
|
|
);
|
|
});
|