Files
Goddess/pwa/sw.js
TutorialsGHG b011c7ad75 Initial commit
2026-04-12 21:57:21 +02:00

40 lines
1.0 KiB
JavaScript

// NotifyPulse PWA Service Worker v3
const CACHE = 'notifypulse-v3';
const STATIC = ['/pwa/', '/pwa/index.html'];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE).then(c => c.addAll(STATIC).catch(() => {}))
);
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', e => {
const url = new URL(e.request.url);
// Never cache API calls
if (url.pathname.startsWith('/api/')) return;
e.respondWith(
caches.match(e.request).then(r => r || fetch(e.request).then(res => {
if (res.ok && e.request.method === 'GET') {
const clone = res.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
}
return res;
}).catch(() => caches.match('/pwa/')))
);
});
// Handle messages from main thread
self.addEventListener('message', e => {
if (e.data === 'skipWaiting') self.skipWaiting();
});