BR-007: never cache a redirected or failed shell response

This commit is contained in:
ai-bot committed 2026-10-14 12:48:03 +00:00
1 parent 528ff51ec2
commit e030da9bf4
1 file changed
+13 -2
+13 -2
View File
@@ -61,6 +61,15 @@ function isShellRequest(request) {
return url.origin === self.location.origin && SHELL_PATHS.has(url.pathname); 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) => { self.addEventListener("fetch", (event) => {
const { request } = event; const { request } = event;
if (!isShellRequest(request)) { if (!isShellRequest(request)) {
@@ -72,8 +81,10 @@ self.addEventListener("fetch", (event) => {
.then((response) => { .then((response) => {
// A fresh copy replaces the cached shell, so an update is picked up as // A fresh copy replaces the cached shell, so an update is picked up as
// soon as the server answers. // soon as the server answers.
const copy = response.clone(); if (isCacheable(response)) {
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy)); const copy = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
}
return response; return response;
}) })
.catch(() => .catch(() =>