fix(web): give static assets a validator caches can check

The front-end was served with `no-cache` but no `ETag` and no
`Last-Modified`, so a cache had nothing to revalidate against and could keep
handing out modules older than the API they talk to — which surfaces as a
cryptic error in a view rather than as anything resembling a stale cache.

Assets now carry both, and an unchanged file answers 304.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nocodeandClaude Opus 5 committed 2026-09-23 23:20:07 +02:00
1 parent 3bd290cba6
commit 539b1506f4
3 files changed
+35 -2

No files matched your search

+13 -1
View File
@@ -132,7 +132,7 @@ export function clientIp(request) {
* Serves a file from `root`, refusing anything that escapes it. * Serves a file from `root`, refusing anything that escapes it.
* Returns false when the file does not exist, so the caller can fall back. * Returns false when the file does not exist, so the caller can fall back.
*/ */
export async function sendStatic(response, root, requestPath, { fallback = null } = {}) { export async function sendStatic(response, root, requestPath, { fallback = null, request = null } = {}) {
const relative = decodeURIComponent(requestPath).replace(/^\/+/, ''); const relative = decodeURIComponent(requestPath).replace(/^\/+/, '');
const resolved = path.resolve(root, relative || 'index.html'); const resolved = path.resolve(root, relative || 'index.html');
const target = resolved.startsWith(path.resolve(root)) ? resolved : null; const target = resolved.startsWith(path.resolve(root)) ? resolved : null;
@@ -150,12 +150,24 @@ export async function sendStatic(response, root, requestPath, { fallback = null
if (!info?.isFile()) return false; if (!info?.isFile()) return false;
const extension = path.extname(file).toLowerCase(); const extension = path.extname(file).toLowerCase();
// `no-cache` alone tells a cache to revalidate, but without a validator it has
// nothing to revalidate against — which is how a browser or a CDN ends up
// serving a front-end older than the API it talks to.
const etag = `"${info.size.toString(16)}-${Math.floor(info.mtimeMs).toString(16)}"`;
if (request?.headers?.['if-none-match'] === etag) {
response.writeHead(304, { ...SECURITY_HEADERS, ETag: etag }).end();
return true;
}
response.writeHead(200, { response.writeHead(200, {
...SECURITY_HEADERS, ...SECURITY_HEADERS,
'Content-Type': MIME_TYPES[extension] ?? 'application/octet-stream', 'Content-Type': MIME_TYPES[extension] ?? 'application/octet-stream',
'Content-Length': info.size, 'Content-Length': info.size,
// The SPA shell must never be cached, its modules are revalidated on each load. // The SPA shell must never be cached, its modules are revalidated on each load.
'Cache-Control': extension === '.html' ? 'no-store' : 'no-cache', 'Cache-Control': extension === '.html' ? 'no-store' : 'no-cache',
ETag: etag,
'Last-Modified': new Date(info.mtimeMs).toUTCString(),
}); });
createReadStream(file).pipe(response); createReadStream(file).pipe(response);
return true; return true;
+1 -1
View File
@@ -75,7 +75,7 @@ export function createHandler({ client = null, setupOnly = false, startedAt = Da
// The wiki is a separate public page: it must never fall back to the // The wiki is a separate public page: it must never fall back to the
// dashboard shell, which would send a visitor to a login screen. // dashboard shell, which would send a visitor to a login screen.
const fallback = pathname === '/wiki' || pathname.startsWith('/wiki/') ? 'wiki.html' : 'index.html'; const fallback = pathname === '/wiki' || pathname.startsWith('/wiki/') ? 'wiki.html' : 'index.html';
const served = await sendStatic(response, publicDir, pathname, { fallback }); const served = await sendStatic(response, publicDir, pathname, { fallback, request });
if (!served) sendEmpty(response, 404); if (!served) sendEmpty(response, 404);
return; return;
} }
+21
View File
@@ -95,6 +95,27 @@ describe('dashboard server', () => {
assert.match(response.headers.get('content-type'), /javascript/); assert.match(response.headers.get('content-type'), /javascript/);
}); });
it('gives its assets a validator so a cache can revalidate', async () => {
const response = await app.fetch('/core/dom.js');
assert.ok(response.headers.get('etag'), 'no ETag on a static asset');
assert.ok(response.headers.get('last-modified'), 'no Last-Modified on a static asset');
});
it('answers 304 when the cached copy is still current', async () => {
const first = await app.fetch('/core/dom.js');
const etag = first.headers.get('etag');
const second = await app.fetch('/core/dom.js', { headers: { 'If-None-Match': etag } });
assert.equal(second.status, 304);
assert.equal(await second.text(), '');
});
it('sends the file again when the validator does not match', async () => {
const response = await app.fetch('/core/dom.js', { headers: { 'If-None-Match': '"stale"' } });
assert.equal(response.status, 200);
assert.ok((await response.text()).length > 0);
});
it('sends the security headers on every response', async () => { it('sends the security headers on every response', async () => {
const response = await app.fetch('/'); const response = await app.fetch('/');
assert.equal(response.headers.get('x-frame-options'), 'DENY'); assert.equal(response.headers.get('x-frame-options'), 'DENY');