Skip to content

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

Closed

Description

@thinkclay

Investigated as the follow-on to #791. The splitting itself works and is worth roughly 37% of per-page JavaScript. The resulting app does not start. Not shipped; this records the attempt so the next one starts from the diagnosis rather than repeating it.

app.config.ts carried // NOTE: asyncRoutes is disabled — see below. with no "below" — this is the missing explanation.

What enabling it achieves

plugins: [['expo-router', { asyncRoutes: { web: true, default: false } }]], then expo export --platform web:

before   1 entry bundle                     13.60 MB
after    180 chunks
           __common                          7.44 MB
           entry                             1.06 MB
           index-f84b76… (mapbox route)      1.85 MB
           index-f341c3… (assessment route)  0.99 MB
           per-route chunks                  0.02 MB and under

Per page, measured by resolving the script tags the SSR HTML emits:

/          8.52 MB      /privacy   8.51 MB
/jobs      8.53 MB      /terms     8.51 MB

13.60 MB → ~8.52 MB per page, -37%. And the two libraries #791 called out did move off the shared path:

mapbox-gl     -> index-f84b76…   (route chunk)
luscher-test  -> index-f341c3…   (route chunk)
lucide        -> __common        (correct — used by nearly every route)

SSR survives intact: every page still server-renders its title, /jobs still emits its ItemList JSON-LD with 3 entries, / still emits 3 structured-data blocks.

Why it does not boot

Every page renders blank, with:

Uncaught (in promise) Error: Requiring unknown module "1313".  at entry-*.js
Uncaught            Error: Requiring unknown module "930".     at entry-*.js

The cause, end to end:

  1. The SSR HTML emits all six script tags with async="":

    <script src="/_expo/static/js/web/__common-….js" async="">
    <script src="/_expo/static/js/web/entry-….js" async="">
  2. async means execution order is whatever finishes downloading first — not document order.

  3. entry is 1.06 MB; __common is 7.44 MB. Entry reliably wins.

  4. Modules 930 and 1313 are both defined in __common — verified by searching every emitted chunk — so entry runs and requires modules that have not been registered yet.

This is size-dependent, which makes it nastier than a flat failure: with a small enough __common the race could resolve the other way and the build would appear to work, then break when the shared chunk grows.

What would fix it

The scripts need ordered execution — defer rather than async, or a loader that waits for the shared chunk. That emission is inside Expo's SSR HTML generation, not app code, so the options are roughly:

  • an upstream fix / issue against expo-router + @expo/cli for multi-chunk SSR output
  • a post-render rewrite of the script attributes in apps/scaffald/server/index.js — I attempted this as a proof and the middleware did not intercept, because the SSR response streams rather than going through res.send. Worth another look with the stream wrapped properly; if it renders, that both confirms the diagnosis and is a viable stopgap.
  • checking whether a newer expo-router emits defer (this is [email protected], [email protected])

Acceptance

  • asyncRoutes: { web: true } produces a build that boots on every public route
  • Per-page JS around 8.5 MB rather than 13.6 MB, with mapbox-gl and luscher-test in route chunks
  • SSR metadata and JSON-LD unchanged (they already survive splitting)
  • Client navigation between routes works, including a cold jump into a split route
  • Lighthouse re-run on /jobs and /privacy, before/after recorded on First Lighthouse baseline: /jobs has a 94s LCP, and no public page meets its budget #774
  • Replace the dangling // NOTE: asyncRoutes is disabled — see below. with a link to this issue

Related: #791 (bundle audit — 31% of the bundle is feature code every page carries), #774, #795.

