noted/service-worker.js
King Omar b515726cfe fix caching: network-first HTML, no-cache headers, bump SW to v3
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-19 22:44:26 +10:00

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;
}))
);
});