Skip to content

feat(router): compile script ownership and static hydration - #2092

Merged
domgan-oai merged 7 commits into
octanejs:mainfrom
openai-oss-forks:codex/strong-router-scripts-2059
Oct 15, 2026
Merged

domgan-oai merged 7 commits into
octanejs:mainfrom
openai-oss-forks:codex/strong-router-scripts-2059

Conversation

@domgan-oai

@domgan-oai domgan-oai commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Continue #2059 by compiling Router’s shared script owner and server-only scroll-restoration range without changing their lifetime. Fresh route scripts execute on client navigation; existing server scripts are adopted without replay; self-removing Start transport leaves no stale item frame.

A literal cloned script template is inert on navigation. Router now uses a Strong keyed host template with a compiler-internal __executeOnMount marker; it is not a public JSX option. Only those script tokens prepare executable sources on the first parsed cache read for each namespace. Ordinary template records, their factory and resolver retain main’s layout and path. Runtime-provided Router attributes cannot forward the reserved marker. The scroll script uses the existing permanent-static hydration range, and static-only client lowering avoids retaining deferred activation code. Router and its SSR-query dependent explicitly select Octane’s TSRX type compiler in their Octane-owned programs, preventing mixed React/Octane dependencies from selecting React’s projection. Pristine React comparison programs retain their compiler.

Validation

  • pnpm format:check — scoped changed-file formatting passes locally; the full gate passes in current-head CI.
  • pnpm typecheck — affected source/public typing programs pass locally; the full gate and both Router-reaching adapted parity lanes pass in current-head CI.
  • pnpm test — current-head sharded CI and separate website/browser integration lanes pass; no monolithic full-workspace local run is claimed.
  • Targeted tests: 185 Router/Start tests across 33 files; 164 compiler/runtime/Router owning tests; 38 runtime script tests; six Chromium script cases, including controlled external-script response ordering. These scopes overlap and are not additive. Staged DOM checks and four-mode Strong compilation pass. Website analysis covers all 98 authored source files with zero diagnostics; recursive traversal leaves the three modules listed below.
  • Actual website: final-source development and production builds/runs compared with the frozen main 3691ea2b using the same Chromium harness and dependency graph. All 24 paired hydration/navigation journeys and eight paired preboot scroll cases pass across desktop/narrow viewports. After the Router type-compiler configuration fix, the website rebuilt with all 614 public JavaScript files and four Lynx containers byte-identical to the browser-tested output; the later SSR-query follow-up changes only its type configurations and audit metadata. Original document/shell and script nodes are adopted; route transitions, history, controls and preboot 610px scroll restoration behave as on main. Existing Lynx teardown diagnostics and deliberate 404s match main; no request failures occurred. Narrow viewport is emulation, not physical-device or deployed-site proof.
  • Final-source complete production website inventory: 614 public JavaScript files, 19,411,562 → 19,405,648 raw bytes (−5,914) and 4,353,883 → 4,352,564 summed level-9 per-file gzip bytes (−1,319). Four binary Lynx containers are separate: raw 2,145,537 unchanged, gzip 667,163 → 667,168 (+5). These are full emitted inventories, not initial-route payloads or runtime timing.
  • Matched isolated complete Router fixture: ordinary entry +6,250 raw / +2,200 gzip, compiled entry −4,758 raw / −1,055 gzip versus main. The ordinary minimal core fixture is byte-identical to main, removing the prior +48 raw/+15 gzip common-path cost. The opted script fixture grows +132 raw/+23 gzip from the previous PR runtime; these fixtures do not measure a real app’s payload.
  • pnpm sync; independent read-only complexity/performance review found no must-fix findings.

Current-head CI passed all 27 jobs on signed 86a051348, including the Router and SSR-query parity shards, 54,421 general tests (two skipped), 105/105 website tests, 620/620 Chromium tests, types, lint, publication, examples and bundle gates. Bugbot passed with no new findings. The current-head benchmark passed: all 64 work counters and all 13 complete application-JavaScript gzip metrics are unchanged. The full-export framework fixture grows 775 raw / 213 gzip bytes, and three deferred/split reachability fixtures grow 6–10 gzip bytes. One of 20 paired timings, TSRX select_lots, is flagged slower (+8.6%, 95% CI +6.6% to +10.5%; raw averages 0.005541 → 0.006200 ms); the other 19 are within the report’s noise threshold. Independent source review found no changed selection mechanism in that fixture, but the timing difference remains unexplained; this is not a speed-equivalence claim.

