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

Handle hydration replaceState for static page with searchParams #42744

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})
})
})