diff --git a/static/js/sw.js b/static/js/sw.js index 05999e6..aa6b47a 100644 --- a/static/js/sw.js +++ b/static/js/sw.js @@ -2,7 +2,7 @@ * diora service worker — caches the app shell for offline use. */ -const CACHE = 'diora-v8'; +const CACHE = 'diora-v9'; const PODCAST_CACHE = 'diora-podcast-v1'; const SHELL = [ '/static/css/app.css', @@ -71,7 +71,7 @@ self.addEventListener('fetch', function (event) { return; } - // Cache-first only for pre-defined shell assets; everything else hits the network + // Cache-first for pre-defined shell assets const isShell = SHELL.some(function (s) { return url.pathname === s; }); if (isShell) { event.respondWith( @@ -79,5 +79,22 @@ self.addEventListener('fetch', function (event) { return cached || fetch(event.request); }) ); + return; + } + + // Navigation requests (the HTML page itself): network-first, fall back to + // last cached version so the app opens offline after being visited once. + if (event.request.mode === 'navigate') { + event.respondWith( + fetch(event.request).then(function (response) { + if (response.ok) { + var clone = response.clone(); + caches.open(CACHE).then(function (cache) { cache.put(event.request, clone); }); + } + return response; + }).catch(function () { + return caches.match(event.request); + }) + ); } });