Initial commit
This commit is contained in:
39
pwa/sw.js
Normal file
39
pwa/sw.js
Normal file
@@ -0,0 +1,39 @@
|
||||
// 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();
|
||||
});
|
||||
Reference in New Issue
Block a user