Skip to content

Commit 6273854

Browse files
Carlos MaldonadoCarlos Maldonado
authored andcommitted
Prevent control UI index caching
1 parent 64d4f94 commit 6273854

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/gateway/control-ui.http.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,20 @@ describe("handleControlUiHttpRequest", () => {
368368
});
369369
});
370370

371+
it("serves control-ui index with no-store cache headers", async () => {
372+
await withControlUiRoot({
373+
fn: async (tmp) => {
374+
const getRes = runControlUiRequest({ url: "/", method: "GET", rootPath: tmp });
375+
expect(getRes.handled).toBe(true);
376+
expect(getRes.res.setHeader).toHaveBeenCalledWith("Cache-Control", "no-store");
377+
378+
const headRes = runControlUiRequest({ url: "/", method: "HEAD", rootPath: tmp });
379+
expect(headRes.handled).toBe(true);
380+
expect(headRes.res.setHeader).toHaveBeenCalledWith("Cache-Control", "no-store");
381+
},
382+
});
383+
});
384+
371385
it("includes CSP hash for inline scripts in index.html", async () => {
372386
const scriptContent = "(function(){ var x = 1; })();";
373387
const html = `<html><head><script>${scriptContent}</script></head><body></body></html>\n`;

src/gateway/control-ui.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,16 @@ export function handleControlUiAvatarRequest(
468468
}
469469
}
470470

471+
function controlUiCacheControlForPath(filePath: string): string {
472+
return path.basename(filePath).toLowerCase() === "index.html"
473+
? "no-store"
474+
: "no-cache";
475+
}
476+
471477
function setStaticFileHeaders(res: ServerResponse, filePath: string) {
472478
const ext = path.extname(filePath).toLowerCase();
473479
res.setHeader("Content-Type", contentTypeForExt(ext));
474-
// Static UI should never be cached aggressively while iterating; allow the
475-
// browser to revalidate.
476-
res.setHeader("Cache-Control", "no-cache");
480+
res.setHeader("Cache-Control", controlUiCacheControlForPath(filePath));
477481
}
478482

479483
function serveResolvedFile(res: ServerResponse, filePath: string, body: Buffer) {
@@ -490,7 +494,7 @@ function serveResolvedIndexHtml(res: ServerResponse, body: string) {
490494
);
491495
}
492496
res.setHeader("Content-Type", "text/html; charset=utf-8");
493-
res.setHeader("Cache-Control", "no-cache");
497+
res.setHeader("Cache-Control", "no-store");
494498
res.end(body);
495499
}
496500

0 commit comments

Comments
 (0)