From 529911dc126470b763557dc2650fbf132d55464c Mon Sep 17 00:00:00 2001 From: slaguru666 <111923774+slaguru666@users.noreply.github.com> Date: Tue, 21 Jul 2026 11:42:55 +0100 Subject: [PATCH] fix: service worker network-first for the page so deploys reach devices The cache-first SW never revalidated the HTML, so a device that loaded the app once kept serving the old bundle forever (new deploys never landed). Now: network-first for navigations/HTML (fresh on every online load, cached shell as offline fallback), cache-first only for immutable hashed assets. Bumped CACHE to director-v2 so the stale v1 cache is purged on activate. Co-Authored-By: Claude Opus 4.8 --- public/sw.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/public/sw.js b/public/sw.js index e539a94..01c6f49 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,8 +1,5 @@ -const CACHE = 'director-v1'; -// Precache only stable, build-invariant navigation paths. Hashed asset -// bundles (/assets/*) are cached at runtime on first online load by the -// cache-first fetch handler below. (Follow-up: generate a full precache -// list from the Vite build manifest for cold-offline-before-first-visit.) +const CACHE = 'director-v2'; +// Precache the navigation shell so a cold, offline launch works. const SHELL = ['/', '/index.html']; self.addEventListener('install', (e) => { @@ -16,6 +13,22 @@ self.addEventListener('activate', (e) => { }); self.addEventListener('fetch', (e) => { if (e.request.method !== 'GET') return; + const url = new URL(e.request.url); + const isDoc = e.request.mode === 'navigate' || url.pathname === '/' || url.pathname.endsWith('.html'); + + if (isDoc) { + // Network-first for the page itself, so a new deploy lands immediately; + // fall back to the cached shell when offline. + e.respondWith( + fetch(e.request).then((res) => { + if (res.ok) { const copy = res.clone(); e.waitUntil(caches.open(CACHE).then((c) => c.put(e.request, copy))); } + return res; + }).catch(() => caches.match(e.request).then((hit) => hit || caches.match('/index.html'))) + ); + return; + } + + // Cache-first for hashed, content-addressed assets (immutable), caching at runtime. e.respondWith( caches.match(e.request).then((hit) => hit || fetch(e.request).then((res) => { if (res.ok && res.type === 'basic') {