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 22:31:43 +02:00
1 parent dd53c79b39
commit 405692f228
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.
* 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 resolved = path.resolve(root, relative || 'index.html');
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;
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, {
...SECURITY_HEADERS,
'Content-Type': MIME_TYPES[extension] ?? 'application/octet-stream',
'Content-Length': info.size,
// The SPA shell must never be cached, its modules are revalidated on each load.
'Cache-Control': extension === '.html' ? 'no-store' : 'no-cache',
ETag: etag,
'Last-Modified': new Date(info.mtimeMs).toUTCString(),
});
createReadStream(file).pipe(response);
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
// dashboard shell, which would send a visitor to a login screen.
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);
return;
}