Skip to content

Let Back restore a leaf.page page the visitor has acted on: every Leaf server answers no-cache - #5717

Merged
max-sixty merged 1 commit into
mainfrom
leaf-page-no-cache
Nov 8, 2026
Merged

max-sixty merged 1 commit into
mainfrom
leaf-page-no-cache

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

leaf.page now answers a page's document, state and media under the cache policy the local server already used, so Back restores a page the visitor has acted on from the browser's back/forward cache instead of loading it again. This builds the decision on #4024: one cache policy for every Leaf server.

What changed

The policy now has one home: CACHE_CONTROL = "no-cache" and IMMUTABLE_CACHE_CONTROL = "private, max-age=31536000, immutable" in src/leaf/http.py. They are module constants rather than PageEndpoint class attributes, so no endpoint can override them, and the revision-resource answer uses the second instead of its own copy of the string. The comment above them records the reversal and the cost accepted.

Three copies of the old no-store choice are deleted:

  • WebsitePageEndpoint.cache_control = "no-store" and media_cache_control (worker/leaf_website/__init__.py) made the container answer everything a visitor's page loads with no-store: its document, state, event answers and attached images.
  • SampleEndpoint copied its parent's two attributes (src/leaf/http.py), so a live sample's child page on leaf.page answered no-store like its parent.
  • The Durable Object replaced the Cache-Control Python captured on every saved response with private, no-store (worker/src/storage.ts). Those saved responses serve the acted-on page's document and state while the container sleeps. The Durable Object now serves Python's headers unchanged.

The edge's own answers:

  • The published state answer for a visitor who hasn't acted (staticState, also used during a rollout) now says no-cache with Vary: Leaf-Data-Version, Leaf-View-Revision, the request headers its body depends on, just as Python's state answer names its own. Without Vary, a document Chrome loads from history could reuse a stored answer whose data was left out for another tab.
  • Four POST acknowledgements (the startup beacon, interaction batches, a passive reader's page fault and an unrecorded read) drop no-store. Browsers don't store the answer to a fetch POST, so it protected nothing.
  • Kept: no-store on the rollout refusal (503 this release is still starting). It protects against a stored error, not a page's document, state or media: a history load reuses a stored answer without asking. In Chromium 156 with the back/forward cache off, Back showed a stored no-cache 503 document again without a request, while a no-store one was fetched again. Without no-store, the refusal could replace the page after the rollout ends.

notes/browser-support.md ("Page cache"), worker/README.md (saved state) and the private-state paragraph of docs/website.html now describe the single policy and its cost.

What the visitor gains, and the cost accepted

On Back, the page reappears at once with its scroll position, selection, focus and open disclosures, and the runtime catches up from there. Before this change, it loaded again, and Chrome or Firefox showed its state after about 100–180 ms. Attached images are also no longer fetched again on every load.

The cost, accepted by the maintainer: a visitor's comments, replies and images stay in the browser's disk cache after their session. Back shows them to whoever next uses that browser profile.

How Chrome decided

Measured in Playwright's full Chromium 156.0.8078.4 with its back/forward cache enabled. A no-store document alone is now kept. Chrome drops it once a fetch from the page also gets a no-store answer, or once an HttpOnly cookie changes. Acting on a leaf.page page does both: the activation cookie is set, and the container answers the page's state reads. A no-cache document was kept even when its fetches got no-store answers or set cookies. That is why the edge's POST acknowledgements did not block the back/forward cache once the document said no-cache.

Tests

  • tests/process/website_server/test_routes.py: the website's document, state and an attached image now assert the shared policy. On main the same assertions fail with 'no-store' == 'no-cache'. The sample test's image upload is removed: it existed to check that the parent's policy reached the child, and that copy no longer exists.
  • worker/test/storage.test.ts: a saved image is served with the Cache-Control Python captured. worker/test/index.test.ts: the published state answer says no-cache with its Vary. Both fail on main: expected 'private, no-store' and expected 'no-store'.
  • pnpm run check:worker and pnpm run check pass. The website_server route and saved-record tests, the local server's cache-header test, the sample, media and news tests in test_interact_server.py, and test_a_page_loaded_from_history_reads_state_from_the_server pass.
  • Back in a real browser: headless Chromium 156 (back/forward cache enabled) against the container's server (leaf_website, run as the container runs it) on the built site. It opened heat-loss, chose an option, reloaded to get the private document, selected a paragraph, followed a link and pressed Back 5 times. With this branch, all 5 Backs restored the page: pageshow.persisted was true and the selection was still there. With main's container code, all 5 reloaded, with MainResourceHasCacheControlNoStore and JsNetworkRequestReceivedCacheControlNoStoreResource.
  • Not run end to end: the same journey through wrangler dev. On this Mac, the local Worker's first request to a page container hung. leaf-dev verify-site website-worker hung the same way at its activation read, and the container logged no request. The PR's test job runs that verifier on Linux. Safari remains unverified.

Closes #4024

This was written by Claude Code on behalf of max-sixty

…f server answers no-cache

leaf.page now answers a page's document, state and media under the cache
policy the local server uses, so Chrome keeps a page the visitor has acted
on in its back/forward cache. The policy has one home, `CACHE_CONTROL` and
`IMMUTABLE_CACHE_CONTROL` in `src/leaf/http.py`; the website override, the
sample child's copy of it and the Durable Object's `private, no-store` are
deleted. Reverses leaf.page's `no-store` choice, per the decision on #4024.
@max-sixty
max-sixty merged commit f2f04b0 into main Nov 8, 2026
5 checks passed
@max-sixty
max-sixty deleted the leaf-page-no-cache branch November 8, 2026 22:22
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.

Decide whether Back may restore a leaf.page page whole

2 participants