Dateien nach "frontend/public" hochladen
This commit is contained in:
34
frontend/public/sw-1.js
Normal file
34
frontend/public/sw-1.js
Normal file
@@ -0,0 +1,34 @@
|
||||
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;
|
||||
})
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user