Skip to content
Source
Sign inSign up
TanStack/router

perf(router-core): hydrate without awaiting code-split component chunks #8484

ClosedTarek-kharsa(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/8484/headopened

🎯 Changes

hydrate() awaits loadRouteChunk() for every committed match before setMatches. RouterClient and StartClient render <Await> on that promise, so the React root commits nothing until the whole component closure of the matched routes has downloaded. That includes chrome whose code already sits in the entry chunk, such as a header in the root route. A click on a header <Link> in that window is a native document navigation.

We measured the cost on a production Start app on @tanstack/router-core@1.171.32. The table shows the root commit time, median of 3 runs, at 4x CPU slowdown with 150 ms RTT and 1.6 Mbps:

PageAwaiting chunksWithout the wait
Home4484 ms3375 ms
Search results4969 ms3825 ms
Detail page6237 ms3890 ms
Login3991 ms3372 ms

The route body still waits for its own chunks. The bytes loaded before the commit do not change.

React does not need the wait. A code-split component is a lazyRouteComponent, which suspends on its first render. React keeps the server HTML of a dehydrated Suspense boundary until the promise resolves and does not render the fallback. The root Outlet always puts the child match inside such a boundary.

This PR keeps the loads starting in hydrate() and skips the wait when the router sets _hydrateWithoutComponentChunks. Only the React Router sets it. Vue's lazyRouteComponent renders an empty <div> until its chunk arrives, so Vue must keep the wait. I have not verified Solid's lazy() path through hydration, so Solid also keeps it. Either adapter can opt in later with one line.

A lane with a lazyFn route whose options are not merged (route.lazyFn && route._lazy !== true) still waits for every chunk. Such a route has no component yet and would render <Outlet /> in its place, which is a hydration mismatch. The wait could later be scoped to that route's subtree.

Failure path. React's lazyRouteComponent.preload never rejects. It records the error and throws it on render, where a module-not-found error reloads the document once (guarded in sessionStorage) and any other error goes to the route's error boundary. So for code-split components the failure path does not change. The one change is a hand-written component whose preload rejects. Before, hydrate() called retryFrom on that match. Now the component renders as is, because nothing awaits its preload. Lanes that still wait keep retryFrom.

Tests.

  • packages/react-router/tests/hydration-component-chunks.test.tsx, "commits the matches before a code-split component chunk arrives". A lazyRouteComponent import is held pending. hydrate() resolves, the matches commit, hydrateRoot keeps the server HTML byte for byte before and after the import resolves, and onRecoverableError is not called. On main this test fails with expected 'waiting' to be 'hydrated'.
  • The same file, "waits for lazy route options before committing the matches". A pending lazyFn keeps hydrate() waiting until the options merge. This test passes on main too and guards the kept wait.
  • packages/router-core/tests/hydration-component-chunks.test.ts checks both flag values against a component with a pending preload. Without the flag hydrate() waits, and with the flag it resolves. The flag case fails on main.

I did not find a router option that fits this. The behavior depends on the renderer, not on the app, so I set it in the adapter.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with the relevant test commands, or tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

Local runs, all exit 0: test:unit, test:types, test:eslint, build and test:build for @tanstack/router-core and @tanstack/react-router, and test:unit for @tanstack/react-start-client. I did not run the e2e suites.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features
    • React applications can now complete hydration while a code-split route component is still loading. The server-rendered content remains visible until the component is ready.
  • Bug Fixes
    • Hydration continues to wait for route options that have not yet resolved.
    • Solid and Vue hydration behavior is unchanged.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: Repository: TanStack/router/.coderabbit.yaml

      Review profile: CHILL

      Plan: Advanced

      Run ID: d085f99a-9554-44da-ab10-4c1c3dbada87

      📥 Commits

      Reviewing files that changed from the base of the PR and between fe7f1fd0e6ef73c3f341dd2338b406749538a85d and da70ff35e814fc6e91e6636f808a5ba5ac264dca.

      📒 Files selected for processing (6)
      • .changeset/hydrate-without-component-chunks.md
      • packages/react-router/src/router.ts
      • packages/react-router/tests/hydration-component-chunks.test.tsx
      • packages/router-core/src/load-client.ts
      • packages/router-core/src/router.ts
      • packages/router-core/tests/hydration-component-chunks.test.ts

      Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


      📝 Walkthrough

      Walkthrough

      React Router now allows hydration to commit matches before lazy route component chunks load. Router Core still waits for unresolved lazy route options and retains chunk waiting by default. Tests cover both behaviors and verify that React hydration preserves the server HTML.

      Changes

      Hydration behavior

      Layer / File(s)Summary
      Conditional chunk waiting and validation
      packages/router-core/src/router.ts, packages/router-core/src/load-client.ts, packages/react-router/src/router.ts, packages/router-core/tests/hydration-component-chunks.test.ts, packages/react-router/tests/hydration-component-chunks.test.tsx, .changeset/hydrate-without-component-chunks.md
      Router Core adds an optional flag that controls whether hydration waits for component chunks. React Router enables the flag. Hydration still waits for unresolved lazy route options. Tests cover both waiting behaviors, and the changeset records the package updates.

      Priority: ⬇️ Low

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Feature

      Suggested reviewers: schiller-manuel

      Merge Risk: ⚪ Minimal · up to da70f

      React apps can now finish hydrating before code-split route components download. Those routes keep showing their server-rendered HTML until the chunk arrives. Solid and Vue are unchanged, and routes that still load their options lazily continue to wait. No confirmed defect blocks merging. It is still worth checking how a failed chunk download behaves during hydration, because that path now surfaces as a render-time error instead of a hydration-time retry.

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 5 files. (1 skipped: 1…Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the primary change: React Router hydration no longer waits for code-split component chunks.
      Description check✅ PassedThe description follows the required template, explains the motivation and implementation, documents test coverage and limitations, completes the checklist, and includes the required changeset informa…
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Full details: Docstring Coverage

      Explanation

      Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 5 files. (1 skipped: 1 unsupported.)

      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit da70ff35e814fc6e91e6636f808a5ba5ac264dca

      CommandStatusDurationResult
      nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded13m 7sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded2m 7sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-09-23 15:43:10 UTC

    3. More templates

      @tanstack/arktype-adapter

      npm i https://pkg.pr.new/@tanstack/arktype-adapter@8484

      @tanstack/eslint-plugin-router

      npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@8484

      @tanstack/eslint-plugin-start

      npm i https://pkg.pr.new/@tanstack/eslint-plugin-start@8484

      @tanstack/history

      npm i https://pkg.pr.new/@tanstack/history@8484

      @tanstack/nitro-v2-vite-plugin

      npm i https://pkg.pr.new/@tanstack/nitro-v2-vite-plugin@8484

      @tanstack/react-router

      npm i https://pkg.pr.new/@tanstack/react-router@8484

      @tanstack/react-router-devtools

      npm i https://pkg.pr.new/@tanstack/react-router-devtools@8484

      @tanstack/react-router-ssr-query

      npm i https://pkg.pr.new/@tanstack/react-router-ssr-query@8484

      @tanstack/react-start

      npm i https://pkg.pr.new/@tanstack/react-start@8484

      @tanstack/react-start-client

      npm i https://pkg.pr.new/@tanstack/react-start-client@8484

      @tanstack/react-start-rsc

      npm i https://pkg.pr.new/@tanstack/react-start-rsc@8484

      @tanstack/react-start-server

      npm i https://pkg.pr.new/@tanstack/react-start-server@8484

      @tanstack/router-cli

      npm i https://pkg.pr.new/@tanstack/router-cli@8484

      @tanstack/router-core

      npm i https://pkg.pr.new/@tanstack/router-core@8484

      @tanstack/router-devtools

      npm i https://pkg.pr.new/@tanstack/router-devtools@8484

      @tanstack/router-devtools-core

      npm i https://pkg.pr.new/@tanstack/router-devtools-core@8484

      @tanstack/router-generator

      npm i https://pkg.pr.new/@tanstack/router-generator@8484

      @tanstack/router-plugin

      npm i https://pkg.pr.new/@tanstack/router-plugin@8484

      @tanstack/router-ssr-query-core

      npm i https://pkg.pr.new/@tanstack/router-ssr-query-core@8484

      @tanstack/router-utils

      npm i https://pkg.pr.new/@tanstack/router-utils@8484

      @tanstack/router-vite-plugin

      npm i https://pkg.pr.new/@tanstack/router-vite-plugin@8484

      @tanstack/solid-router

      npm i https://pkg.pr.new/@tanstack/solid-router@8484

      @tanstack/solid-router-devtools

      npm i https://pkg.pr.new/@tanstack/solid-router-devtools@8484

      @tanstack/solid-router-ssr-query

      npm i https://pkg.pr.new/@tanstack/solid-router-ssr-query@8484

      @tanstack/solid-start

      npm i https://pkg.pr.new/@tanstack/solid-start@8484

      @tanstack/solid-start-client

      npm i https://pkg.pr.new/@tanstack/solid-start-client@8484

      @tanstack/solid-start-server

      npm i https://pkg.pr.new/@tanstack/solid-start-server@8484

      @tanstack/start-client-core

      npm i https://pkg.pr.new/@tanstack/start-client-core@8484

      @tanstack/start-fn-stubs

      npm i https://pkg.pr.new/@tanstack/start-fn-stubs@8484

      @tanstack/start-plugin-core

      npm i https://pkg.pr.new/@tanstack/start-plugin-core@8484

      @tanstack/start-server-core

      npm i https://pkg.pr.new/@tanstack/start-server-core@8484

      @tanstack/start-static-server-functions

      npm i https://pkg.pr.new/@tanstack/start-static-server-functions@8484

      @tanstack/start-storage-context

      npm i https://pkg.pr.new/@tanstack/start-storage-context@8484

      @tanstack/valibot-adapter

      npm i https://pkg.pr.new/@tanstack/valibot-adapter@8484

      @tanstack/virtual-file-routes

      npm i https://pkg.pr.new/@tanstack/virtual-file-routes@8484

      @tanstack/vue-router

      npm i https://pkg.pr.new/@tanstack/vue-router@8484

      @tanstack/vue-router-devtools

      npm i https://pkg.pr.new/@tanstack/vue-router-devtools@8484

      @tanstack/vue-router-ssr-query

      npm i https://pkg.pr.new/@tanstack/vue-router-ssr-query@8484

      @tanstack/vue-start

      npm i https://pkg.pr.new/@tanstack/vue-start@8484

      @tanstack/vue-start-client

      npm i https://pkg.pr.new/@tanstack/vue-start-client@8484

      @tanstack/vue-start-server

      npm i https://pkg.pr.new/@tanstack/vue-start-server@8484

      @tanstack/zod-adapter

      npm i https://pkg.pr.new/@tanstack/zod-adapter@8484

      commit: da70ff3

    4. Sheraff(GitHub profile, opens in a new tab)commented
      1. Could you give me a human-readable TL;DR of this change? If i understand correctly we don't await the chunks during hydration (unless they are explicitly lazy) because they are not required for the hydration (they will simply load later, in render, and cause a suspense)

      2. The failure path you are describing, if i understand correctly, is that we're not catching a failed lazy chunk load during hydration anymore, is that right?

      3. Could you enumerate the setups that are impacted and those that aren't? (stuff like auto code splitting, manual code splitting, custom preload, explicit .lazy...)

      4. it'd be interesting to see if we can apply the same logic to Vue and/or Solid. We don't like to advance 1 adapter and leave the other 2 behind (if there isn't a good reason to). And as a bonus if we can apply this to all 3, we can remove _hydrateWithoutComponentChunks completely.

      5. also this is interesting

        The wait could later be scoped to that route's subtree

        does that make the code much more complex? we're trying not to add too many bytes, but i could see that being quite nice for some apps

    5. Merging this PR will improve performance by 3.63%

      :warning: Different runtime environments detected

      Some benchmarks with significant performance changes were compared across different runtime environments, which may affect the accuracy of the results.

      Open the report in CodSpeed to investigate

      ⚡ 1 improved benchmark ✅ 188 untouched benchmarks ⏩ 3 skipped benchmarks1

      Performance Changes

      ModeBenchmarkBASEHEADEfficiency
      ⚡Memorymem server error-paths unmatched (react)889.3 KB858.2 KB+3.63%

      Tip

      Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


      Comparing Tarek-kharsa:perf/hydrate-without-awaiting-component-chunks (da70ff3) with main (fe7f1fd) Open in CodSpeed

      Footnotes

      1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

    6. Moved to #8577 from my personal account. Same change, rebased on main.

Sign in to join this conversation.