Skip to content

Fix(segment-cache): replace hash instead of concatenating on repeated same-path navigation - #93855

Open
Varshil-B-Shah wants to merge 2 commits into
vercel:canaryfrom
Varshil-B-Shah:fix/hash-concatenation-on-cross-path-navigation
Open

Varshil-B-Shah wants to merge 2 commits into
vercel:canaryfrom
Varshil-B-Shah:fix/hash-concatenation-on-cross-path-navigation

Conversation

@Varshil-B-Shah

Copy link
Copy Markdown

What?

Fixes a bug where navigating between hash fragments on the same pathname results in hash concatenation instead of hash replacement.

Steps to reproduce:

  1. Navigate from /start to /destination#foo via a <Link>
  2. On /destination#foo, click another <Link> to /destination#baz
  3. The browser URL becomes /destination#foo#baz instead of /destination#baz

This also affects router.push('/path#newHash') when the current URL already contains a hash on the same path.

Why?

When navigating to a URL that has already been prefetched and cached in the segment cache, navigateUsingPrefetchedRouteTree constructs the new canonicalUrl by appending url.hash directly to route.canonicalUrl:

// packages/next/src/client/components/segment-cache/navigation.ts
const canonicalUrl = route.canonicalUrl + url.hash

The problem is that route.canonicalUrl is stored in the cache with the hash included from the first navigation (e.g. /destination#foo). When the user navigates to the same path with a different hash (e.g. /destination#baz), the new hash is blindly appended to the already-hashed canonical URL, producing /destination#foo#baz.

The cache intentionally keys entries without the hash (so /destination#foo and /destination#baz share the same cache entry), but the canonicalUrl stored in the entry retains the full URL including the hash from when the entry was first populated. The code comment in the file even acknowledges this design tension:

"hash entries do not vary by hash fragment. However, the one we set in the router state does include the hash..."

This regression was introduced as part of the Segment Cache / Cached Navigations feature that shipped in Next.js 16.2.0.

How?

Strip any existing hash from route.canonicalUrl before appending the new hash:

// Before
const canonicalUrl = route.canonicalUrl + url.hash

// After
const canonicalUrl = route.canonicalUrl.split('#')[0] + url.hash

split('#')[0] takes everything before the first #, ensuring no stale hash can survive into the new URL. The correct new hash from url.hash is then appended cleanly.

This is safe because:

  • If route.canonicalUrl has no hash: split('#')[0] returns the full string unchanged — zero behavior change for the common case
  • If route.canonicalUrl has a hash: the stale hash is stripped before the new one is appended — the bug is fixed
  • If url.hash is '' (navigating to a path with no hash): the result is just the clean path — correct

The cache lookup logic (which ignores hashes) is completely untouched.

Testing

The test fixtures and test case are adapted from the draft PR #93132 by @icyJoseph which identified the bug with a failing e2e test. This PR adds the fix that makes the test pass.

The test covers the exact reproduction scenario:

  1. Start at /hash-cross-path-push
  2. Click <Link href="/hash-cross-path-push/destination#foo"> → URL becomes /destination#foo ✓
  3. Click <Link href="/hash-cross-path-push/destination#baz"> → URL becomes /destination#baz ✓ (was /destination#foo#baz before the fix)

Fixes #93126

@Varshil-B-Shah
Varshil-B-Shah force-pushed the fix/hash-concatenation-on-cross-path-navigation branch from ac5ac6d to 41e75ba Compare May 14, 2026 15:56
@Varshil-B-Shah
Varshil-B-Shah force-pushed the fix/hash-concatenation-on-cross-path-navigation branch from 41e75ba to dc2bcae Compare May 14, 2026 16:02
@github-actions

Copy link
Copy Markdown
Contributor

Failing test suites

Commit: dc2bcae | About building and testing Next.js

pnpm test-deploy test/e2e/app-dir/navigation/navigation.test.ts (job)

  • app dir - navigation > SEO > should emit noindex meta tag for not found page when streaming (DD)
  • app dir - navigation > SEO > should emit refresh meta tag for redirect page when streaming (DD)
  • app dir - navigation > SEO > should emit refresh meta tag (permanent) for redirect page when streaming (DD)
  • app dir - navigation > SEO > should contain default meta tags in error page (DD)
  • app dir - navigation > SEO > should not log 404 errors in ipc server (DD)
  • app dir - navigation > browser back to a revalidated page > should load the page (DD)
  • app dir - navigation > cross-pathname Link then same-pathname hash change > should replace (not concatenate) the hash when triggers the same-pathname hash change (DD)
  • app dir - navigation > external push > should push external url without affecting hooks (DD)
  • app dir - navigation > hash > should scroll to the specified hash (DD)
  • app dir - navigation > hash > should not scroll to hash when scroll={false} is set (DD)
  • app dir - navigation > hash-link-back-to-same-page > should scroll to the specified hash (DD)
  • app dir - navigation > hash-with-scroll-offset > should scroll to the specified hash (DD)
  • app dir - navigation > middleware redirect > should change browser location when router.refresh() gets a redirect response (DD)
  • app dir - navigation > navigating to a page with async metadata > shows a fallback when prefetch was pending (DD)
  • app dir - navigation > navigating to a page with async metadata > shows a fallback when prefetch completed (DD)
  • app dir - navigation > navigating to dynamic params & changing the casing > should load the page correctly (DD)
  • app dir - navigation > navigation between pages and app > should not contain _rsc query while navigating from app to pages (DD)
  • app dir - navigation > navigation between pages and app > should not contain _rsc query while navigating from pages to app (DD)
  • app dir - navigation > navigation between pages and app > should not omit the hash while navigating from app to pages (DD)
  • app dir - navigation > navigation between pages and app > should not continously initiate a mpa navigation to the same URL when router state changes (DD)
  • app dir - navigation > navigations when attaching a Proxy to window.Promise > should navigate without issue (DD)
  • app dir - navigation > nested navigation > should navigate to nested pages (DD)
  • app dir - navigation > nested navigation > should load chunks correctly without double encoding of url (DD)
  • app dir - navigation > not-found > should trigger not-found in a server component (DD)
  • app dir - navigation > not-found > should trigger not-found in a client component (DD)
  • app dir - navigation > not-found > should trigger not-found client-side (DD)
  • app dir - navigation > not-found > should trigger not-found while streaming (DD)
  • app dir - navigation > query string > should set query correctly (DD)
  • app dir - navigation > query string > should set query with semicolon correctly (DD)
  • app dir - navigation > query string > should handle unicode search params (DD)
  • app dir - navigation > query string > should not reset shallow url updates on prefetch (DD)
  • app dir - navigation > query string > useParams identity between renders > should be stable in app (DD)
  • app dir - navigation > query string > useParams identity between renders > should be stable in pages (DD)
  • app dir - navigation > redirect > components > should redirect in a server component (DD)
  • app dir - navigation > redirect > components > should redirect in a client component (DD)
  • app dir - navigation > redirect > components > should redirect client-side (DD)
  • app dir - navigation > redirect > components > should redirect to external url (DD)
  • app dir - navigation > redirect > components > should redirect to external url, initiating only once (DD)
  • app dir - navigation > redirect > components > should only trigger the redirect once (/redirect/servercomponent) (DD)
  • app dir - navigation > redirect > components > should only trigger the redirect once (redirect/redirect-with-loading) (DD)
  • app dir - navigation > redirect > middleware redirects > should redirect from middleware (DD)
  • app dir - navigation > redirect > middleware redirects > should redirect from middleware with link navigation (DD)
  • app dir - navigation > redirect > next.config.js redirects > should redirect from next.config.js (DD)
  • app dir - navigation > redirect > next.config.js redirects > should redirect from next.config.js with link navigation (DD)
  • app dir - navigation > relative hashes and queries > should work with a hash-only href (DD)
  • app dir - navigation > relative hashes and queries > should work with a hash-only router.push(...) (DD)
  • app dir - navigation > relative hashes and queries > should work with a query-only href (DD)
  • app dir - navigation > relative hashes and queries > should work with both relative hashes and queries (DD)
  • app dir - navigation > scroll restoration > should restore original scroll position when navigating back (DD)
  • app dir - navigation > useRouter identity between navigations > should preserve identity when navigating to the same page (DD)
  • app dir - navigation > useRouter identity between navigations > should preserve identity when navigating between different pages (DD)
Expand output

● app dir - navigation › query string › should set query correctly

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › query string › should set query with semicolon correctly

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › query string › should handle unicode search params

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › query string › should not reset shallow url updates on prefetch

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › query string › useParams identity between renders › should be stable in app

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › query string › useParams identity between renders › should be stable in pages

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › hash › should scroll to the specified hash

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › hash › should not scroll to hash when scroll={false} is set

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › hash-with-scroll-offset › should scroll to the specified hash

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › hash-link-back-to-same-page › should scroll to the specified hash

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following URL:
> https://vercel.com/docs/cli/about-telemetry
Error: No existing credentials found. Please run `vercel login` or pass "--token"
Learn More: https://err.sh/vercel/no-credentials-found (1)

  343 |
  344 |     if (linkRes.exitCode !== 0) {
> 345 |       throw new Error(
      |             ^
  346 |         `Failed to link project ${linkRes.stdout} ${linkRes.stderr} (${linkRes.exitCode})`
  347 |       )
  348 |     }

  at NextDeployInstance.setup (lib/next-modes/next-deploy.ts:345:13)
  at lib/e2e-utils/index.ts:285:7
  at Span.traceAsyncFn (../packages/next/src/trace/trace.ts:146:14)
  at createNext (lib/e2e-utils/index.ts:250:12)
  at Object.<anonymous> (lib/e2e-utils/index.ts:340:14)

● app dir - navigation › relative hashes and queries › should work with a hash-only href

Failed to link project  > NOTE: The Vercel CLI now collects telemetry regarding usage of the CLI.
> This information is used to shape the CLI roadmap and prioritize features.
> You can learn more, including how to opt-out if you'd not like to participate in this program, by visiting the following

... truncated ...

brijeshb42 added a commit to brijbyte/md3 that referenced this pull request Jul 14, 2026
Navigating to a route whose segment-cache entry was recorded with a hash
appends the new fragment instead of replacing it (vercel/next.js#93126,
regression in 16.2.0). Applies the one-line fix from the upstream PR
(vercel/next.js#93855) via pnpm patch; drop the patch once it ships in a
stable release.

Assisted by Claude Fable 5
icyJoseph added a commit that referenced this pull request Aug 3, 2026
Fixes: #93126,
#95551

Repro:

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

Unlike, #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.
m-kawafuji pushed a commit to m-kawafuji/next.js that referenced this pull request Aug 8, 2026
Fixes: vercel#93126,
vercel#95551

Repro:

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

Unlike, vercel#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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Router .push with fragments duplicates when doing client side navigation

1 participant