diff --git a/src/web/http.js b/src/web/http.js index 5f9c914..766f9cf 100644 --- a/src/web/http.js +++ b/src/web/http.js @@ -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; diff --git a/src/web/server.js b/src/web/server.js index ab5375e..2dc855c 100644 --- a/src/web/server.js +++ b/src/web/server.js @@ -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; } diff --git a/tests/web-server.test.js b/tests/web-server.test.js index c49eb42..b55b343 100644 --- a/tests/web-server.test.js +++ b/tests/web-server.test.js @@ -95,6 +95,27 @@ describe('dashboard server', () => { 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 () => { const response = await app.fetch('/'); assert.equal(response.headers.get('x-frame-options'), 'DENY');