Skip to content

Commit 60932dd

Browse files
fix: handle stale redirected matches during view transitions
1 parent 9797eac commit 60932dd

23 files changed

Lines changed: 566 additions & 83 deletions

File tree

‎.changeset/bright-cars-lie.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@tanstack/react-router': patch
3+
---
4+
5+
Fix a client-side crash when a root `beforeLoad` redirect races with pending UI and a lazy target route while `defaultViewTransition` is enabled.
6+
7+
React now handles stale redirected matches more safely during the transition, and a dedicated `e2e/react-router/issue-7120` fixture covers this regression.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Issue 7120</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<script type="module" src="/src/main.tsx"></script>
11+
</body>
12+
</html>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "tanstack-router-e2e-react-issue-7120",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"dev": "vite --port 3000",
7+
"dev:e2e": "vite",
8+
"build": "vite build && tsc --noEmit",
9+
"preview": "vite preview",
10+
"start": "vite",
11+
"test:e2e": "rm -rf port*.txt; playwright test --project=chromium"
12+
},
13+
"dependencies": {
14+
"@tailwindcss/vite": "^4.2.2",
15+
"@tanstack/react-router": "workspace:^",
16+
"react": "^19.0.0",
17+
"react-dom": "^19.0.0",
18+
"redaxios": "^0.5.1",
19+
"tailwindcss": "^4.2.2"
20+
},
21+
"devDependencies": {
22+
"@playwright/test": "^1.50.1",
23+
"@tanstack/router-e2e-utils": "workspace:^",
24+
"@types/react": "^19.0.8",
25+
"@types/react-dom": "^19.0.3",
26+
"@vitejs/plugin-react": "^6.0.1",
27+
"vite": "^8.0.0"
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { defineConfig, devices } from '@playwright/test'
2+
import {
3+
getDummyServerPort,
4+
getTestServerPort,
5+
} from '@tanstack/router-e2e-utils'
6+
import packageJson from './package.json' with { type: 'json' }
7+
8+
const PORT = await getTestServerPort(packageJson.name)
9+
const EXTERNAL_PORT = await getDummyServerPort(packageJson.name)
10+
const baseURL = `http://localhost:${PORT}`
11+
12+
export default defineConfig({
13+
testDir: './tests',
14+
workers: 1,
15+
reporter: [['line']],
16+
globalSetup: './tests/setup/global.setup.ts',
17+
globalTeardown: './tests/setup/global.teardown.ts',
18+
use: {
19+
baseURL,
20+
},
21+
webServer: {
22+
command: `VITE_NODE_ENV="test" VITE_SERVER_PORT=${PORT} VITE_EXTERNAL_PORT=${EXTERNAL_PORT} pnpm build && pnpm preview --port ${PORT}`,
23+
url: baseURL,
24+
reuseExistingServer: !process.env.CI,
25+
stdout: 'pipe',
26+
},
27+
projects: [
28+
{
29+
name: 'chromium',
30+
use: { ...devices['Desktop Chrome'] },
31+
},
32+
],
33+
})
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import ReactDOM from 'react-dom/client'
2+
import {
3+
Outlet,
4+
RouterProvider,
5+
createRootRoute,
6+
createRoute,
7+
createRouter,
8+
redirect,
9+
} from '@tanstack/react-router'
10+
import { fetchPosts } from './posts'
11+
import './styles.css'
12+
13+
const rootRoute = createRootRoute({
14+
component: RootComponent,
15+
pendingMs: 0,
16+
pendingComponent: () => <div data-testid="root-pending">loading</div>,
17+
beforeLoad: async ({ matches }) => {
18+
if (matches.find((match) => match.routeId === '/posts')) {
19+
return
20+
}
21+
22+
await new Promise((resolve) => setTimeout(resolve, 1000))
23+
throw redirect({ to: '/posts' })
24+
},
25+
})
26+
27+
function RootComponent() {
28+
return <Outlet />
29+
}
30+
31+
const indexRoute = createRoute({
32+
getParentRoute: () => rootRoute,
33+
path: '/',
34+
component: () => <div>Home</div>,
35+
})
36+
37+
const postsRoute = createRoute({
38+
getParentRoute: () => rootRoute,
39+
path: 'posts',
40+
loader: async () => {
41+
await new Promise((resolve) => setTimeout(resolve, 10))
42+
return fetchPosts()
43+
},
44+
}).lazy(() => import('./posts.lazy').then((d) => d.Route))
45+
46+
const routeTree = rootRoute.addChildren([indexRoute, postsRoute])
47+
48+
const router = createRouter({
49+
routeTree,
50+
defaultViewTransition: true,
51+
})
52+
53+
declare module '@tanstack/react-router' {
54+
interface Register {
55+
router: typeof router
56+
}
57+
}
58+
59+
const rootElement = document.getElementById('app')!
60+
61+
if (!rootElement.innerHTML) {
62+
const root = ReactDOM.createRoot(rootElement)
63+
root.render(<RouterProvider router={router} />)
64+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Link, createLazyRoute } from '@tanstack/react-router'
2+
3+
export const Route = createLazyRoute('/posts')({
4+
component: PostsComponent,
5+
})
6+
7+
function PostsComponent() {
8+
const posts = Route.useLoaderData()
9+
10+
return (
11+
<div className="p-2">
12+
<ul className="list-disc pl-4">
13+
{posts.map((post) => {
14+
return (
15+
<li key={post.id} className="whitespace-nowrap">
16+
<Link
17+
to="/posts"
18+
className="block py-1 px-2 text-blue-600 hover:opacity-75"
19+
>
20+
<div>{post.title.substring(0, 20)}</div>
21+
</Link>
22+
</li>
23+
)
24+
})}
25+
</ul>
26+
</div>
27+
)
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import axios from 'redaxios'
2+
3+
type PostType = {
4+
id: string
5+
title: string
6+
body: string
7+
}
8+
9+
let queryURL = 'https://jsonplaceholder.typicode.com'
10+
11+
if (import.meta.env.VITE_NODE_ENV === 'test') {
12+
queryURL = `http://localhost:${import.meta.env.VITE_EXTERNAL_PORT}`
13+
}
14+
15+
export const fetchPosts = async () => {
16+
return axios
17+
.get<Array<PostType>>(`${queryURL}/posts`)
18+
.then((r) => r.data.slice(0, 10))
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@import 'tailwindcss' source('../');
2+
3+
@layer base {
4+
*,
5+
::after,
6+
::before,
7+
::backdrop,
8+
::file-selector-button {
9+
border-color: var(--color-gray-200, currentcolor);
10+
}
11+
}
12+
13+
html {
14+
color-scheme: light dark;
15+
}
16+
17+
* {
18+
@apply border-gray-200 dark:border-gray-800;
19+
}
20+
21+
body {
22+
@apply bg-gray-50 text-gray-950 dark:bg-gray-900 dark:text-gray-200;
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect, test } from '@playwright/test'
2+
3+
test('root beforeLoad redirect does not blank when pending UI and view transitions are enabled', async ({
4+
page,
5+
}) => {
6+
const pageErrors: Array<string> = []
7+
8+
page.on('pageerror', (error) => {
9+
pageErrors.push(error.message)
10+
})
11+
12+
await page.goto('/')
13+
14+
await expect(page).toHaveURL(/\/posts$/)
15+
await expect(page.getByText('sunt aut facere repe')).toBeVisible()
16+
await expect(page.getByTestId('root-pending')).not.toBeVisible()
17+
expect(pageErrors).toEqual([])
18+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { e2eStartDummyServer } from '@tanstack/router-e2e-utils'
2+
import packageJson from '../../package.json' with { type: 'json' }
3+
4+
export default async function setup() {
5+
await e2eStartDummyServer(packageJson.name)
6+
}

0 commit comments

Comments
 (0)