Skip to content

Commit

Permalink
Handle hydration replaceState for static page with searchParams (#42744)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
timneutkens committed Nov 10, 2022
1 parent 276533d commit 9d30e77
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
10 changes: 6 additions & 4 deletions packages/next/client/components/app-router.tsx
Expand Up @@ -20,6 +20,7 @@ import {
ACTION_REFRESH,
ACTION_RESTORE,
ACTION_SERVER_PATCH,
createHrefFromUrl,
reducer,
} from './reducer'
import {
Expand Down Expand Up @@ -132,10 +133,11 @@ function Router({
pushRef: { pendingPush: false, mpaNavigation: false },
focusAndScrollRef: { apply: false },
canonicalUrl:
initialCanonicalUrl +
// 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 the useEffect further down.
(typeof window !== 'undefined' ? window.location.hash : ''),
// location.href 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.
typeof window !== 'undefined'
? createHrefFromUrl(new URL(window.location.href))
: initialCanonicalUrl,
}
}, [children, initialCanonicalUrl, initialTree])
const [
Expand Down
2 changes: 1 addition & 1 deletion packages/next/client/components/reducer.ts
Expand Up @@ -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
}

Expand Down
14 changes: 13 additions & 1 deletion test/e2e/app-dir/app-static.test.ts
Expand Up @@ -4,7 +4,7 @@ import { promisify } from 'util'
import path, { join } from 'path'
import { createNext, FileRef } from 'e2e-utils'
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'

const glob = promisify(globOrig)
Expand Down Expand Up @@ -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()
})
})
})

0 comments on commit 9d30e77

Please sign in to comment.