50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
// Service Worker – index.html immer frisch vom Server, Assets gecacht
|
||
const CACHE_NAME = 'dickendock-v3';
|
||
|
||
self.addEventListener('install', () => {
|
||
self.skipWaiting();
|
||
});
|
||
|
||
self.addEventListener('activate', e => {
|
||
e.waitUntil(
|
||
caches.keys()
|
||
.then(keys => Promise.all(keys.map(k => caches.delete(k))))
|
||
.then(() => self.clients.claim())
|
||
);
|
||
});
|
||
|
||
self.addEventListener('fetch', e => {
|
||
const url = new URL(e.request.url);
|
||
|
||
// API: immer Netzwerk
|
||
if (url.pathname.startsWith('/api/')) {
|
||
e.respondWith(fetch(e.request));
|
||
return;
|
||
}
|
||
|
||
// HTML-Navigation: immer frisch vom Server, nie aus Cache
|
||
if (e.request.mode === 'navigate') {
|
||
e.respondWith(
|
||
fetch(e.request, { cache: 'no-store' })
|
||
.catch(() => caches.match('/index.html'))
|
||
);
|
||
return;
|
||
}
|
||
|
||
// Statische Assets (JS/CSS mit Hash): Network-first, Cache als Fallback
|
||
e.respondWith(
|
||
fetch(e.request)
|
||
.then(res => {
|
||
if (res.ok) {
|
||
caches.open(CACHE_NAME).then(c => c.put(e.request, res.clone()));
|
||
}
|
||
return res;
|
||
})
|
||
.catch(() => caches.match(e.request))
|
||
);
|
||
});
|
||
|
||
self.addEventListener('message', e => {
|
||
if (e.data === 'SKIP_WAITING') self.skipWaiting();
|
||
});
|