Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/react-router/tests/redirect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,73 @@ describe('redirect', () => {
expect(nestedFooLoaderMock).toHaveBeenCalled()
})

test('when `redirect` is thrown during preload', async () => {
let signedIn = false // Simulate user authentication state
const beforeRedirectMock = vi.fn()
const afterRedirectMock = vi.fn()

const rootRoute = createRootRoute({})
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return (
<div>
<h1>Index page</h1>
<Link to="/protected">link to protected</Link>
</div>
)
},
})
const protectedRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/protected',
beforeLoad: () => {
beforeRedirectMock()
if (!signedIn) throw redirect({ to: '/login' })
afterRedirectMock()
},
component: () => <div>Protected page</div>,
})
const signInRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/login',
component: () => <div>Sign In page</div>,
})
const routeTree = rootRoute.addChildren([
signInRoute,
protectedRoute,
indexRoute,
])
const router = createRouter({
routeTree,
history,
defaultPreload: 'intent',
})

render(<RouterProvider router={router} />)

const linkToProtected = await screen.findByText('link to protected')
expect(linkToProtected).toBeInTheDocument()

// preload
fireEvent.focus(linkToProtected)
await sleep(WAIT_TIME)

// sign-in
signedIn = true

// navigate
fireEvent.click(linkToProtected)

const protectedElement = await screen.findByText('Protected page')
expect(protectedElement).toBeInTheDocument()
expect(router.state.location.href).toBe('/protected')
expect(window.location.pathname).toBe('/protected')
expect(beforeRedirectMock).toHaveBeenCalledTimes(2)
expect(afterRedirectMock).toHaveBeenCalledTimes(1)
})

test('when `redirect` is thrown in `loader` after `router.invalidate()`', async () => {
let shouldRedirect = false

Expand Down
17 changes: 9 additions & 8 deletions packages/router-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ export type SubscribeFn = <TType extends keyof RouterEvents>(
export interface MatchRoutesOpts {
preload?: boolean
throwOnError?: boolean
/** @internal */
_buildLocation?: boolean
dest?: BuildNextOptions
}
Expand Down Expand Up @@ -909,14 +910,14 @@ export class RouterCore<

if (!this.__store) {
this.__store = new Store(getInitialRouterState(this.latestLocation), {
onUpdate: () => {
this.__store.state = {
...this.state,
cachedMatches: this.state.cachedMatches.filter(
(d) => !['redirected'].includes(d.status),
),
}
},
// onUpdate: () => {
// this.__store.state = {
// ...this.state,
// cachedMatches: this.state.cachedMatches.filter(
// (d) => !['redirected'].includes(d.status),
// ),
// }
// },
})

setupScrollRestoration(this)
Expand Down