Skip to content

Commit 9d30e77

Browse files
authored
Handle hydration replaceState for static page with searchParams (#42744)
Ensures the searchParams do not get removed when the page is static. Fixes #42697 <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change that you're making: --> ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
1 parent 276533d commit 9d30e77

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

packages/next/client/components/app-router.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
ACTION_REFRESH,
2121
ACTION_RESTORE,
2222
ACTION_SERVER_PATCH,
23+
createHrefFromUrl,
2324
reducer,
2425
} from './reducer'
2526
import {
@@ -132,10 +133,11 @@ function Router({
132133
pushRef: { pendingPush: false, mpaNavigation: false },
133134
focusAndScrollRef: { apply: false },
134135
canonicalUrl:
135-
initialCanonicalUrl +
136-
// Hash is read as the initial value for canonicalUrl in the browser
137-
// This is safe to do as canonicalUrl can't be rendered, it's only used to control the history updates the useEffect further down.
138-
(typeof window !== 'undefined' ? window.location.hash : ''),
136+
// location.href is read as the initial value for canonicalUrl in the browser
137+
// This is safe to do as canonicalUrl can't be rendered, it's only used to control the history updates in the useEffect further down in this file.
138+
typeof window !== 'undefined'
139+
? createHrefFromUrl(new URL(window.location.href))
140+
: initialCanonicalUrl,
139141
}
140142
}, [children, initialCanonicalUrl, initialTree])
141143
const [

packages/next/client/components/reducer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function readRecordValue(thenable: any) {
4343
}
4444
}
4545

46-
function createHrefFromUrl(url: URL): string {
46+
export function createHrefFromUrl(url: URL): string {
4747
return url.pathname + url.search + url.hash
4848
}
4949

test/e2e/app-dir/app-static.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { promisify } from 'util'
44
import path, { join } from 'path'
55
import { createNext, FileRef } from 'e2e-utils'
66
import { NextInstance } from 'test/lib/next-modes/base'
7-
import { check, fetchViaHTTP, normalizeRegEx } from 'next-test-utils'
7+
import { check, fetchViaHTTP, normalizeRegEx, waitFor } from 'next-test-utils'
88
import webdriver from 'next-webdriver'
99

1010
const glob = promisify(globOrig)
@@ -452,5 +452,17 @@ describe('app-dir static/dynamic handling', () => {
452452
)
453453
})
454454
}
455+
456+
it('should keep querystring on static page', async () => {
457+
const browser = await webdriver(next.url, '/blog/tim?message=hello-world')
458+
const checkUrl = async () =>
459+
expect(await browser.url()).toBe(
460+
next.url + '/blog/tim?message=hello-world'
461+
)
462+
463+
checkUrl()
464+
await waitFor(1000)
465+
checkUrl()
466+
})
455467
})
456468
})

0 commit comments

Comments
 (0)