Skip to main content

Module http_cache

Module http_cache 

Source
Expand description

Conditional requests and cache directives for the v2 API.

Every API response used to carry no ETag, no Cache-Control, no Last-Modified and no Vary. Two consequences, one for each side:

  • Clients re-transfer everything. A SPA polling its navbar pays the full body on every request because there is nothing to revalidate against.
  • Shared caches can leak. tree, menus/{slug} and detail’s children are all functions of the session cookie, and a cache keyed on the URL alone may hand a member’s response to the next anonymous caller. The HTML path already guards against this — persist_locale appends Vary: Cookie to every page response — but the API router is mounted as a sibling and never saw that layer.

This middleware fixes both, and it is deliberately dumb: it hashes the rendered body. No updated_at bookkeeping to keep in sync, no way for the validator to drift from the content, and it works for endpoints (like the tree) whose body depends on rows from four tables plus the caller’s permissions. The cost is that the handler still runs — this saves bandwidth, not database work.

Functions§

layer
Attach validators and directives, and answer If-None-Match with 304.