From cf5157c4968b1de9ae90caeb03c31d5d65f20304 Mon Sep 17 00:00:00 2001 From: Florian Heinz Date: Sun, 12 Apr 2026 17:10:22 +0200 Subject: [PATCH] fix pwa caching for fresh settings and env changes --- nouri/static/pwa/service-worker.js | 32 +++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/nouri/static/pwa/service-worker.js b/nouri/static/pwa/service-worker.js index 669a146..ec700f0 100644 --- a/nouri/static/pwa/service-worker.js +++ b/nouri/static/pwa/service-worker.js @@ -1,6 +1,5 @@ -const CACHE_NAME = "nouri-v0-5-0"; -const APP_SHELL = [ - "/", +const CACHE_NAME = "nouri-v0-5-1"; +const STATIC_ASSETS = [ "/static/css/styles.css", "/static/js/theme.js", "/static/js/ui.js", @@ -13,7 +12,7 @@ const APP_SHELL = [ self.addEventListener("install", (event) => { event.waitUntil( - caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)).then(() => self.skipWaiting()) + caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS)).then(() => self.skipWaiting()) ); }); @@ -27,9 +26,32 @@ self.addEventListener("activate", (event) => { self.addEventListener("fetch", (event) => { if (event.request.method !== "GET") return; + + const requestUrl = new URL(event.request.url); + const isStaticAsset = + requestUrl.origin === self.location.origin && + ( + requestUrl.pathname.startsWith("/static/") + || requestUrl.pathname === "/app.webmanifest" + || requestUrl.pathname === "/service-worker.js" + ); + + if (event.request.mode === "navigate") { + event.respondWith( + fetch(event.request).catch(() => caches.match(event.request)) + ); + return; + } + + if (!isStaticAsset) { + return; + } + event.respondWith( caches.match(event.request).then((cached) => { - if (cached) return cached; + if (cached) { + return cached; + } return fetch(event.request).then((response) => { if (!response || response.status !== 200 || response.type !== "basic") { return response;