Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50c7528

Browse files
authored
Fix resolving href with query and matching as (vercel#29246)
1 parent 8e52126 commit 50c7528

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

packages/next/shared/lib/router/router.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,9 @@ export default class Router implements BaseRouter {
833833
return false
834834
}
835835
const shouldResolveHref =
836-
url === as || (options as any)._h || (options as any)._shouldResolveHref
836+
(options as any)._h ||
837+
(options as any)._shouldResolveHref ||
838+
pathNoQueryHash(url) === pathNoQueryHash(as)
837839

838840
// for static pages with query params in the URL we delay
839841
// marking the router ready until after the query is updated

test/integration/dynamic-routing/pages/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const Page = () => {
2626
<a id="view-post-1-hash-1-href-only">View post 1 (hash only href)</a>
2727
</Link>
2828
<br />
29+
<Link href="/post-1?hidden=value" as="/post-1">
30+
<a id="view-post-1-hidden-query">View post 1 (href query)</a>
31+
</Link>
32+
<br />
2933
<Link
3034
href={{
3135
hash: 'my-hash',

test/integration/dynamic-routing/test/index.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,23 @@ function runTests(dev) {
368368
}
369369
})
370370

371+
it('should navigate to a dynamic page with href with differing query and as correctly', async () => {
372+
let browser
373+
try {
374+
browser = await webdriver(appPort, '/')
375+
await browser.eval('window.beforeNav = 1')
376+
await browser.elementByCss('#view-post-1-hidden-query').click()
377+
await browser.waitForElementByCss('#asdf')
378+
379+
expect(await browser.eval('window.beforeNav')).toBe(1)
380+
381+
const text = await browser.elementByCss('#asdf').text()
382+
expect(text).toMatch(/this is.*?post-1/i)
383+
} finally {
384+
if (browser) await browser.close()
385+
}
386+
})
387+
371388
it('should navigate to a dynamic page successfully no as', async () => {
372389
let browser
373390
try {

0 commit comments

Comments
 (0)