Skip to content

Serve the Events and i18n runtimes from a content-hashed URL #578

Closed

Description

@JuroOravec

What happens now

Citry serves the Events browser runtime at {prefix}/ext/events/runtime.js and the i18n runtime at {prefix}/ext/i18n/runtime.js. Neither URL contains a version, and both answer with Cache-Control: no-store. As a result:

  • every page load downloads the full Events runtime again (about 400 KB, the same file as citry.js), and the i18n runtime with it;
  • the browser cannot keep either file between pages.

no-store is there because the Events plan pairs a rendered page with its runtime only through that one URL (runtime-route amendment). An upgraded runtime has to reach the browser at the same URL.

During #574 we tried no-cache with an ETag, so the browser asks before each use and gets 304 while its copy is current. Review found a gap: the back and forward buttons may reuse a stored script without asking the server. Right after a deploy, a page reached that way can get new HTML with the old runtime. With security_script_integrity on, the page then fails its integrity check and never starts. So 0.7.0 keeps no-store.

Proposal

Serve each runtime from a URL that contains a hash of its bytes, for example {prefix}/ext/events/runtime.<hash>.js, with Cache-Control: public, max-age=31536000, immutable, like the Events definitions and stylesheets already are. The page links the hash of the runtime it was rendered for. Then:

  • the browser keeps the runtime for a year and does not ask again on each page load (no per-use 304);
  • after a deploy, a new page links a new URL, so it can never run with an old runtime, on a normal load or a back/forward one;
  • an old page keeps asking for its own hash.

citry.js (the dependencies runtime) serves the same bytes and also stays on no-store in 0.7.0 (#574), so it moves to the same scheme.

This reverses a plan decision

The Events plan says the runtime keeps a stable URL with no version in it ("There is no runtime query-version compatibility protocol"). This issue reverses that decision, so write a short plan before implementing it. The plan should cover:

  • the URL shape, and where the page gets the hash (emission, the fragment preloader, integrity, and preload hints all name this URL);
  • whether the stable URL stays as an alias (with no-store or no-cache) for pages and tools that hard-code it;
  • static export (static_deps.py), the preview host, and the docs playground;
  • what moves for i18n, whose runtime has its own build ID.

Error modes the plan must state

  • An old page asks for a hash that is gone. A page rendered before a deploy, or reopened from history, asks for runtime.<old-hash>.js after the server only holds the new runtime. Choose one: answer 404 (with no-store, so the miss is not cached), and say what the page does next (it never starts; does it need a reload message?); or keep serving recent hashes for a while (how many, where they are stored, and how they are dropped across workers and restarts).
  • Rolling deploys. Workers on different versions answer different hashes. A page rendered by a new worker may ask an old worker for the new hash. Decide whether that is a 404, a retry, or something the shared cache covers.
  • A malformed or unknown hash (wrong length or characters): 404, the same as the definitions route.
  • A CDN in front of the app that keeps 404 answers for longer than intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions