Skip to content

perf(web): route-level code splitting — 13.6 MB → 8.5 MB of JS per page - #803

Merged
thinkclay merged 3 commits into
mainfrom
fix/797-route-split-defer
Sep 17, 2026
Merged

thinkclay merged 3 commits into
mainfrom
fix/797-route-split-defer

Conversation

@thinkclay

@thinkclay thinkclay commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes #797. Part of #802.

What

Enables asyncRoutes: { web: 'production' } and makes the result boot.

before (main) after (this branch)
JS per public page, raw 13.60 MB (one entry bundle) 8.51 MB (runtime + __common 7.4 MB + entry 1.1 MB + route chunk)
JS per public page, brotli 2.88 MB 1.55 MB
mapbox-gl in every page its own 1.9 MB route chunk
assessment corpus (luscher-test) in every page its own 1.0 MB route chunk

Measured, same machine, same harness (devtools throttling, mobile, median of 2)

page perf FCP LCP SI TBT TTI CLS
/ 0.77 → 0.75 1323 → 1330 1323 → 1330 1396 → 1419 699 → 508 17148 → 12566 0.018 → 0.017
/jobs 0.80 → 0.85 1293 → 1559 1293 → 1559 1345 → 1627 684 → 546 16840 → 12244 0.000 → 0.000
/privacy 0.79 → 0.79 1356 → 1290 1586 → 1290 1647 → 1528 839 → 566 17249 → 12596 0.000 → 0.000
/terms 0.79 → 0.86 1286 → 1300 1286 → 1300 1463 → 1520 816 → 471 16894 → 12120 0.001 → 0.001
/contact 0.81 → 0.87 1288 → 1290 1288 → 1290 1316 → 1324 713 → 485 17161 → 12521 0.002 → 0.002
/support 0.77 → 0.80 1305 → 1318 1305 → 1318 1485 → 1534 757 → 582 17151 → 12553 0.114 → 0.114

Paint metrics unchanged (they were already fine), TBT −25–40 %, TTI −27 % on every page — the parse/execute cost the split removes. The CI run on the same commit is linked from #797.

Why it never booted, and the fix

The SSR HTML emitted every chunk with async="", so the 1 MB entry chunk regularly executed before the 7.4 MB __common chunk it requires — Requiring unknown module "1313" on every page. The async is React's bootstrapScripts: @expo/router-server's streaming renderer (build/server/renderStreamingContent.js) handed it the chunk list. Expo's own static emitters already write defer; SSR does not use them.

