55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
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;
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('push', event => {
|
|
if (!event.data) return;
|
|
let data = {};
|
|
try { data = event.data.json(); } catch { data = { title:'Neue Nachricht', body: event.data.text() }; }
|
|
event.waitUntil(
|
|
self.registration.showNotification(data.title || 'DickenDock', {
|
|
body: data.body || '',
|
|
icon: data.icon || '/favicon.svg',
|
|
badge: '/favicon.svg',
|
|
tag: 'message',
|
|
renotify: true,
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('notificationclick', event => {
|
|
event.notification.close();
|
|
event.waitUntil(clients.openWindow('/'));
|
|
});
|