Skip to content

Commit

Permalink
fix(react-router): do not preload links with reloadDocument: true
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel committed Dec 17, 2024
1 parent 9559b9c commit f85edf2
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 64 deletions.
1 change: 1 addition & 0 deletions e2e/react-router/basic-file-based/src/routes/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function PostsComponent() {
params={{
postId: post.id,
}}
reloadDocument={true}
className="block py-1 text-blue-600 hover:opacity-75"
activeProps={{ className: 'font-bold underline' }}
>
Expand Down
12 changes: 7 additions & 5 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,12 @@ export function useLinkProps<
() => router.buildLocation(options as any),
[router, options, currentSearch],
)
const preload = React.useMemo(
() => userPreload ?? router.options.defaultPreload,
[router.options.defaultPreload, userPreload],
)
const preload = React.useMemo(() => {
if (options.reloadDocument) {
return false
}
return userPreload ?? router.options.defaultPreload
}, [router.options.defaultPreload, userPreload, options.reloadDocument])
const preloadDelay =
userPreloadDelay ?? router.options.defaultPreloadDelay ?? 0

Expand Down Expand Up @@ -768,7 +770,7 @@ export function useLinkProps<

// All is well? Navigate!
// N.B. we don't call `router.commitLocation(next) here because we want to run `validateSearch` before committing
router.buildAndCommitLocation({
return router.navigate({
...options,
replace,
resetScroll,
Expand Down
176 changes: 117 additions & 59 deletions packages/react-router/tests/link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ beforeEach(() => {
disconnect: ioDisconnectMock,
})
vi.stubGlobal('IntersectionObserver', io)
window.history.replaceState(null, 'root', '/')
})

afterEach(() => {
vi.resetAllMocks()
window.history.replaceState(null, 'root', '/')
cleanup()
})

Expand Down Expand Up @@ -1194,77 +1194,70 @@ describe('Link', () => {
expect(notFoundComponent).not.toBeCalled()
})

test.each([{ reloadDocument: true }, { reloadDocument: false }])(
'when navigating to /posts with params',
async ({ reloadDocument }) => {
const rootRoute = createRootRoute()
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return (
<>
<h1>Index</h1>
<Link
reloadDocument={reloadDocument}
to="/posts/$postId"
params={{ postId: 'id1' }}
>
To first post
</Link>
</>
)
},
})

const PostsComponent = () => {
test('when navigating to /posts with params', async () => {
const rootRoute = createRootRoute()
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return (
<>
<h1>Posts</h1>
<Link to="/">Index</Link>
<Outlet />
<h1>Index</h1>
<Link to="/posts/$postId" params={{ postId: 'id1' }}>
To first post
</Link>
</>
)
}
},
})

const postsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'posts',
component: PostsComponent,
})
const PostsComponent = () => {
return (
<>
<h1>Posts</h1>
<Link to="/">Index</Link>
<Outlet />
</>
)
}

const PostComponent = () => {
const params = useParams({ strict: false })
return <span>Params: {params.postId}</span>
}
const postsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'posts',
component: PostsComponent,
})

const postRoute = createRoute({
getParentRoute: () => postsRoute,
path: '$postId',
component: PostComponent,
})
const PostComponent = () => {
const params = useParams({ strict: false })
return <span>Params: {params.postId}</span>
}

const router = createRouter({
routeTree: rootRoute.addChildren([
indexRoute,
postsRoute.addChildren([postRoute]),
]),
})
const postRoute = createRoute({
getParentRoute: () => postsRoute,
path: '$postId',
component: PostComponent,
})

render(<RouterProvider router={router} />)
const router = createRouter({
routeTree: rootRoute.addChildren([
indexRoute,
postsRoute.addChildren([postRoute]),
]),
})

const postLink = await screen.findByRole('link', {
name: 'To first post',
})
render(<RouterProvider router={router} />)

expect(postLink).toHaveAttribute('href', '/posts/id1')
const postLink = await screen.findByRole('link', {
name: 'To first post',
})

fireEvent.click(postLink)
expect(postLink).toHaveAttribute('href', '/posts/id1')

const paramText = await screen.findByText('Params: id1')
expect(paramText).toBeInTheDocument()
},
)
fireEvent.click(postLink)

const paramText = await screen.findByText('Params: id1')
expect(paramText).toBeInTheDocument()
})

test('when navigating from /posts to ./$postId', async () => {
const rootRoute = createRootRoute()
Expand Down Expand Up @@ -4352,4 +4345,69 @@ describe('search middleware', () => {
expect(postsLinkSearch.get('root')).toBe('abc')
expect(postsLink).toHaveAttribute('data-status', 'active')
})

describe('reloadDocument', () => {
test('link to /posts with params', async () => {
const rootRoute = createRootRoute()
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return (
<>
<h1>Index</h1>
<Link
to="/posts/$postId"
params={{ postId: 'id1' }}
reloadDocument={true}
data-testid="link-to-post-1"
>
To first post
</Link>
</>
)
},
})

const PostsComponent = () => {
return (
<>
<h1>Posts</h1>
<Link to="/">Index</Link>
<Outlet />
</>
)
}

const postsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'posts',
component: PostsComponent,
})

const PostComponent = () => {
const params = useParams({ strict: false })
return <span>Params: {params.postId}</span>
}

const postRoute = createRoute({
getParentRoute: () => postsRoute,
path: '$postId',
component: PostComponent,
})

const router = createRouter({
routeTree: rootRoute.addChildren([
indexRoute,
postsRoute.addChildren([postRoute]),
]),
})

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

const postLink = await screen.findByTestId('link-to-post-1')

expect(postLink).toHaveAttribute('href', '/posts/id1')
})
})
})

0 comments on commit f85edf2

Please sign in to comment.