Activity

  1. added
    bug
    on Sep 17, 2026
  2. thinkclay commented on Sep 17, 2026

    @thinkclay
    ContributorAuthor

    Checked the upstream angle so the next attempt does not repeat it (2026-09-17):

    • No newer Expo emits defer for this path. In both the installed @expo/[email protected] and the current @expo/[email protected], Expo's own static HTML emitters write defer (build/src/start/server/metro/serializeHtml.js:90, build/src/export/html.js:30) — and the multi-chunk branch in serializeHtml.js:105-106 has the defer line commented out with a TODO, identical in both versions. Upgrading to SDK 57 (deps: Expo SDK 56 -> 57 must be one coordinated bump, not 7 individual Dependabot PRs #483) will not fix this on its own.
    • The async="" you observed does not come from those emitters (none of them writes async), and nothing in expo-router, @expo/cli or expo-server passes bootstrapScripts. The remaining candidate is React 19's hoistable-resource rendering during the SSR stream (a <script async src> element rendered from the router's script-tag component becomes async="" in the output). First step next time: grep the exported server bundle in dist/server/ for async next to _expo/static/js, not node_modules — the emission is compiled into the route render.
    • That makes the stopgap the realistic route: wrap the stream in apps/scaffald/server/index.js (it uses expo-server/adapter/express's createRequestHandler at line 62, so intercept the response body write, not res.send) and rewrite async="" → defer="" on <script src="/_expo/static/js/…"> tags. Since defer preserves document order and serializeHtml already orders __common before entry, that is sufficient. File the upstream issue in parallel, linking this one.

    Also a reminder that this is the only remaining lever that moves TBT/TTI: the nightly on c67f930d2 shows 2.9 MB on the wire per page but TBT 2.0–2.4 s and TTI ~17.5 s — the cost is parsing and executing 13.6 MB of JS, not downloading it.

  3. thinkclay commented on Sep 17, 2026

    @thinkclay
    ContributorAuthor

    Correction to my note above, and the diagnosis, with the PR up.

    There is an upstream fix — I looked in the wrong packages. The streaming SSR renderer is not in expo-router or @expo/cli; it is @expo/router-server (build/server/renderStreamingContent.js), and it passes the chunk list to React as bootstrapScripts, which is where async="" comes from. @expo/[email protected] already replaces that with preload links in the head and <script defer> in the body, in asset order — their comment names this exact race. So the SDK 57 bump (#483) does fix it after all; what is true is that no 56.x release does.

    The stopgap is not the server rewrite. apps/scaffald/server/index.js is what the Lighthouse job and the Dockerfile run, but production is the EAS Hosting worker (scripts/deploy-web-eas.sh), which never executes that file. A rewrite there would have made CI green and left scaffald.com blank. The fix that reaches every runtime is to change the rendered HTML itself: a pnpm patch on the installed @expo/[email protected] back-porting the 57.0.10 change, compiled into dist/server/_expo/server/render.js at export.

    Everything else in the issue held: the split is 13.6 MB → 8.5 MB per page (2.88 → 1.55 MB brotli), mapbox-gl and the assessment corpus are in their own route chunks, SSR metadata and JSON-LD survive, and the failure really is size-dependent. Two things the acceptance list did not anticipate: the hydration audit reports clean on a build whose JavaScript died before hydration (it only greps hydration messages), so there is now a scripts/audit/boot-pages.mjs that fails on "Requiring unknown module", a page error, or an empty root — and the Lighthouse job runs it before measuring anything.

    Lighthouse before/after follows from the ci:full run on the PR.

  4. thinkclay commented on Sep 17, 2026

    @thinkclay
    ContributorAuthor

    Shipped in #803. Before/after from the same machine and harness (devtools throttling, mobile) is in the PR; the CI run on the final commit (35219924138, devtools throttling, median of 3):

    page perf FCP LCP SI TBT TTI CLS
    / 0.78 1607 1607 1851 944 13000 0.016
    /contact 0.79 1266 1266 2225 925 12975 0.002
    /jobs 0.68 2689 3035 3445 926 14073 0.000
    /privacy 0.78 1260 1260 2867 930 12967 0.000
    /support 0.74 1562 1562 3025 926 12977 0.113
    /terms 0.79 1266 1266 2550 909 12580 0.001

    Against last night's main (simulated throttling, so directionally only): TBT ~2.0–2.4 s → ~0.93 s, TTI ~17.5 s → ~13 s, LCP run-to-run spread on / now 100 ms. The only assertion warning is the pre-existing /support CLS 0.113 (#802 step 2).

    One finding the acceptance list did not anticipate, now written into the workflow: the first CI runs reported LCP of 13 s on / and /contact in two runs out of three. That was Lighthouse's default simulated throttling — Lantern estimating from an unthrottled trace — folding hydration into LCP whenever Chrome's unthrottled first paint happened to land after the deferred chunks had already executed (they finish in ~450 ms on localhost). Observed LCP was 0.1–0.9 s in every one of those runs, and a Playwright probe under real throttling showed a single LCP candidate with the server-rendered node still in the DOM. The nightly now uses throttlingMethod: devtools; pre-#803 nightly numbers are not comparable, and the budget ratchet waits for the first nightly on main.

    Acceptance: boots on every public route ✓ (12/12 page×device, new boot-pages.mjs in the job) · ~8.5 MB per page with mapbox-gl and luscher-test in route chunks ✓ · SSR metadata and JSON-LD unchanged ✓ · client navigation into a split route ✓ · Lighthouse before/after ✓ (above and in the PR) · dangling NOTE replaced ✓.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Frontendbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions