fix caching: network-first HTML, no-cache headers, bump SW to v3

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
King Omar 2026-07-19 22:44:26 +10:00
parent 11d86b17e5
commit b515726cfe
2 changed files with 23 additions and 8 deletions

8
_headers Normal file
View file

@ -0,0 +1,8 @@
/*.html
Cache-Control: no-cache, no-store, must-revalidate
/
Cache-Control: no-cache, no-store, must-revalidate
/service-worker.js
Cache-Control: no-cache, no-store, must-revalidate

View file

@ -1,8 +1,8 @@
const CACHE = 'noted-v2';
const ASSETS = ['./', './index.html', './manifest.json', './fry.png'];
const CACHE = 'noted-v3';
const STATIC = ['./fry.png', './manifest.json'];
self.addEventListener('install', e => {
e.waitUntil(caches.open(CACHE).then(c => c.addAll(ASSETS)));
e.waitUntil(caches.open(CACHE).then(c => c.addAll(STATIC)));
self.skipWaiting();
});
@ -14,15 +14,22 @@ self.addEventListener('activate', e => {
});
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 && e.request.method === 'GET') {
caches.open(CACHE).then(c => c.put(e.request, res.clone()));
}
if (res.ok) caches.open(CACHE).then(c => c.put(e.request, res.clone()));
return res;
}).catch(() => {
if (e.request.mode === 'navigate') return caches.match('./index.html');
}))
);
});