-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(router-core): preserve retained beforeLoad context while pending #8067
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
Closed
ulrichstark
wants to merge
2
commits into
TanStack:main
from
ulrichstark:fix(router-core)--preserve-retained-beforeLoad-context-while-pending
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
153 changes: 153 additions & 0 deletions
153
packages/react-router/tests/retained-ancestor-beforeload-context.test.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,153 @@ | ||
| import { cleanup, render, screen, waitFor } from '@testing-library/react' | ||
| import { afterEach, expect, test } from 'vitest' | ||
| import { createControlledPromise } from '@tanstack/router-core' | ||
| import { | ||
| Outlet, | ||
| RouterProvider, | ||
| createMemoryHistory, | ||
| createRootRoute, | ||
| createRoute, | ||
| createRouter, | ||
| } from '../src' | ||
|
|
||
| afterEach(cleanup) | ||
|
|
||
| // While a child route shows its pending fallback, ancestor routes stay mounted | ||
| // and must keep the context their own `beforeLoad` produced. Losing it is not | ||
| // cosmetic: ancestors that reach into a nested context value - the usual shape | ||
| // for an auth/session context - throw while they are still on screen. | ||
| test('retained ancestor keeps its beforeLoad context while a child route is pending', async () => { | ||
| const childLoader = createControlledPromise<void>() | ||
| // Errors thrown while rendering the mounted ancestor. Caught here only so the | ||
| // failure surfaces as an assertion instead of taking out the route's error | ||
| // boundary; in an app this is an uncaught render crash. | ||
| const renderErrors: Array<string> = [] | ||
|
|
||
| const rootRoute = createRootRoute({ | ||
| beforeLoad: () => ({ auth: { user: 'ada' } }), | ||
| component: function RootLayout() { | ||
| const { auth } = rootRoute.useRouteContext() | ||
| let user | ||
| try { | ||
| user = auth.user | ||
| } catch (error) { | ||
| renderErrors.push(String(error)) | ||
| } | ||
| return ( | ||
| <> | ||
| <div>{`user:${user ?? 'MISSING'}`}</div> | ||
| <Outlet /> | ||
| </> | ||
| ) | ||
| }, | ||
| }) | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| component: () => <div>Home</div>, | ||
| }) | ||
| const childRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/child', | ||
| pendingMs: 0, | ||
| pendingComponent: () => <div>Pending</div>, | ||
| loader: () => childLoader, | ||
| component: () => <div>Child</div>, | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute, childRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/'] }), | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
| await screen.findByText('Home') | ||
| await waitFor(() => expect(router.state.status).toBe('idle')) | ||
|
|
||
| const navigation = router.navigate({ to: '/child' }) | ||
| await screen.findByText('Pending') | ||
|
|
||
| expect(renderErrors).toEqual([]) | ||
| expect(screen.getByText('user:ada')).toBeInTheDocument() | ||
|
|
||
| childLoader.resolve() | ||
| await navigation | ||
| }) | ||
|
|
||
| // Contextualization walks the lane serially, so an ancestor whose `beforeLoad` | ||
| // is still in flight parks the walk above every deeper ancestor. The pending | ||
| // fallback is published from that parked state, so the ancestors the walk has | ||
| // not reached yet must already be presentable. | ||
| test('retained ancestor keeps its beforeLoad context while an ancestor above it is pending', async () => { | ||
| const childLoader = createControlledPromise<void>() | ||
| const rootBeforeLoad = createControlledPromise<void>() | ||
| let rootResolved = false | ||
| let rootBeforeLoadStarted = false | ||
| const renderErrors: Array<string> = [] | ||
|
|
||
| const rootRoute = createRootRoute({ | ||
| beforeLoad: async () => { | ||
| // Only the navigation blocks; the initial load must settle normally. | ||
| if (rootResolved) { | ||
| rootBeforeLoadStarted = true | ||
| await rootBeforeLoad | ||
| } | ||
| rootResolved = true | ||
| return { session: 'live' } | ||
| }, | ||
| component: () => <Outlet />, | ||
| }) | ||
| const dashRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/dash', | ||
| beforeLoad: () => ({ auth: { user: 'ada' } }), | ||
| component: function DashLayout() { | ||
| const { auth } = dashRoute.useRouteContext() | ||
| let user | ||
| try { | ||
| user = auth.user | ||
| } catch (error) { | ||
| renderErrors.push(String(error)) | ||
| } | ||
| return ( | ||
| <> | ||
| <div>{`user:${user ?? 'MISSING'}`}</div> | ||
| <Outlet /> | ||
| </> | ||
| ) | ||
| }, | ||
| }) | ||
| const overviewRoute = createRoute({ | ||
| getParentRoute: () => dashRoute, | ||
| path: 'overview', | ||
| component: () => <div>Overview</div>, | ||
| }) | ||
| const detailRoute = createRoute({ | ||
| getParentRoute: () => dashRoute, | ||
| path: 'detail', | ||
| pendingMs: 0, | ||
| pendingComponent: () => <div>Pending</div>, | ||
| loader: () => childLoader, | ||
| component: () => <div>Detail</div>, | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([ | ||
| dashRoute.addChildren([overviewRoute, detailRoute]), | ||
| ]), | ||
| history: createMemoryHistory({ initialEntries: ['/dash/overview'] }), | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
| await screen.findByText('Overview') | ||
| await waitFor(() => expect(router.state.status).toBe('idle')) | ||
|
|
||
| const navigation = router.navigate({ to: '/dash/detail' }) | ||
| await screen.findByText('Pending') | ||
|
|
||
| expect(rootBeforeLoadStarted).toBe(true) | ||
| expect(renderErrors).toEqual([]) | ||
| expect(screen.getByText('user:ada')).toBeInTheDocument() | ||
|
|
||
| rootBeforeLoad.resolve() | ||
| childLoader.resolve() | ||
| await navigation | ||
| }) | ||
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.
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.