router: consider href when detecting hash-only changes; add integrati… #85036
+123
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
href
(the internal url) has different query/search params between calls, butas
contains the same path + hash (e.g....#hash
).onlyAHashChange
now accepts an optionalurl
(href) and, when provided, compares the href's pathname+search with the previous asPath so changes in the href's query are considered real navigations.Router.change()
now passesurl
intoonlyAHashChange(...)
.getInitialProps
is re-run whenhref
's query changes even ifas
contains the same#hash
.getInitialProps
is not re-run for genuine hash-only changes.Why?
Router.replace(hrefWithQueryAndHash, asWithHash)
previously could be considered “only a hash change” ifas
stayed identical (including#hash
), even whenhref
changed its query string. That made the router skip the full navigation/data-fetch flow (sogetInitialProps
or data fetching wasn't re-run).href
(url) in the hash-only check so changes to thehref
's query/search cause a full navigation/data-fetch as expected.How?
packages/next/src/shared/lib/router/router.ts
:onlyAHashChange(as: string, url?: string)
— ifurl
is provided, parse it withparseRelativeUrl(url)
and use the pathname+search when comparing to previousasPath
.change()
, callthis.onlyAHashChange(cleanedAs, url)
instead ofthis.onlyAHashChange(cleanedAs)
.scrollToHash
behavior unchanged (still usesonlyAHashChange(as)
without url), preserving current scroll optimizations for pure hash navigations.Files changed
packages/next/src/shared/lib/router/router.ts
onlyAHashChange
signature and logic, updated call-site inchange()
.test/integration/router-replace-hash-query/pages/index.js
test/integration/router-replace-hash-query/test/index.test.js
test/integration/router-replace-hash-query/package.json
(empty, placeholder test dir)Tests added
test/integration/router-replace-hash-query/test/index.test.js
next-test-utils
+next-webdriver
to:/
.test: undefined
.Router.replace(route + '?test=123#hash', asPath + '#hash')
.test: 123
andgetInitialProps
ran again.getInitialProps
.Checklist for Contributors (please run before opening PR)
pnpm prettier-fix
to fix formatting issues.fixes #<issue>
if applicable.Checklist for Maintainers
Closes NEXT-
Fixes #10900 <-- replace with the appropriate issue number if available
How to run the test locally (copy/paste)