Risk / follow-ups

  • Script preparation now costs three cold cache accessors per opted token, then replaces each used accessor with a cached node. This isolates setup from ordinary templates; it is not a speed claim. External scripts follow browser dynamic-insertion semantics: route-array order and defer do not serialize their execution. An inserted hidden primary may execute before a later sibling suspends, as with the prior descriptor owner.
  • The ordinary Router size tradeoff applies to a real surface: the Start plugin defaults to the ordinary entry, and the TanStack Start and TanStack.com benchmark apps render its <Scripts /> with scroll restoration. Their exact application deltas were not measured. The Octane website explicitly selects the compiled entry.
  • A superseded CI run timed out before an accessibility scan while waiting for animations on /benchmarks in dark mode. The test and inspected animation sources match main, eight matched local main/candidate probes passed, and the final-head website lane passed unchanged; the original timeout’s exact cause remains unknown.
  • Local validation reused a fingerprint-verified matching dependency graph after the configured dependency firewall blocked a fresh install; hosted fresh installation is a separate gate. Pristine upstream Router coverage remains recorded-unverified, and Router is an existing strict packed-source TypeScript-matrix exception.
  • Router compiled/Matches.tsrx, Router linkBinding.ts, Three web/events.ts, broader descriptor elimination and final package-level enforcement remain under Run the website in Strong mode #2059.

Provenance

  • An agent produced this diff (agent-authored)

Codex task · implementation host com-79386.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.


Note

Medium Risk
Changes script execution semantics, hydration adoption, and Router SSR/client navigation paths; mistakes could cause double execution, skipped scripts, or broken scroll restoration.

Overview
Adds a compiler-only __executeOnMount flag on native <script> templates so opted-in scripts use scriptTemplate (live document scripts on clone) while ordinary templates stay inert. Volar strips the marker from public DOM typing; spreads and non-DOM renderers are rejected.

Permanent-static hydration now renders via exported __PermanentStaticHydrate with a direct import, so client bundles can drop deferred Hydrate activation code; bundle benchmarks add a permanent-static scenario to enforce that. Range preservation no longer assumes an active hydration pass when skipping.

TanStack Router moves route scripts to Strong keyed templates with mount execution, strips caller-supplied __executeOnMount from runtime attrs, and wraps scroll-restoration / ScriptOnce output in permanent-static ranges (server HTML adopted, scroll script removed after hydration). Router tsconfigs pin octane/compiler/volar for TSRX typing. Start/document hydration tests assert script adoption, input retention, and no duplicate script insertion.

Docs, status notes, and broad compiler/browser tests cover execution vs replay, CSP nonces, and external script ordering.

Reviewed by Cursor Bugbot for commit 86a0513. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent feat PR: new feature or capability labels Oct 15, 2026
@trueadm
trueadm marked this pull request as ready for review October 15, 2026 14:36

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is ON, but it could not run because the branch was deleted or merged before autofix could start.

Reviewed by Cursor Bugbot for commit 107c05b. Configure here.

Comment thread packages/octane/src/compiler/compile.js Resolved
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🟡 js-framework: 1 timed operation(s) slower beyond ±3%
  • 🔴 bundle-size: 6 value(s) increased
  • 🔴 bundle-reachability: 8 value(s) increased

Compares 3691ea2b1, the merge commit's first parent with 583d16167 on the same runner. Any increase in a js-framework work counter fails this check; byte changes are a report. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

