From a025b04e8e6bcf73756d6c267f9d35ba0b0253b7 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Tue, 13 Oct 2026 15:42:43 +0000 Subject: [PATCH] static: intercept only the shell paths in the service worker (section 31 point 6) --- static/sw.js | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/static/sw.js b/static/sw.js index 09848f4..e613d2e 100644 --- a/static/sw.js +++ b/static/sw.js @@ -1,15 +1,16 @@ "use strict"; // The VardKey service worker of section 31 point 6. It caches only the static -// shell, never an API response, never /health, and never the login page, so the -// app can never show stale or unauthenticated data. Every fetch still goes to -// the server first; the cache is only there for the shell while the server is -// unreachable. +// shell, never an API response, never the health endpoint, and never the login +// page, so the app can never show stale or unauthenticated data. It does that +// with an allowlist: only a request for one of the shell paths below is handled +// at all. Everything else is left to the browser and goes straight to the +// server. Every shell fetch still tries the server first; the cache is only +// there while the server is unreachable. const CACHE_NAME = "vardkey-shell-v1"; -// The shell: the pages and the assets they need. /api/*, /health and /login are -// deliberately absent. +// The shell: the pages and the assets they need. const SHELL = [ "/", "/setup", @@ -24,6 +25,7 @@ const SHELL = [ "/health-check.js", "/history.js", ]; +const SHELL_PATHS = new Set(SHELL); self.addEventListener("install", (event) => { event.waitUntil( @@ -52,15 +54,8 @@ function isShellRequest(request) { return false; } const url = new URL(request.url); - if (url.origin !== self.location.origin) { - return false; - } - // The API, the readiness endpoint and the login page always go to the server. - return !( - url.pathname.startsWith("/api/") - || url.pathname === "/health" - || url.pathname === "/login" - ); + // A query string, such as the history detail link, still matches its path. + return url.origin === self.location.origin && SHELL_PATHS.has(url.pathname); } self.addEventListener("fetch", (event) => {