35 lines
968 B
JavaScript
35 lines
968 B
JavaScript
const CACHE = 'dickendock-v1';
|
|
const ASSETS = ['/', '/index.html'];
|
|
|
|
self.addEventListener('install', e => {
|
|
e.waitUntil(
|
|
caches.open(CACHE).then(c => c.addAll(ASSETS)).then(() => self.skipWaiting())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', e => {
|
|
e.waitUntil(
|
|
caches.keys().then(keys =>
|
|
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
|
|
).then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', e => {
|
|
// API-Anfragen immer live holen
|
|
if (e.request.url.includes('/api/')) {
|
|
e.respondWith(fetch(e.request).catch(() => new Response('Offline', { status: 503 })));
|
|
return;
|
|
}
|
|
// Alles andere: Cache first, dann Network
|
|
e.respondWith(
|
|
caches.match(e.request).then(cached => {
|
|
const network = fetch(e.request).then(res => {
|
|
if (res.ok) caches.open(CACHE).then(c => c.put(e.request, res.clone()));
|
|
return res;
|
|
});
|
|
return cached || network;
|
|
})
|
|
);
|
|
});
|