rsnext/test/e2e/app-dir/prefetch-searchparam/app/page.tsx
Jiwon Choi 79c934aaec
fix(next): initial prefetch cache not set properly with different search params (#65977)
cc @icyJoseph @ztanner

NOTE: The canary release
[`v14.1.1-canary.51`](https://github.com/vercel/next.js/releases/tag/v14.1.1-canary.51)
and below work as expected.

### Why?

Introduced from #61535, the initial prefetch cache is set based on the
`location.pathname`.
When a page is loaded WITH the search param, the cache key does not
contain information of the search param.

The issue is when on a dynamic page reading the `searchParams` value,
the value doesn't change if navigated as:

```
/?q=foo --> /
```

The prefetch cache hits, not re-rendering, and the `searchParams` value
is not passed properly.

### How?

For the prefetch cache, add the `location.search` as well.

Since `createPrefetchCacheKey` uses
[`createHrefFromUrl`](https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/router-reducer/create-href-from-url.ts)
which includes `location.search`, I'm expecting the change won't affect
current cache key behavior.

Fixes #64170
Fixes #65030

---------

Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com>
2024-05-20 15:44:03 +00:00

11 lines
250 B
TypeScript

import Link from 'next/link'
export default function Page({ searchParams }: { searchParams: any }) {
return (
<>
<Link href="/">/</Link>
<Link href="/?q=bar">/?q=bar</Link>
<p>{JSON.stringify(searchParams)}</p>
</>
)
}