Unverified Commit 9d30e777 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

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)
parent 276533d0
...@@ -20,6 +20,7 @@ import { ...@@ -20,6 +20,7 @@ import {
ACTION_REFRESH, ACTION_REFRESH,
ACTION_RESTORE, ACTION_RESTORE,
ACTION_SERVER_PATCH, ACTION_SERVER_PATCH,
createHrefFromUrl,
reducer, reducer,
} from './reducer' } from './reducer'
import { import {
...@@ -132,10 +133,11 @@ function Router({ ...@@ -132,10 +133,11 @@ function Router({
pushRef: { pendingPush: false, mpaNavigation: false }, pushRef: { pendingPush: false, mpaNavigation: false },
focusAndScrollRef: { apply: false }, focusAndScrollRef: { apply: false },
canonicalUrl: canonicalUrl:
initialCanonicalUrl + // location.href is read as the initial value for canonicalUrl in the browser
// Hash is read as the initial value for canonicalUrl in the browser // 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.
// This is safe to do as canonicalUrl can't be rendered, it's only used to control the history updates the useEffect further down. typeof window !== 'undefined'
(typeof window !== 'undefined' ? window.location.hash : ''), ? createHrefFromUrl(new URL(window.location.href))
: initialCanonicalUrl,
} }
}, [children, initialCanonicalUrl, initialTree]) }, [children, initialCanonicalUrl, initialTree])
const [ const [
......
...@@ -43,7 +43,7 @@ function readRecordValue(thenable: any) { ...@@ -43,7 +43,7 @@ function readRecordValue(thenable: any) {
} }
} }
function createHrefFromUrl(url: URL): string { export function createHrefFromUrl(url: URL): string {
return url.pathname + url.search + url.hash return url.pathname + url.search + url.hash
} }
......
...@@ -4,7 +4,7 @@ import { promisify } from 'util' ...@@ -4,7 +4,7 @@ import { promisify } from 'util'
import path, { join } from 'path' import path, { join } from 'path'
import { createNext, FileRef } from 'e2e-utils' import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' import { NextInstance } from 'test/lib/next-modes/base'
import { check, fetchViaHTTP, normalizeRegEx } from 'next-test-utils' import { check, fetchViaHTTP, normalizeRegEx, waitFor } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
const glob = promisify(globOrig) const glob = promisify(globOrig)
...@@ -452,5 +452,17 @@ describe('app-dir static/dynamic handling', () => { ...@@ -452,5 +452,17 @@ describe('app-dir static/dynamic handling', () => {
) )
}) })
} }
it('should keep querystring on static page', async () => {
const browser = await webdriver(next.url, '/blog/tim?message=hello-world')
const checkUrl = async () =>
expect(await browser.url()).toBe(
next.url + '/blog/tim?message=hello-world'
)
checkUrl()
await waitFor(1000)
checkUrl()
})
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment