Skip to content
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

fix(next): initial prefetch cache not set properly with different search params #65977

Merged
merged 6 commits into from
May 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ export function createInitialRouterState({
// Seed the prefetch cache with this page's data.
// This is to prevent needlessly re-prefetching a page that is already reusable,
// and will avoid triggering a loading state/data fetch stall when navigating back to the page.
const url = new URL(location.pathname, location.origin)
const url = new URL(
`${location.pathname}${location.search}`,
location.origin
)

const initialFlightData: FlightData = [['', initialTree, null, null]]
createPrefetchCacheEntryForInitialLoad({
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/prefetch-searchparam/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
11 changes: 11 additions & 0 deletions test/e2e/app-dir/prefetch-searchparam/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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>
</>
)
}
6 changes: 6 additions & 0 deletions test/e2e/app-dir/prefetch-searchparam/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
25 changes: 25 additions & 0 deletions test/e2e/app-dir/prefetch-searchparam/prefetch-searchparam.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'

describe('prefetch-searchparam', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should set prefetch cache properly on different search params', async () => {
// load WITH search param
const browser = await next.browser('/?q=foo')
expect(await browser.elementByCss('p').text()).toBe('{"q":"foo"}')

// navigate to different search param, should update the search param
await browser.elementByCss('[href="/?q=bar"]').click()
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('{"q":"bar"}')
})

// navigate to home, should clear the searchParams value
await browser.elementByCss('[href="/"]').click()
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('{}')
})
})
})
Loading