Upstream fixed exactly this in @expo/[email protected] — preload links in the head, <script defer> in the body, in asset order (their comment: "React's bootstrapScripts emits async scripts, but Metro chunks must execute in asset order"). This PR back-ports that change as a pnpm patch on the installed 56.0.18, with one addition: the preload links keep fetchPriority="low", as React's did — at default priority the shared chunk contended with the fonts and /support's first paint slipped from 2 s to 10 s in the first CI run. Because the patch is compiled into dist/server/_expo/server/render.js at export time it covers every runtime: the Express server CI measures with, the EAS Hosting worker production runs on, and expo serve. A rewrite in apps/scaffald/server/index.js (the stopgap #797 proposed) would have fixed CI and left production broken, since production never runs that file.

Drop the patch with the SDK 57 bump (#483). Recorded in .radium/ci-deployment.md beside the other pins, and in the app.config.ts comment that used to say "see below" with no below.

'production' rather than true: the dev server keeps eager bundling, so pnpm web and the Playwright e2e job (which timed out on waitForLoadState against a split dev server in the first run) are unchanged. expo export is always a production build.

The harness change

.lighthouserc.json now sets throttlingMethod: devtools. Lighthouse's default simulate records an unthrottled trace and has Lantern estimate the metrics; on this build the deferred chunks download and execute within ~450 ms on localhost, so whether Chrome's first paint lands before or after them is a coin flip — and when it lands after, Lantern folds all of hydration into LCP. Same commit, same page: 2.2 s and 13 s in alternating runs, with observed LCP at 0.1–0.9 s in every one (a Playwright probe under real throttling confirmed no remount: one LCP candidate, the server-rendered node still in the DOM). The unsplit build never hit this because its 2.6 MB async entry could not arrive before the first paint. Devtools throttling measures the paint that actually happens: three local runs within 60 ms of each other. Runs take ~35 s instead of ~13 s, timeout raised to 35 min. Nightly numbers from before this change are not comparable.

Verification

  • scripts/audit/boot-pages.mjs (new, also runs in the Lighthouse job before it measures): 12/12 page × device pairs render with no page error; desktop client navigation / → /jobs keeps the document and loads the route chunk lazily. It exists because hydration-pages.mjs reported clean on the build whose JavaScript died before hydration — it only greps hydration messages.
  • scripts/audit/hydration-pages.mjs: 18/18 clean across phone / tablet / desktop
  • Script tags: __expo-metro-runtime, __common, entry, layouts, route — all defer, in that order, on every page; JSON-LD unchanged (3 blocks on /, ItemList on /jobs)
  • CI, E2E and Lighthouse green on the branch; pnpm prepush green; pnpm radium:check's three violations are pre-existing and not in touched files

Two traps, written down in .radium/ci-deployment.md: an export without EXPO_PUBLIC_SUPABASE_URL/ANON_KEY throws on the client before rendering, and Metro's cache serves that env-less transform on the next export until $TMPDIR/metro-cache is wiped.

Not in this PR

🤖 Generated with Claude Code

…ge (#797)

`asyncRoutes: { web: true }` splits the single entry bundle into 180 chunks:
mapbox-gl (1.9 MB) and the assessment corpus (1.0 MB) now ship only on the
routes that use them, and every public page drops from 13.6 MB to 8.5 MB of
JavaScript (2.88 MB → 1.55 MB brotli on the wire).

The reason it was never enabled: the SSR HTML emitted every chunk `async`,
so the 1 MB entry chunk could execute before the 7 MB shared chunk it
requires and every page died with "Requiring unknown module". The `async`
comes from React's `bootstrapScripts`, which `@expo/router-server`'s
streaming renderer passed the chunk list to. Upstream fixed exactly this in
`@expo/[email protected]` (preload in the head, `<script defer>` in the
body, in asset order); this back-ports that change as a pnpm patch on the
installed 56.0.18, so it lands in `dist/server/_expo/server/render.js` at
export and covers every runtime — the Express server CI measures with, the
EAS Hosting worker, and `expo serve`. Drop the patch with the SDK 57 bump.

Verified on a served export with the CI env: 12/12 page×device pairs render
with no page errors, the hydration audit is 18/18 clean, desktop client
navigation into the lazily loaded /jobs chunk keeps the document, and the
JSON-LD on / and /jobs is unchanged.

`scripts/audit/boot-pages.mjs` is the regression check the hydration audit
cannot be: it loads each public page in a real browser and fails on
"Requiring unknown module", a page error, or an empty root. The Lighthouse
job now runs it before measuring, because Lighthouse happily reports paint
metrics for a page whose JavaScript died.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@thinkclay thinkclay added Frontend ci:full Run the full nightly suites (Lighthouse, E2E) on this PR labels Sep 17, 2026
thinkclay and others added 2 commits September 17, 2026 07:48
…xports only

The first Lighthouse run on the split build showed /support painting at
10–12 s in two of three runs where every other page moved ~150 ms: the
back-ported preload links load every chunk at default (High) priority, so
the 1.5 MB shared chunk contended with the fonts, which did not start until
it finished. React's bootstrapScripts preload — what production shipped
before — carried fetchPriority="low"; the patch now emits the same. The
scripts are deferred regardless, so nothing waits on the lower priority.

asyncRoutes is 'production' rather than true: the dev server keeps eager
bundling, so `pnpm web` and the Playwright e2e job (which failed on this
branch with waitForLoadState timeouts against the dev server) are unchanged.
`expo export` is a production build, so the split is unaffected.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…n a deferred-script build

Lighthouse's default `simulate` mode records an unthrottled trace and has
Lantern estimate the metrics from it. On the split build the deferred chunks
download and execute within ~450 ms on localhost, so whether Chrome's first
paint lands before or after them is a coin flip — and when it lands after,
Lantern folds all of hydration into LCP: the same commit and page reported
2.2 s and 13 s in alternating runs, with observed LCP at 0.1–0.9 s in every
one. The unsplit build never hit this because its 2.6 MB async entry could
not arrive before the first paint.

`throttlingMethod: devtools` measures the paint under real throttling
instead: LCP 1.28–1.34 s across three local runs on /, TBT ~500 ms, TTI
~12.6 s. Runs take ~35 s each instead of ~13 s, so the job timeout rises to
35 minutes. Numbers from before this change are not comparable.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@thinkclay
thinkclay merged commit 275022c into main Sep 17, 2026
10 checks passed
@thinkclay
thinkclay deleted the fix/797-route-split-defer branch September 17, 2026 13:01
thinkclay added a commit that referenced this pull request Sep 25, 2026
…) (#908)

`expo install --check` resolves expected versions from Expo's live registry,
so when Expo cut the SDK 56 patch set below, the Monorepo integrity job
started failing on every branch at once. That job is gated on `deps ==
'true'`, so only dependency PRs run it — which made it look as though each
of the five open Dependabot PRs was individually broken. #853 bumps
`adm-zip` alone and failed identically.

Catalog pins moved to what the registry expects:

    expo            ~56.0.21 -> ~56.0.22
    expo-constants  ~56.0.25 -> ~56.0.26
    expo-dev-client ~56.0.26 -> ~56.0.27
    expo-image      ~56.0.12 -> ~56.0.13
    expo-linking    ~56.0.17 -> ~56.0.18
    expo-location   ~56.0.25 -> ~56.0.26
    expo-router     ~56.2.20 -> ~56.2.21
    expo-updates    ~56.0.26 -> ~56.0.27

Patch-level within SDK 56. This is not #483, the coordinated 56 -> 57 bump.

## The @expo/router-server patch had to move with it

`[email protected]` pulls `@expo/cli@^56.1.26`, which wants
`@expo/router-server@^56.0.19` — so the #797/#803 patch pinned at 56.0.18
stopped applying and pnpm refused the install with ERR_PNPM_UNUSED_PATCH.

Half of that patch is now upstream. 56.0.19 ships the module-ordering fix
itself: it drops `bootstrapScripts` from `renderToReadableStream` and
injects `createInjectedScriptAsNodes` into head and body, carrying its own
note that React's bootstrapScripts emits async scripts while Metro chunks
must be ordered. That is the half that stopped the route-split build dying
with "Requiring unknown module".

The `fetchPriority="low"` half is not upstream. Without it the 1.5 MB
shared chunk preloads at default priority, contends with fonts, and first
paint slips from 2 s to 10 s on a throttled connection (#797). So the patch
is re-targeted to 56.0.19 and reduced to that one hunk.

Verified in the exported server bundle, not just in node_modules: a web
export puts `fetchPriority:"low"` on the preload links and `defer` on the
body script nodes in `dist/server/_expo/server/render.js`. The remaining
`bootstrapScripts` strings in that file are react-dom's own server
internals, not an expo call site.

Also checked: `expo install --check` reports up to date, `pnpm dedupe
--check` is clean, and `pnpm prepush` passes (build for 9 projects, Deno
suites load, autolinking exports match, overrides/catalog aligned).

Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:full Run the full nightly suites (Lighthouse, E2E) on this PR Frontend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Route splitting works and the result does not boot: async script tags race the shared chunk

1 participant