Skip to content

Swaps: another origin's module scripts do not decide the swap; a fallback load still morphs - #161

Merged
tiavision merged 3 commits into
mainfrom
ccr-b0c6b86d-92y7mr
Dec 6, 2026
Merged

tiavision merged 3 commits into
mainfrom
ccr-b0c6b86d-92y7mr

Conversation

@tiavision

Copy link
Copy Markdown
Member

Problem

On realworld.tiaforge.dev, Cloudflare Web Analytics injects a module script from another origin into every page:

<script type="module" src="https://static.cloudflareinsights.com/beacon.min.js/…" integrity="sha512-…" crossorigin="anonymous">

Two things went wrong because of it:

  1. The swap failed whenever the beacon couldn't load. prepare import()ed every script[type=module][src] of the next page and gave up if any import failed. With the beacon blocked (by an ad blocker, or by a network that doesn't allow its host), every link fell back to a full page load. The import() also ignored the tag's integrity, so a swap ran a script that a page load would have blocked.
  2. That fallback load didn't morph. fail() used location.assign(), whose navigation carries no link. transition.ts then found no row on pageswap, so no data-sn-transition pair was named.

Reproduced against the live site in Chromium: with the beacon blocked, every article link did a full load with no title morph; with the beacon stubbed in, every link swapped and morphed.

Fix

  • src/swap-run.ts: only the page's same-origin module scripts are imported, and only they decide whether the swap succeeds. Module scripts from another origin are run after the commit as real <script> elements, so integrity, crossorigin and the nonce apply. They run once per document, the swap doesn't wait for them, and if one fails it fails alone, as on a page load.
  • src/swap.ts + src/links.ts: the swap's own navigations (the fallback load, and the navigation it starts again after a history write) go through navigation.navigate() with the link in info, the same way navigate() calls already do. The load's pageswap then finds the row and the pair morphs.
  • Docs (docs/navigation.md: swaps, page transitions) and a changelog entry.

Tests

New steps in e2e/permanent/run.mjs, using a second server on another origin (localhost against the pages' 127.0.0.1) that injects a beacon into B the way Cloudflare does:

  • a beacon that doesn't load: the page still swaps;
  • a beacon whose integrity doesn't match: the page swaps, and the beacon is blocked;
  • a beacon that loads: it runs after the swap, and only once per document;
  • a swap whose page answers an error, so it loads instead: the load still morphs the pair (in browsers with transitions between documents).

In Chromium, all steps pass, and without the src/ changes three of the four new ones fail (marker null, beacon 1, pair false). Firefox and WebKit aren't installed in this environment; CI runs both. npm test (3,599 tests), npm run typecheck and size --check pass.


Generated by Claude Code

…back load still morphs

The swap imported every module script of the next page and loaded the page instead if any failed. A CDN's
injected analytics beacon (Cloudflare's, on realworld.tiaforge.dev) blocked by an extension or offline turned
every swap into a load. Only the page's own-origin modules are imported and awaited now; another origin's run
after the commit through script elements, once per document, so their `integrity` and `crossorigin` apply
(an `import()` ignored the integrity).

A swap that loads instead navigated with `location.assign()`, whose navigation carries no link: the page's
`pageswap` found no row and nothing morphed. It now navigates with the link in `info`, as `navigate()` calls
do, here and where a history write makes the swap navigate again.

e2e/permanent: a beacon on another origin that does not load, whose integrity does not match, and that runs
once per document; and a swap that loads instead still morphing its pair.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AjKzdzTBYrLUCJWpL1Vstr
Firefox has no `pagereveal`: nothing is recorded, which is no transitions between documents, not a failed
morph. WebKit reports a script whose integrity does not match without its URL: filtered as the others are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AjKzdzTBYrLUCJWpL1Vstr
… error

Not as a console message: the filter goes where the steps' page collects its errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AjKzdzTBYrLUCJWpL1Vstr
@tiavision
tiavision merged commit dda4f9a into main Dec 6, 2026
20 checks passed
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.

2 participants