SPA mode: hydrating the prerendered shell throws React #418 at any address whose route content is available on the first pass
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- playwright, react, typescript, vite
調査の方向性
Reproduce the issue with the listed build commands and a static-host SPA fallback, checking / and /unknown in e2e/react-start/spa-mode. Read packages/router-core/src/load-server.ts and packages/router-core/src/ssr/ssr-server.ts, then trace the MatchView hydration path described in the report. Done means the built shell hydrates cleanly at all listed addresses and e2e coverage catches no uncaught errors.
索引モデルが issue の本文から書いたものです。
説明
Which project does this relate to?
Start
Describe the bug
In SPA mode, the prerendered shell throws Minified React error #418 ("Hydration failed because the server rendered HTML didn't match the client") whenever the browser can render route content on its first pass. React then discards the server HTML and re-renders the whole document.
The shell leaves the route area empty inside a resolved suspense boundary:
<div id="app"><!--$--><!--$--><!--/$--><!--/$--></div>
Hydration therefore only lines up when the client's first pass also renders nothing, which happens by accident whenever the matched route's chunk has not loaded yet. It does not line up when a component is already available synchronously, and two ordinary cases make it available:
- an address that matches no route -
notFoundComponentis declared on the root route, so it sits in the main bundle and renders immediately; - the address the shell was prerendered for - its route chunk is in the shell's
modulepreloadlist.
This only shows up in the built output served the way a static host serves a SPA (unknown addresses answered with the shell). vite dev never shows it, because it renders a shell per request. vite preview never shows it either, because it answers an unknown address with a 404 document instead of the shell.
Your Example Website or App
This repository's own e2e/react-start/spa-mode app reproduces it, no changes needed.
Steps to Reproduce the Bug or Issue
Using this repo at ac223be:
pnpm install
pnpm nx run @tanstack/react-start:build
pnpm nx run @tanstack/react-router-devtools:build
cd e2e/react-start/spa-mode && pnpm exec vite build
Then serve dist/client the way a static host serves a SPA: an existing file wins, a directory's index.html wins, anything else falls back to /index.html with a 200 (nginx try_files $uri $uri/index.html /index.html). Open each address and watch the console:
| Address | Served | Console |
|---|---|---|
/ |
the shell | React #418 |
/posts/1 |
its own prerendered page | clean |
/unknown |
the shell (SPA fallback) | React #418 |
npx serve dist/client, which package.json currently uses for the e2e run, answers /unknown with its own 404 page, so the suite never loads the shell at an address it was not prerendered for. / is loaded, and it throws, but nothing in the suite listens for uncaught errors.
A minimal app outside this repo, on the published @tanstack/[email protected] / @tanstack/[email protected] with React 19.3.0, behaves the same:
vite.config.ts
import {defineConfig} from 'vite'
import {tanstackStart} from '@tanstack/react-start/plugin/vite'
import viteReact from '@vitejs/plugin-react'
export default defineConfig({
plugins: [
tanstackStart({spa: {enabled: true, prerender: {outputPath: '/index.html'}}}),
viteReact(),
],
})
src/routes/__root.tsx
import {HeadContent, Outlet, Scripts, createRootRoute} from '@tanstack/react-router'
import type {ReactNode} from 'react'
export const Route = createRootRoute({
ssr: false,
shellComponent: RootDocument,
component: RootComponent,
notFoundComponent: NotFound,
})
function RootDocument({children}: {children: ReactNode}) {
return (
<html lang="en">
<head>
<HeadContent />
</head>
<body>
<div id="app">{children}</div>
<Scripts />
</body>
</html>
)
}
function RootComponent() {
return <Outlet />
}
function NotFound() {
return <h1>Not found</h1>
}
with src/routes/index.tsx and src/routes/about.tsx rendering an <h1>. Built and served with the same fallback rule: / throws #418 (its chunk is preloaded by the shell), /no-such-page throws #418 (the not-found component is in the main bundle), /about is clean (its chunk has not loaded on the first pass). Replacing notFoundComponent with a splat route src/routes/$.tsx that renders the same <h1> makes /no-such-page clean, because the page then arrives in its own chunk like any other route.
Expected behavior
Hydrating the shell at any address is clean, and the client fills the empty route area as an ordinary update.
Screenshots or Videos
No response
Platform
- OS: macOS 15
- Browser: Chromium (Playwright 1.5x)
- Version:
@tanstack/react-router1.170.36 and this repo atac223be; React 19.3.0 / 19.2.3; Vite 8
Additional context
What I could establish while tracking this down, in case it saves someone time:
- It is not the not-found page's content. With
notFoundComponentstubbed toreturn nullthe error still fires, so what differs is the shape of the tree, not what is drawn in it. - Deferring that component does not help either: neither
lazyRouteComponent(() => import('./NotFound'))nor wrapping it in the router's ownClientOnlychanges anything. - In shell mode
resolveSsr(packages/router-core/src/load-server.ts) returnsroute.id === rootRouteId, so the root match is dehydrated withssr: trueand it is the only match dehydrated (packages/router-core/src/ssr/ssr-server.ts). On the client nothing then marks the subtree below the root as "not rendered on the server", soMatchViewtakes the plain path rather than theClientOnlypath that produced the empty HTML. - I tried carrying an
isShellflag in the dehydrated payload and settingssr: falseon the client for the matches below the dehydrated prefix. It did not fix it, which I read as the mismatch being the boundary structure itself rather than the ssr flag: the shell renders no child match at all (only the root is inrouter.stores.matches, so the root's<Outlet />returnsnull), while the hydrating client renders<Suspense>plus a childMatch. I did not take that further because the hydration path is intricate enough that a guess seemed likely to cost you more time than it saves.
Workaround for anyone hitting the not-found half of this: a catch-all route (src/routes/$.tsx) rendering the same page, so an unknown address matches an ordinary code-split route.
I have a PR adding the missing e2e coverage for this (serving the built app with a SPA fallback, and asserting nothing uncaught on load) that I can link once it is up.
- 主要言語
- TypeScript
- スター
- 15.3k
- フォーク
- 1.9k
- 平均マージ
- 1日 5時間
- マージ済み PR(30日)
- 46
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
TanStack/router のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
Masked navigation drops the first character of the hash each time (__tempLocation hash missing '#')対応中かも @hassanabrar202 が 8 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
router-generator: lazy-load Prettier (costs ~300–450ms per build even when formatting is off)対応中かも @Sheraff が 27 日前に担当しました。 オープンpackage: router-generator performance
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
router-generator: sibling `physical()` subtrees in a virtual route config generate duplicate identifiers対応中かも @Sheraff が 27 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
docs(router): clarify static title fallback in SPA mode対応中かも @Rohan-Dedhia が 18 日前に担当しました。 オープン
難易度 1/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
TanStack/router の issue をすべて見る
似ている issue
-
[Docs] CONTRIBUTING points the Backend Remake milestone at a branch that does not exist対応中かも @franpts2 が今日担当しました。 オープンdocs
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
メンテナーはふだん 3 日以内に返信
-
ready-for-agent
難易度 2/5 半日 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
cloudflare/nimbus#423 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
[BUG]: Shared links drop the configured basePath on GitHub Pages対応中かも @anshulchikhale30-p が今日担当しました。 オープンbug
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
StabilityNexus/IdentityTokens-EVM-Frontend#182 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信