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): always answer a page revalidation with the current page

Dongdong Chenc850a1dd0582
diff --git a/apps/web/src/test/static-host.test.ts b/apps/web/src/test/static-host.test.ts
index e359f58a..88ad88b3 100644
--- a/apps/web/src/test/static-host.test.ts
+++ b/apps/web/src/test/static-host.test.ts
@@ -1,6 +1,6 @@
import { createServer, type Server } from "node:http";
import { connect } from "node:net";
-import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
+import { mkdtemp, mkdir, rm, utimes, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { brotliCompressSync, gzipSync } from "node:zlib";
@@ -195,6 +195,55 @@ describe("Web static host cache policy", () => {
expect(response.headers.get("cache-control")).toBe("no-cache");
});
+ it("answers a revalidation with the current page after an update", async () => {
+ // Two releases' pages of one size, both unpacked at the epoch.
+ const page = (version: string) =>
+ `<!doctype html><script src="/assets/index-${version}.js"></script>`;
+ const release = async (version: string) => {
+ const root = await mkdtemp(join(tmpdir(), "linger-static-release-"));
+ await writeFile(join(root, "index.html"), page(version));
+ await utimes(join(root, "index.html"), 0, 0);
+ return root;
+ };
+ const serve = async (root: string) => {
+ const host = serveHost(createStaticHost({ root }));
+ await new Promise<void>((resolve) =>
+ host.listen(0, "127.0.0.1", resolve),
+ );
+ const address = host.address() as { port: number };
+ return { host, url: `http://127.0.0.1:${address.port}/` };
+ };
+ const older = await release("old111");
+ const newer = await release("new222");
+ try {
+ const first = await serve(older);
+ const cached = await fetch(first.url, {
+ headers: { accept: "text/html", connection: "close" },
+ });
+ await expect(cached.text()).resolves.toBe(page("old111"));
+ first.host.close();
+ const second = await serve(newer);
+ // What the browser revalidates with: the older response's validators,
+ // or sirv's size-mtime ETag that both pages would share.
+ const response = await fetch(second.url, {
+ headers: {
+ accept: "text/html",
+ connection: "close",
+ "if-none-match":
+ cached.headers.get("etag") ?? `W/"${page("old111").length}-0"`,
+ "if-modified-since": new Date(0).toUTCString(),
+ },
+ });
+ second.host.close();
+ expect(response.status).toBe(200);
+ expect(response.headers.get("etag")).toBeNull();
+ await expect(response.text()).resolves.toBe(page("new222"));
+ } finally {
+ await rm(older, { force: true, recursive: true });
+ await rm(newer, { force: true, recursive: true });
+ }
+ });
+
it("keeps non-fingerprinted public assets revalidatable", async () => {
const response = await fetch(`${baseUrl}/brand/logo.png`, {
headers: { connection: "close" },
diff --git a/apps/web/static-host.mjs b/apps/web/static-host.mjs
index e813c3b9..daf143b9 100644
--- a/apps/web/static-host.mjs
+++ b/apps/web/static-host.mjs
@@ -21,7 +21,12 @@ export function createStaticHost({ root } = {}) {
const staticRoot = root ?? fileURLToPath(new URL("./dist", import.meta.url));
const serveFiles = sirv(staticRoot, {
brotli: true,
- etag: true,
+ // sirv's ETag is W/"size-mtime", and the packaged source unpacks every
+ // file at the epoch: two releases' index.html of one size share it, so
+ // revalidation kept answering 304 with the older page and its assets.
+ // Without one, every revalidation sends the current file; fingerprinted
+ // assets are immutable and never revalidate.
+ etag: false,
extensions: [],
gzip: true,
setHeaders(response, pathname) {