From e030da9bf491c0a5dda25fc9c5e5aee3ee7ba027 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Wed, 14 Oct 2026 12:48:03 +0000 Subject: [PATCH] BR-007: never cache a redirected or failed shell response --- static/sw.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/static/sw.js b/static/sw.js index aa4bd7d..7e11054 100644 --- a/static/sw.js +++ b/static/sw.js @@ -61,6 +61,15 @@ function isShellRequest(request) { return url.origin === self.location.origin && SHELL_PATHS.has(url.pathname); } +// Only the shell itself is stored: a successful, same-origin answer that was not +// redirected. A page fetched without a session answers 302 -> /login, and fetch +// follows it, so without this check the login page would replace the cached +// shell and the app would show the wrong screen offline. A failed answer is left +// out for the same reason (section 31 point 6). +function isCacheable(response) { + return response.ok && response.type === "basic" && !response.redirected; +} + self.addEventListener("fetch", (event) => { const { request } = event; if (!isShellRequest(request)) { @@ -72,8 +81,10 @@ self.addEventListener("fetch", (event) => { .then((response) => { // A fresh copy replaces the cached shell, so an update is picked up as // soon as the server answers. - const copy = response.clone(); - caches.open(CACHE_NAME).then((cache) => cache.put(request, copy)); + if (isCacheable(response)) { + const copy = response.clone(); + caches.open(CACHE_NAME).then((cache) => cache.put(request, copy)); + } return response; }) .catch(() =>