target operation base ms head ms head/base 95% CI
octane-tsrx select_lots 0.006 0.006 +8.6% +6.6% … +10.5% 🟡 slower
All 20 timed operations (19 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 3.608 3.317 ±0.0% −3.2% … +8.1% ⚪ within noise
octane-tsrx replace 8.950 9.142 +5.9% −0.4% … +11.6% ⚪ within noise
octane-tsrx add 2.525 2.642 ±0.0% −3.3% … +4.2% ⚪ within noise
octane-tsrx update 0.146 0.145 −1.4% −3.0% … +0.5% ⚪ within noise
octane-tsrx select 0.006 0.006 −1.1% −2.4% … +1.5% ⚪ within noise
octane-tsrx swap 0.018 0.018 −1.0% −3.6% … +0.8% ⚪ within noise
octane-tsrx remove 0.019 0.019 +0.3% −3.7% … +7.6% ⚪ within noise
octane-tsrx runlots 25.483 25.042 −0.8% −4.5% … +5.2% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 +8.6% +6.6% … +10.5% 🟡 slower
octane-tsrx clear 47.425 46.633 +1.0% −3.2% … +3.6% ⚪ within noise
octane-jsx run 4.000 4.292 −2.8% −6.8% … +9.0% ⚪ within noise
octane-jsx replace 10.292 9.317 −1.0% −8.4% … +3.4% ⚪ within noise
octane-jsx add 3.867 4.083 +2.9% −2.3% … +11.3% ⚪ within noise
octane-jsx update 0.287 0.288 −0.8% −4.3% … +2.8% ⚪ within noise
octane-jsx select 0.146 0.147 −1.2% −2.9% … +0.2% ⚪ within noise
octane-jsx swap 0.200 0.198 +1.2% −2.2% … +3.3% ⚪ within noise
octane-jsx remove 0.192 0.192 −1.0% −2.7% … +2.8% ⚪ within noise
octane-jsx runlots 33.808 33.650 +0.1% −2.1% … +2.5% ⚪ within noise
octane-jsx select_lots 1.760 1.729 −1.2% −3.4% … +1.5% ⚪ within noise
octane-jsx clear 55.933 58.000 +1.3% −5.4% … +8.7% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

target metric base head Δ
octane-tsrx full_fw_raw 570,628 571,403 +775 (+0.1%) 🔴
octane-tsrx full_fw_gzip 172,687 172,900 +213 (+0.1%) 🔴
octane-tsrx full_fw_brotli 145,372 145,583 +211 (+0.1%) 🔴
octane-jsx full_fw_raw 570,628 571,403 +775 (+0.1%) 🔴
octane-jsx full_fw_gzip 172,687 172,900 +213 (+0.1%) 🔴
octane-jsx full_fw_brotli 145,372 145,583 +211 (+0.1%) 🔴

87 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
deferred-hydration raw 191,620 191,633 +13 (+0.0%) 🔴
deferred-hydration gzip 61,236 61,242 +6 (+0.0%) 🔴
deferred-hydration brotli 53,390 53,377 −13 (−0.0%) 🟢
split-shell raw 415,274 415,287 +13 (+0.0%) 🔴
split-shell gzip 133,031 133,040 +9 (+0.0%) 🔴
split-shell brotli 114,511 114,538 +27 (+0.0%) 🔴
split-shell-runtime raw 332,429 332,442 +13 (+0.0%) 🔴
split-shell-runtime gzip 105,536 105,546 +10 (+0.0%) 🔴
split-shell-runtime brotli 89,929 89,941 +12 (+0.0%) 🔴
binding-base-ui-esbuild brotli 61,515 61,477 −38 (−0.1%) 🟢
binding-aria-esbuild brotli 2,808 2,805 −3 (−0.1%) 🟢

109 other measured values are unchanged.

@trueadm trueadm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI on 732550f is fully green (30/30), all 64 work counters are unchanged, and the test coverage is thorough. I'm leaving this as a comment rather than an approval for two reasons: it adds new public API that needs the owner's sign-off, and there's a small cost that every app pays.

1. executeOnMount is a new public, non-React attribute. It's typed on ScriptHTMLAttributes in jsx-runtime.d.ts, documented in docs/differences-from-react.md, and shipped as an octane minor changeset. The semantics look carefully drawn:

  • static boolean only, with spreads rejected;
  • HTML and DOM templates only;
  • stripped from server HTML;
  • adopted without replay on hydration;
  • opts out of resource hoisting.

The implementation matches that description. But Octane's public surface has so far been React parity plus documented divergences, and this is the first author-facing opt-in that React has no equivalent for. React 19 leaves client-inserted scripts inert unless they're async src resources. That needs an explicit yes from Dominic before it merges. If the only consumer is Router's Scripts, a compiler-internal spelling (a __-prefixed attribute, or a directive that isn't in the public typings) would get the same lowering without committing to API.

2. Every app pays about 48 B raw / 15–19 B gzip. From the benchmark on this head (run 38757178462):

  • TSRX js-framework is +48 / +14 gzip, todo/chat/weather +48 / +16–18, Threadline and Pagecraft +48 / +13–19.
  • JSX js is +48 / +14.
  • All reachability roots (hooks-state, root-static-local, component-owned-effects…) are +48 / +15–19.
  • full_fw is +643 / +179, which comes from the new exports.

