Unverified Commit 459617a1 authored by Joseph's avatar Joseph Committed by GitHub

fix: double fragment on navigation (#93132)

Fixes: https://github.com/vercel/next.js/issues/93126,
https://github.com/vercel/next.js/issues/95551

Repro:

- from `/` navigate to `/abc#foo`
- on `/abc#foo` - now click `/abc#bar`
- the url is now at `/abc#foo#bar`

Unlike, https://github.com/vercel/next.js/pull/93855, this PR fixes what
gets stored in the segment cache navigation, rather than slicing away an
existing hash. It is plausible that dropping the hash from the segment
cache is not desired though.
parent cbf0cef6
...@@ -534,7 +534,9 @@ async function navigateToUnknownRoute( ...@@ -534,7 +534,9 @@ async function navigateToUnknownRoute(
navigationSeed.routeTree, navigationSeed.routeTree,
metadataVaryPath, metadataVaryPath,
couldBeIntercepted, couldBeIntercepted,
createHrefFromUrl(canonicalUrl), // Store a hashless canonical URL: the entry is shared across hashes, and
// a later same-route hash nav appends `url.hash` to it.
createHrefFromUrl(canonicalUrl, false),
supportsPerSegmentPrefetching, supportsPerSegmentPrefetching,
false // hasDynamicRewrite - not a retry, rewrite detection happens during traversal false // hasDynamicRewrite - not a retry, rewrite detection happens during traversal
) )
......
import Link from 'next/link'
export function RouterPushToTarget() {
return (
<Link id="link-to-target-bar" href="/hash-cross-path-push/destination#bar">
To destination#bar
</Link>
)
}
import Link from 'next/link'
export default function Page() {
return (
<>
<h1>Target</h1>
<Link
href="/hash-cross-path-push/destination#baz"
id="link-to-target-baz"
>
Link to destination#baz
</Link>
</>
)
}
import Link from 'next/link'
import { RouterPushToTarget } from './client-component'
export default function Page() {
return (
<>
<h1>Start</h1>
<Link
href="/hash-cross-path-push/destination#foo"
id="link-to-target-foo"
>
Link to destination#foo
</Link>
<RouterPushToTarget />
</>
)
}
...@@ -366,6 +366,29 @@ describe('app dir - navigation', () => { ...@@ -366,6 +366,29 @@ describe('app dir - navigation', () => {
}) })
}) })
describe('cross-pathname Link then same-pathname hash change', () => {
const startPath = '/hash-cross-path-push'
const destinationPath = '/hash-cross-path-push/destination'
it('should replace (not concatenate) the hash when <Link> triggers the same-pathname hash change', async () => {
const browser = await next.browser(startPath)
await browser.elementByCss('#link-to-target-foo').click()
await retry(() =>
expect(browser.url()).resolves.toEqual(
next.url + destinationPath + '#foo'
)
)
await browser.elementByCss('#link-to-target-baz').click()
await retry(() =>
expect(browser.url()).resolves.toEqual(
next.url + destinationPath + '#baz'
)
)
})
})
describe('not-found', () => { describe('not-found', () => {
it('should trigger not-found in a server component', async () => { it('should trigger not-found in a server component', async () => {
const browser = await next.browser('/not-found/servercomponent') const browser = await next.browser('/not-found/servercomponent')
......
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