Repository navigation
fix(react-router): avoid Suspense above root documents #8055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7edb8b3
fix(react-router): avoid suspense above root documents
Sheraff 794368e
test(react-start): cover root document hydration
Sheraff a3d57df
test(react-router): remove indirect hydration assertion
Sheraff e7fcb00
perf(react-router): reduce suspense wrapper bundle size
Sheraff 00d71dd
test(react-start): isolate root hydration regression
Sheraff b4ce166
fix(react-router): preserve root suspense in CSR
Sheraff 0184d2f
refactor(react-router): simplify root suspense checks
Sheraff cc4dac6
fix(react-router): preserve functional ssr classification
Sheraff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/react-router': patch | ||
| --- | ||
|
|
||
| Avoid wrapping root components that may render the HTML document in a Suspense boundary during SSR and hydration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| node_modules | ||
| dist | ||
| test-results | ||
| playwright-report | ||
| port*.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| src/routeTree.gen.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "name": "tanstack-react-start-e2e-root-document-hydration", | ||
| "private": true, | ||
| "sideEffects": false, | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite dev --port 3000", | ||
| "dev:e2e": "vite dev --port $PORT", | ||
| "build": "vite build && tsc --noEmit", | ||
| "start": "pnpx srvx --prod -s ../client dist/server/server.js", | ||
| "test:e2e:dev": "MODE=dev playwright test --project=chromium", | ||
| "test:e2e:prod": "MODE=prod playwright test --project=chromium", | ||
| "test:e2e": "rm -rf port*.txt; pnpm run test:e2e:dev && pnpm run test:e2e:prod" | ||
| }, | ||
| "dependencies": { | ||
| "@tanstack/react-router": "workspace:*", | ||
| "@tanstack/react-start": "workspace:*", | ||
| "react": "^19.0.0", | ||
| "react-dom": "^19.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@playwright/test": "^1.61.0", | ||
| "@tanstack/router-e2e-utils": "workspace:*", | ||
| "@types/node": "^22.10.2", | ||
| "@types/react": "^19.0.8", | ||
| "@types/react-dom": "^19.0.3", | ||
| "@vitejs/plugin-react": "^6.0.1", | ||
| "srvx": "^0.11.9", | ||
| "@typescript/native": "npm:typescript@^7.0.2", | ||
| "typescript": "npm:@typescript/typescript6@^6.0.2", | ||
| "vite": "^8.0.14" | ||
| }, | ||
| "nx": { | ||
| "targets": { | ||
| "test:e2e": { | ||
| "parallelism": false | ||
| } | ||
| } | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
e2e/react-start/root-document-hydration/playwright.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { defineConfig, devices } from '@playwright/test' | ||
| import { getTestServerPort } from '@tanstack/router-e2e-utils' | ||
| import packageJson from './package.json' with { type: 'json' } | ||
|
|
||
| const isDev = process.env.MODE === 'dev' | ||
| const PORT = await getTestServerPort(packageJson.name) | ||
| const baseURL = `http://localhost:${PORT}` | ||
|
|
||
| export default defineConfig({ | ||
| testDir: './tests', | ||
| workers: 1, | ||
| reporter: [['line']], | ||
|
|
||
| use: { | ||
| baseURL, | ||
| }, | ||
|
|
||
| webServer: { | ||
| command: isDev ? 'pnpm dev:e2e' : `pnpm build && PORT=${PORT} pnpm start`, | ||
| url: baseURL, | ||
| reuseExistingServer: !process.env.CI, | ||
| stdout: 'pipe', | ||
| env: { | ||
| PORT: String(PORT), | ||
| }, | ||
| }, | ||
|
|
||
| projects: [ | ||
| { | ||
| name: 'chromium', | ||
| use: { | ||
| ...devices['Desktop Chrome'], | ||
| }, | ||
| }, | ||
| ], | ||
| }) |
68 changes: 68 additions & 0 deletions
68
e2e/react-start/root-document-hydration/src/routeTree.gen.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* eslint-disable */ | ||
|
|
||
| // @ts-nocheck | ||
|
|
||
| // noinspection JSUnusedGlobalSymbols | ||
|
|
||
| // This file was automatically generated by TanStack Router. | ||
| // You should NOT make any changes in this file as it will be overwritten. | ||
| // Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified. | ||
|
|
||
| import { Route as rootRouteImport } from './routes/__root' | ||
| import { Route as IndexRouteImport } from './routes/index' | ||
|
|
||
| const IndexRoute = IndexRouteImport.update({ | ||
| id: '/', | ||
| path: '/', | ||
| getParentRoute: () => rootRouteImport, | ||
| } as any) | ||
|
|
||
| export interface FileRoutesByFullPath { | ||
| '/': typeof IndexRoute | ||
| } | ||
| export interface FileRoutesByTo { | ||
| '/': typeof IndexRoute | ||
| } | ||
| export interface FileRoutesById { | ||
| __root__: typeof rootRouteImport | ||
| '/': typeof IndexRoute | ||
| } | ||
| export interface FileRouteTypes { | ||
| fileRoutesByFullPath: FileRoutesByFullPath | ||
| fullPaths: '/' | ||
| fileRoutesByTo: FileRoutesByTo | ||
| to: '/' | ||
| id: '__root__' | '/' | ||
| fileRoutesById: FileRoutesById | ||
| } | ||
| export interface RootRouteChildren { | ||
| IndexRoute: typeof IndexRoute | ||
| } | ||
|
|
||
| declare module '@tanstack/react-router' { | ||
| interface FileRoutesByPath { | ||
| '/': { | ||
| id: '/' | ||
| path: '/' | ||
| fullPath: '/' | ||
| preLoaderRoute: typeof IndexRouteImport | ||
| parentRoute: typeof rootRouteImport | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const rootRouteChildren: RootRouteChildren = { | ||
| IndexRoute: IndexRoute, | ||
| } | ||
| export const routeTree = rootRouteImport | ||
| ._addFileChildren(rootRouteChildren) | ||
| ._addFileTypes<FileRouteTypes>() | ||
|
|
||
| import type { getRouter } from './router.tsx' | ||
| import type { createStart } from '@tanstack/react-start' | ||
| declare module '@tanstack/react-start' { | ||
| interface Register { | ||
| ssr: true | ||
| router: Awaited<ReturnType<typeof getRouter>> | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { createRouter } from '@tanstack/react-router' | ||
| import { routeTree } from './routeTree.gen' | ||
|
|
||
| export function getRouter() { | ||
| return createRouter({ routeTree }) | ||
| } |
36 changes: 36 additions & 0 deletions
36
e2e/react-start/root-document-hydration/src/routes/__root.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { | ||
| HeadContent, | ||
| Outlet, | ||
| Scripts, | ||
| createRootRoute, | ||
| } from '@tanstack/react-router' | ||
| import { useEffect } from 'react' | ||
|
|
||
| export const Route = createRootRoute({ | ||
| pendingComponent: () => null, | ||
| component: RootComponent, | ||
| }) | ||
|
|
||
| function RootComponent() { | ||
| useEffect(() => { | ||
| document.documentElement.dataset.hydrated = 'true' | ||
| }, []) | ||
|
|
||
| return ( | ||
| <html> | ||
| <head> | ||
| <HeadContent /> | ||
| </head> | ||
| <body> | ||
| <div data-testid="ssr-node" /> | ||
| <script | ||
| dangerouslySetInnerHTML={{ | ||
| __html: `window.__ssrNode ??= document.currentScript.previousElementSibling`, | ||
| }} | ||
| /> | ||
| <Outlet /> | ||
| <Scripts /> | ||
| </body> | ||
| </html> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/')({ | ||
| component: () => <main>Root document hydration</main>, | ||
| }) |
47 changes: 47 additions & 0 deletions
47
e2e/react-start/root-document-hydration/tests/root-document-hydration.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { expect } from '@playwright/test' | ||
| import { test } from '@tanstack/router-e2e-utils' | ||
|
|
||
| test('a root pendingComponent preserves the SSR document during hydration', async ({ | ||
| page, | ||
| }) => { | ||
| await page.addInitScript(() => { | ||
| const state = { removed: false } | ||
| ;(window as any).__hydration = state | ||
|
|
||
| new MutationObserver((records) => { | ||
| const ssrNode = (window as any).__ssrNode as Node | undefined | ||
| if (!ssrNode) { | ||
| return | ||
| } | ||
|
|
||
| for (const record of records) { | ||
| for (const removedNode of record.removedNodes) { | ||
| if ( | ||
| removedNode === ssrNode || | ||
| (removedNode instanceof Element && removedNode.contains(ssrNode)) | ||
| ) { | ||
| state.removed = true | ||
| } | ||
| } | ||
| } | ||
| }).observe(document, { childList: true, subtree: true }) | ||
| }) | ||
|
|
||
| const response = await page.goto('/') | ||
| expect(response?.ok()).toBe(true) | ||
| await expect(page.locator('html')).toHaveAttribute('data-hydrated', 'true') | ||
|
|
||
| expect( | ||
| await page.evaluate(() => ({ | ||
| captured: !!(window as any).__ssrNode, | ||
| removed: (window as any).__hydration.removed, | ||
| replaced: | ||
| (window as any).__ssrNode !== | ||
| document.querySelector('[data-testid="ssr-node"]'), | ||
| })), | ||
| ).toEqual({ | ||
| captured: true, | ||
| removed: false, | ||
| replaced: false, | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "include": ["**/*.ts", "**/*.tsx"], | ||
| "compilerOptions": { | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "jsx": "react-jsx", | ||
| "module": "ESNext", | ||
| "moduleResolution": "Bundler", | ||
| "lib": ["DOM", "DOM.Iterable", "ES2022"], | ||
| "isolatedModules": true, | ||
| "resolveJsonModule": true, | ||
| "skipLibCheck": true, | ||
| "target": "ES2022", | ||
| "allowJs": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "noEmit": true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { defineConfig } from 'vite' | ||
| import { tanstackStart } from '@tanstack/react-start/plugin/vite' | ||
| import viteReact from '@vitejs/plugin-react' | ||
|
|
||
| export default defineConfig({ | ||
| resolve: { tsconfigPaths: true }, | ||
| server: { | ||
| port: 3000, | ||
| }, | ||
| plugins: [tanstackStart(), viteReact()], | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.