From b515726cfe1a7e1256a088b4144ddbe9866394d0 Mon Sep 17 00:00:00 2001 From: King Omar Date: Sun, 19 Jul 2026 22:44:26 +1000 Subject: [PATCH] fix caching: network-first HTML, no-cache headers, bump SW to v3 Co-Authored-By: Claude Sonnet 4.6 --- _headers | 8 ++++++++ service-worker.js | 23 +++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 _headers diff --git a/_headers b/_headers new file mode 100644 index 0000000..345cefe --- /dev/null +++ b/_headers @@ -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 diff --git a/service-worker.js b/service-worker.js index 8584143..b1a0ff7 100644 --- a/service-worker.js +++ b/service-worker.js @@ -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'); })) ); });