diff --git a/internal/web/static/sw.js b/internal/web/static/sw.js index fd0c2ef..292340b 100644 --- a/internal/web/static/sw.js +++ b/internal/web/static/sw.js @@ -4,7 +4,7 @@ // // Bump CACHE_VERSION whenever the precached shell assets change; activate() // drops every cache that doesn't match the current version. -var CACHE_VERSION = "v4"; +var CACHE_VERSION = "v5"; var SHELL_CACHE = "pete-shell-" + CACHE_VERSION; var RUNTIME_CACHE = "pete-runtime-" + CACHE_VERSION; @@ -117,18 +117,24 @@ self.addEventListener("fetch", function (event) { return; } - // Static assets: cache-first (they're versioned by deploy), fill the cache on - // first miss so a later offline visit has them. + // Static assets: network-first. Our asset URLs are NOT content-hashed + // (/static/js/weather-gl.js stays the same URL across deploys), so a + // cache-first strategy would pin whatever bytes were first cached and never + // notice a redeployed file changed — leaving stale JS/CSS in place until the + // CACHE_VERSION bump below. Go's FileServer sets ETag/Last-Modified, so an + // online refetch is a cheap 304 when nothing changed. We still fill the cache + // on every success and fall back to it when the network is unreachable, so + // offline reading keeps the shell it needs. if (url.pathname.indexOf("/static/") === 0) { event.respondWith( - caches.match(req).then(function (hit) { - return hit || fetch(req).then(function (res) { - if (res && res.ok) { - var copy = res.clone(); - caches.open(SHELL_CACHE).then(function (cache) { cache.put(req, copy); }); - } - return res; - }); + fetch(req).then(function (res) { + if (res && res.ok) { + var copy = res.clone(); + caches.open(SHELL_CACHE).then(function (cache) { cache.put(req, copy); }); + } + return res; + }).catch(function () { + return caches.match(req); }) ); return;