The +48 comes from the uniform prepare: null field in every template() record (runtime.ts ~21232) and the lazy.prepare !== null test in resolveLazyTemplate. Only scriptTemplate uses them. The amount is under what I'd block on, but it's paid by apps that never render an executable script.

Could scriptTemplate own its preparation instead? For example, it could return a token whose record resolves through its own path. Or the hook could be a driver that scriptTemplate installs and resolveLazyTemplate reads by truthiness. I'd keep the shape stability you get from always initializing the field, but put it on a record that only scriptTemplate creates. Separately, the body's own isolated measurement (ordinary Router +2,109 gzip, compiled Router −1,149) deserves a line on which real consumers take the ordinary entry, since that's a large step for them.

Correctness notes (none blocking):

  • scriptTemplate's prepare (runtime.ts ~21240) calls view.setAttributeNS(attr.namespaceURI, attribute.name, attribute.value). That mixes the raw attr with the staged attribute view. Use attribute.namespaceURI so the staged-DOM path reads consistently.
  • Execution order. Router's managed external scripts that carry executeOnMount but no async will execute in arbitrary order. A script created by createElement and then cloned inserts as force-async. That's the same as the old descriptor path, so it's not a regression. If upstream relies on ordering between <script src> entries, though, the docs line "async ordering still follow[s] the browser's rules" undersells it: the force-async flag isn't carried across cloneNode, so fixing it on the cached source wouldn't help. A test or a docs sentence stating that sibling external scripts are unordered would settle it.
  • Runtime spreads. The spread rejection only catches statically visible object literals. A runtime spread containing executeOnMount (for example, if script.attrs ever carried one) would fall through to setAttribute('executeonmount', …). That's harmless but worth a dev warning.
  • ScriptOnce / CompiledScrollRestoration. Moving these to <Hydrate split={false} when={never()}> with trustHTML reuses the existing permanent-static range. Importing __PermanentStaticHydrate directly so static-only consumers don't retain deferred activation is a good find, and the Vite/esbuild hydration-free gate now pins it.

What I'd need in order to approve:

  • Dominic's decision on (1): either keep executeOnMount public, with the docs and changeset as written, or make it compiler-internal.
  • For (2): the per-app +48 B gone through a template record or driver that only scriptTemplate creates, or a measured reason it has to stay.
  • The setAttributeNS view fix.

Reviewed with Claude Code

@domgan-oai

Copy link
Copy Markdown
Contributor Author

Addressed on signed head 86a051348:

  • Took the internal-capability path: Router is the only production caller found in the repository, so __executeOnMount is absent from public JSX types and user-facing documentation; the Octane changeset is patch. Router strips the reserved key from runtime-supplied attributes.
  • Moved preparation entirely onto script tokens. The ordinary template record/factory/resolver are identical to main, and the matched ordinary core fixture is byte-identical to main. Script tokens pay three cold cache accessors, then cache each prepared namespace.
  • Read the attribute namespace through the staged view. Controlled Chromium tests also confirm that sibling external scripts, including those with defer, follow dynamic-insertion load ordering.
  • The ordinary entry is real: the Start plugin defaults to it, and both TanStack Start and TanStack.com benchmark apps render its Scripts with scroll restoration. The matched isolated Router fixture grows 2,200 gzip bytes for the ordinary entry and shrinks 1,055 for compiled; those are fixture deltas, not measured app deltas. The complete production website JavaScript inventory shrinks 1,319 summed per-file gzip bytes against main.

Final-source website hydration/navigation and preboot scroll match main in development/production at desktop/narrow widths; focused tests, source/public typing checks, Strong analysis and staged DOM checks pass. Current-head CI passes all 27 jobs, including 105 website and 620 Chromium tests and both Router-reaching parity lanes. The benchmark confirms all 13 complete application-JavaScript gzip metrics and all 64 work counters are unchanged. It flags one of 20 timed operations slower (select_lots, +8.6%; raw averages 0.005541 → 0.006200 ms); the full-export framework fixture grows 213 gzip bytes. Details and limitations are in the PR description.

@domgan-oai
domgan-oai merged commit 9bceb81 into octanejs:main Oct 15, 2026
32 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent feat PR: new feature or capability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants