Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(web): never cache the SPA page or a missing asset across releases

jeffreye3074e3eb8fc
diff --git a/apps/web/src/test/static-host.test.ts b/apps/web/src/test/static-host.test.ts
index 84be0baf..91193900 100644
--- a/apps/web/src/test/static-host.test.ts
+++ b/apps/web/src/test/static-host.test.ts
@@ -205,13 +205,31 @@ describe("Web static host cache policy", () => {
expect(response.headers.get("access-control-allow-origin")).toBe("*");
});
- it("keeps the SPA document revalidatable", async () => {
- const response = await fetch(`${baseUrl}/login`, {
- headers: { accept: "text/html", connection: "close" },
- });
+ it("never lets the HTTP cache replay the SPA document", async () => {
+ for (const requestPath of ["/login", "/", "/index.html"]) {
+ const response = await fetch(`${baseUrl}${requestPath}`, {
+ headers: { accept: "text/html", connection: "close" },
+ });
- expect(response.status).toBe(200);
- expect(response.headers.get("cache-control")).toBe("no-cache");
+ expect(response.status).toBe(200);
+ expect(response.headers.get("cache-control")).toBe("no-store");
+ }
+ });
+
+ it("forbids caching a missing asset so a rollout miss does not stick", async () => {
+ for (const [requestPath, accept] of [
+ ["/assets/index-old111.js", "*/*"],
+ ["/assets/index-old111.js", "text/html"],
+ ["/unknown.png", "*/*"],
+ ["/agents", "application/json"],
+ ] as const) {
+ const response = await fetch(`${baseUrl}${requestPath}`, {
+ headers: { accept, connection: "close" },
+ });
+
+ expect(response.status).toBe(404);
+ expect(response.headers.get("cache-control")).toBe("no-store");
+ }
});
it("answers a revalidation with the current page after an update", async () => {
@@ -343,7 +361,7 @@ describe("Web static host content negotiation", () => {
expect(response.status).toBe(200);
expect(response.headers.get("content-encoding")).toBe("br");
expect(response.headers.get("content-type")).toContain("text/html");
- expect(response.headers.get("cache-control")).toBe("no-cache");
+ expect(response.headers.get("cache-control")).toBe("no-store");
await expect(response.text()).resolves.toBe(indexDocument);
});
diff --git a/apps/web/static-host.mjs b/apps/web/static-host.mjs
index dd8c1971..aa7232e1 100644
--- a/apps/web/static-host.mjs
+++ b/apps/web/static-host.mjs
@@ -33,12 +33,7 @@ export function createStaticHost({ root } = {}) {
if (pathname.startsWith("/assets/")) {
response.setHeader("access-control-allow-origin", "*");
}
- response.setHeader(
- "cache-control",
- pathname.startsWith("/assets/")
- ? "public, max-age=31536000, immutable"
- : "no-cache",
- );
+ response.setHeader("cache-control", cacheControlFor(pathname));
},
});
@@ -49,15 +44,13 @@ export function createStaticHost({ root } = {}) {
new URL(request.url ?? "/", "http://localhost").pathname,
);
} catch {
- response.statusCode = 404;
- response.end();
+ respondNotFound(response);
return;
}
setBrowserSecurityHeaders(response, pathname);
if (request.method !== "GET" && request.method !== "HEAD") {
- response.statusCode = 404;
- response.end();
+ respondNotFound(response);
return;
}
@@ -77,22 +70,38 @@ export function createStaticHost({ root } = {}) {
serveNegotiatedFile(serveFiles, request, response, () => {
const pathname = new URL(request.url ?? "/", "http://localhost").pathname;
if (!isBrowserNavigation(request, pathname)) {
- response.statusCode = 404;
- response.end();
+ respondNotFound(response);
return;
}
const requestUrl = request.url;
request.url = "/index.html";
serveNegotiatedFile(serveFiles, request, response, () => {
- response.statusCode = 404;
- response.end();
+ respondNotFound(response);
});
request.url = requestUrl;
});
};
}
+// A release ships only its own fingerprinted assets, so a page from another
+// release asks for files this one lacks. The page must never be replayed from
+// the HTTP cache (history navigation and restored tabs skip no-cache
+// revalidation), and a miss must not be cached either: without an explicit
+// policy the CDN adds its browser TTL and the 404 outlives the rollout.
+function cacheControlFor(pathname) {
+ if (pathname.startsWith("/assets/")) {
+ return "public, max-age=31536000, immutable";
+ }
+ return extname(pathname) === ".html" ? "no-store" : "no-cache";
+}
+
+function respondNotFound(response) {
+ response.statusCode = 404;
+ response.setHeader("cache-control", "no-store");
+ response.end();
+}
+
function serveNegotiatedFile(serveFiles, request, response, onNoMatch) {
const originalAcceptEncoding = request.headers["accept-encoding"];
const encoding =