From a96591c63fcf417db29bc830a84244c5ccb922de Mon Sep 17 00:00:00 2001 From: marwin Date: Fri, 29 May 2026 23:58:07 +0200 Subject: [PATCH] SW: cache navigation response offline (v9) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ohne diesen Fix schlägt das Öffnen der App im Flugzeug fehl, weil der Service Worker die HTML-Seite (/) nicht cached — der Browser zeigt dann eine Netzwerkfehler-Seite bevor JS überhaupt startet. Strategie: Network-first für Navigationsanfragen, bei Netzwerkfehler die zuletzt gecachte Version ausliefern. Nach dem ersten Online-Besuch funktioniert die App vollständig offline. Co-Authored-By: Claude Sonnet 4.6 --- static/js/sw.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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); + }) + ); } });