ETag and 304, for static files and buffered responses #114

Closed
opened 2026-09-25 14:14:48 +00:00 by chrnx · 2 comments
Owner

Part of the API freeze (#110). Decided: in before rc.1, designed together with compression (#112).

Static: a weak ETag from the stat the resolver already does (src/views.ts:122), W/"<size>-<mtime>", plus Last-Modified. A matching If-None-Match (or If-Modified-Since when there's no ETag) answers 304 without reading the file. Today every hit reads and sends the whole file.

Handlers: createApp({ etag: true }), off by default.

  • A buffered 200 to GET/HEAD gets a weak ETag from a cheap hash of the body (not a cryptographic one: weak ETags only need change detection). A matching If-None-Match becomes a 304 with no body.
  • An etag the handler set itself (a row version, say) is kept and only compared.
  • Streams are out: there's no whole body to hash.
  • It saves bandwidth, not work: the handler still runs. Skipping the database is already possible with @header('if-none-match') and the existing NotModified signal (src/signals.ts:60).

With compression: the hash is taken before encoding, and the ETag stays weak so it can describe every encoding of one body. The 304 carries ETag and Vary the way a 200 would.

Out: Range / 206, until someone asks. It only matters for media and resumable downloads, and it's where the edge cases pile up (multiple ranges, If-Range, 416).

Tests: static 304 without readFile (spy), handler 304, handler-set ETag respected, If-None-Match: * and lists, compression plus ETag together, and all four runtimes.

Part of the API freeze (#110). Decided: in before rc.1, designed together with compression (#112). **Static:** a weak ETag from the `stat` the resolver already does (src/views.ts:122), `W/"<size>-<mtime>"`, plus `Last-Modified`. A matching `If-None-Match` (or `If-Modified-Since` when there's no ETag) answers 304 **without reading the file**. Today every hit reads and sends the whole file. **Handlers:** `createApp({ etag: true })`, off by default. - A buffered `200` to `GET`/`HEAD` gets a weak ETag from a cheap hash of the body (not a cryptographic one: weak ETags only need change detection). A matching `If-None-Match` becomes a 304 with no body. - An `etag` the handler set itself (a row version, say) is kept and only compared. - Streams are out: there's no whole body to hash. - It saves bandwidth, not work: the handler still runs. Skipping the database is already possible with `@header('if-none-match')` and the existing `NotModified` signal (src/signals.ts:60). **With compression:** the hash is taken before encoding, and the ETag stays weak so it can describe every encoding of one body. The 304 carries `ETag` and `Vary` the way a 200 would. **Out:** `Range` / `206`, until someone asks. It only matters for media and resumable downloads, and it's where the edge cases pile up (multiple ranges, `If-Range`, 416). **Tests:** static 304 without `readFile` (spy), handler 304, handler-set ETag respected, `If-None-Match: *` and lists, compression plus ETag together, and all four runtimes.
chrnx added the enhancement label 2026-09-25 16:06:54 +00:00
Author
Owner

Merged into develop in #144 (CARD-15, FPRD-001c, 2026-10-23). Left open until the 1c acceptance gate (CARD-22), per the 1a/1b convention.

Merged into develop in #144 (CARD-15, FPRD-001c, 2026-10-23). Left open until the 1c acceptance gate (CARD-22), per the 1a/1b convention.
Author
Owner

Fixed in 26.11.0-rc.1 (d32fc9f). From the changelog:

  • Static files are revalidated, not re-downloaded (#114, D12). Every static hit carries a weak ETag (W/"<size>-<mtime>"), Last-Modified and Cache-Control: no-cache. A matching If-None-Match, or If-Modified-Since when there is no If-None-Match, answers 304 from the file's stat without reading it. A HEAD no longer reads the file either. no-cache stops browsers from reusing a file for a lifetime they guessed from Last-Modified. To keep the old behaviour, set static: { cacheControl } to whatever your proxy expects.

  • createApp({ etag: true }) (#114, D12): weak ETags for buffered 200 responses to GET and HEAD, from a 32-bit FNV-1a hash of the body plus its length. A matching If-None-Match (weak comparison, lists, *) gets a 304 with no body. A handler that sets its own etag keeps it, and that tag drives the comparison. Streams, non-200 responses and other methods are never tagged. The handler still runs. Off by default; anything but a boolean throws at createApp.

  • static: { dir?, cacheControl? } (#114): the object form of static sets the Cache-Control sent with static files and their 304s (for example 'public, max-age=31536000, immutable' for hashed assets). static: true and static: '<dir>' keep working. An unknown key, or a cacheControl with CR/LF, throws at createApp.

  • Every 304 is bodiless (#114). A 304 has no body and no content-* headers on any adapter, including one from throw new NotModified(), which used to go out with the default JSON envelope's content-type.

Fixed in **26.11.0-rc.1** (`d32fc9f`). From the changelog: - **Static files are revalidated, not re-downloaded** (#114, D12). Every static hit carries a weak `ETag` (`W/"<size>-<mtime>"`), `Last-Modified` and `Cache-Control: no-cache`. A matching `If-None-Match`, or `If-Modified-Since` when there is no `If-None-Match`, answers `304` from the file's `stat` without reading it. A `HEAD` no longer reads the file either. `no-cache` stops browsers from reusing a file for a lifetime they guessed from `Last-Modified`. To keep the old behaviour, set `static: { cacheControl }` to whatever your proxy expects. - **`createApp({ etag: true })`** (#114, D12): weak ETags for buffered `200` responses to `GET` and `HEAD`, from a 32-bit FNV-1a hash of the body plus its length. A matching `If-None-Match` (weak comparison, lists, `*`) gets a `304` with no body. A handler that sets its own `etag` keeps it, and that tag drives the comparison. Streams, non-200 responses and other methods are never tagged. The handler still runs. Off by default; anything but a boolean throws at `createApp`. - **`static: { dir?, cacheControl? }`** (#114): the object form of `static` sets the `Cache-Control` sent with static files and their `304`s (for example `'public, max-age=31536000, immutable'` for hashed assets). `static: true` and `static: '<dir>'` keep working. An unknown key, or a `cacheControl` with CR/LF, throws at `createApp`. - **Every `304` is bodiless** (#114). A `304` has no body and no `content-*` headers on any adapter, including one from `throw new NotModified()`, which used to go out with the default JSON envelope's `content-type`.
chrnx closed this issue 2026-11-18 01:04:06 +00:00
Sign in to join this conversation.