From d87c4b4260b023639373dc9eda9690f15d006570 Mon Sep 17 00:00:00 2001 From: djg Date: Wed, 23 Sep 2026 20:20:03 +0000 Subject: [PATCH] fix(cache): version static assets, update service worker to v3 with network-first navigation, and disable sw caching in nginx --- nginx.conf | 6 ++++-- site/404.html | 2 +- site/index.html | 10 +++++----- site/static/app.js | 4 +++- site/sw.js | 40 ++++++++++++++++++++++++++++++++++------ 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/nginx.conf b/nginx.conf index 58248d1..a4cd907 100644 --- a/nginx.conf +++ b/nginx.conf @@ -17,9 +17,10 @@ http { error_page 404 /404.html; add_header X-Content-Type-Options nosniff always; add_header Referrer-Policy strict-origin-when-cross-origin always; - # Service worker must not be cached aggressively + # Service worker must not be cached location = /sw.js { - add_header Cache-Control "no-cache"; + expires -1; + add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0" always; add_header X-Content-Type-Options nosniff always; } location ~* \.(css|js|svg|png|jpg|ico|woff2?|json)$ { @@ -28,6 +29,7 @@ http { } location / { try_files $uri $uri/ $uri/index.html =404; + add_header Cache-Control "no-cache" always; } } } diff --git a/site/404.html b/site/404.html index aae5849..1c42459 100644 --- a/site/404.html +++ b/site/404.html @@ -6,7 +6,7 @@ Page not found — myadhd.dev - +
diff --git a/site/index.html b/site/index.html index 3f3f642..4747c54 100644 --- a/site/index.html +++ b/site/index.html @@ -10,13 +10,13 @@ - + - +
@@ -387,8 +387,8 @@ - - - + + + diff --git a/site/static/app.js b/site/static/app.js index c3047bd..65ec1ab 100644 --- a/site/static/app.js +++ b/site/static/app.js @@ -11,7 +11,9 @@ /* ---------- register service worker ---------- */ if ('serviceWorker' in navigator) { - navigator.serviceWorker.register('/sw.js').catch(function () {}); + navigator.serviceWorker.register('/sw.js').then(function (reg) { + if (reg) reg.update(); + }).catch(function () {}); } /* ---------- audio helpers ---------- */ diff --git a/site/sw.js b/site/sw.js index bde4c3c..9efd982 100644 --- a/site/sw.js +++ b/site/sw.js @@ -1,10 +1,13 @@ /* myadhd.dev — service worker for offline PWA */ -var CACHE = 'myadhd-v2'; +var CACHE = 'myadhd-v3'; var ASSETS = [ '/', '/index.html', - '/static/style.css', - '/static/app.js', + '/static/style.css?v=3', + '/static/app.js?v=3', + '/static/audio.js?v=3', + '/static/store.js?v=3', + '/static/done.js?v=3', '/static/favicon.svg', '/static/icon-192.png', '/static/icon-512.png', @@ -12,21 +15,46 @@ var ASSETS = [ ]; self.addEventListener('install', function (e) { - e.waitUntil(caches.open(CACHE).then(function (c) { return c.addAll(ASSETS); })); + e.waitUntil( + caches.open(CACHE).then(function (c) { + return c.addAll(ASSETS); + }) + ); self.skipWaiting(); }); self.addEventListener('activate', function (e) { e.waitUntil( caches.keys().then(function (names) { - return Promise.all(names.filter(function (n) { return n !== CACHE; }).map(function (n) { return caches.delete(n); })); + return Promise.all( + names.filter(function (n) { return n !== CACHE; }).map(function (n) { return caches.delete(n); }) + ); }) ); self.clients.claim(); }); self.addEventListener('fetch', function (e) { + // Navigation requests: network-first so updates are picked up immediately + if (e.request.mode === 'navigate') { + e.respondWith( + fetch(e.request).catch(function () { + return caches.match('/index.html') || caches.match('/'); + }) + ); + return; + } + + // Static assets: cache-first with network fallback e.respondWith( - caches.match(e.request).then(function (r) { return r || fetch(e.request); }) + caches.match(e.request).then(function (r) { + return r || fetch(e.request).then(function (res) { + if (res && res.status === 200 && e.request.url.startsWith(self.location.origin)) { + var copy = res.clone(); + caches.open(CACHE).then(function (cache) { cache.put(e.request, copy); }); + } + return res; + }); + }) ); });