Skip to content

perf(deps): let the browser cache component scripts and stylesheets - #574

Merged
JuroOravec merged 2 commits into
mainfrom
fix/asset-cache-control
Nov 1, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/asset-cache-control

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

What changes

The Dependencies routes, including /citry/citry.js, answered with no Cache-Control, so whether the browser kept a copy was left to its own guesses. Each route now says what the browser may do:

URL Cache-Control Why
cache/<class_id>.<hash>.<js|css> (a class version) public, max-age=31536000, immutable The hash is a hash of the bytes served.
asset/<hash>.<ext> public, max-age=31536000, immutable The file name is the hash of the file.
cache/<class_id>.<js|css> no-cache + ETag, 304 for a current copy The URL stays the same when the content changes.
citry.js no-store The same bytes as the Events runtime at an unversioned URL; see below.
cache/<class_id>.<vars_hash>.css (css_data() stylesheet) no-cache + ETag The hash covers the data. The stylesheet text around it comes from Citry's code and can change on upgrade.
Any 404 from these routes no-store Another worker may store the file a moment later.

The immutable value is the one the Events definition and stylesheet routes already sent. It is now one constant (IMMUTABLE_CACHE_CONTROL) that both extensions use. The WSGI adapter gains a 304 Not Modified status line.

citry.js and the Events and i18n runtimes (ext/events/runtime.js, ext/i18n/runtime.js) all use no-store in 0.7.0. With no-cache, a back or forward navigation may reuse a stored runtime without asking, so right after a deploy a page could pair new HTML with the old runtime and, with security_script_integrity on, fail its integrity check. The v1.1 follow-up serves the runtimes from content-hashed URLs instead (#578).

Component URLs without a content hash keep no-cache + ETag. Script integrity applies to scripts only. Citry's pages link component JavaScript by its content-hashed URL, so the unhashed cache/<class_id>.js is not what an integrity-checked page loads. The css_data() stylesheet is CSS, so integrity does not apply to it. A stale copy after an upgrade that changes the stylesheet format would only look wrong until a reload.

Tests

packages/py/citry/tests/test_asset_cors.py:

  • Every public asset route sends the expected policy through both the WSGI and ASGI adapters.
  • A matching If-None-Match gets 304 with the caching and CORS headers.
  • Weak tags, lists of tags and * match. Other tags do not.
  • A css_data() stylesheet and an unhashed .css are revalidated.
  • Every 404 path sends no-store.
  • Two Citry instances give a component script the same tag.
  • With the source reverted to main, the new tests fail.

Review

An independent review ran, with a security pass and a prose pass. Its findings are folded in: the variables stylesheet moved off immutable, the 404s got no-store, the tag is computed once per content, and the docs and changelog wording was corrected. Security verdict: If-None-Match is only compared, never echoed back, and the 304s keep CORS and nosniff. No route can be poisoned per user, because each one answers GET without reading request data.

Known and left as it is: Django's CommonMiddleware adds Content-Length: 0 to the 304. Django's own conditional-GET responses do the same, and browsers ignore it.

🤖 Generated with Claude Code

A URL whose hash is a hash of the bytes it serves (a class version's
script or stylesheet, a served Dependencies file) now answers with
Cache-Control: public, max-age=31536000, immutable, the policy the Events
definition and stylesheet routes already use. citry.js,
cache/<class_id>.<js|css>, and the css_data() variables stylesheet can
change at the same URL, so they answer with Cache-Control: no-cache and an
ETag, and return 304 with no body for a current copy. A 404 from these
routes says no-store, since another worker may store the file a moment
later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
citry.js serves the same bytes as ext/events/runtime.js at an unversioned
URL. A copy reused on back/forward navigation could pair a page with
another runtime and fail its integrity check, so it keeps no-store until
the runtimes move to content-hashed URLs (#578). Component URLs keep
no-cache with an ETag.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the fix/asset-cache-control branch from efc8cfc to 52c84e3 Compare November 1, 2026 18:55
@JuroOravec JuroOravec changed the title perf(deps): let the browser cache citry.js and component scripts perf(deps): let the browser cache component scripts and stylesheets Nov 1, 2026
@JuroOravec
JuroOravec merged commit b3b58e6 into main Nov 1, 2026
2 of 24 checks passed
@JuroOravec
JuroOravec deleted the fix/asset-cache-control branch November 1, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant