35 lines
1 KiB
JavaScript
35 lines
1 KiB
JavaScript
const CACHE = 'noted-v3';
|
|
const STATIC = ['./fry.png', './manifest.json'];
|
|
|
|
self.addEventListener('install', e => {
|
|
e.waitUntil(caches.open(CACHE).then(c => c.addAll(STATIC)));
|
|
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 => {
|
|
// Skip API calls entirely
|
|
if (e.request.url.includes('/api/')) return;
|
|
|
|
// HTML: always network-first so updates are instant
|
|
if (e.request.mode === 'navigate') {
|
|
e.respondWith(
|
|
fetch(e.request).catch(() => caches.match('./index.html'))
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Static assets: cache-first
|
|
e.respondWith(
|
|
caches.match(e.request).then(cached => cached || fetch(e.request).then(res => {
|
|
if (res.ok) caches.open(CACHE).then(c => c.put(e.request, res.clone()));
|
|
return res;
|
|
}))
|
|
);
|
|
});
|