Repository navigation
Conversation
Stats from current PR🔴 2 regressions, 1 improvement
📊 All Metrics📖 Metrics GlossaryDev Server Metrics:
Build Metrics:
Change Thresholds:
⚡ Dev Server
📦 Dev Server (Webpack) (Legacy)📦 Dev Server (Webpack)
⚡ Production Builds
📦 Production Builds (Webpack) (Legacy)📦 Production Builds (Webpack)
📦 Bundle SizesBundle Sizes⚡ TurbopackClient Main Bundles
Server Middleware
Build DetailsBuild Manifests
Build Cache
📦 WebpackClient Main Bundles
Polyfills
Pages
Server Edge SSR
Middleware
Build DetailsBuild Manifests
Build Cache
🔄 Shared (bundler-independent)Runtimes
📝 Changed Files (10 files)Files with changes:
View diffsapp-page-exp..ntime.dev.jsfailed to diffapp-page-exp..time.prod.jsDiff too large to display app-page-tur..ntime.dev.jsfailed to diffapp-page-tur..time.prod.jsDiff too large to display app-page-tur..ntime.dev.jsfailed to diffapp-page-tur..time.prod.jsDiff too large to display app-page.runtime.dev.jsfailed to diffapp-page.runtime.prod.jsDiff too large to display pages-api.ru..time.prod.jsDiff too large to display pages.runtime.prod.jsDiff too large to display 📎 Tarball URLCommit: 9292673 |
Tests PassedCommit: a3325d0 |
This comment was marked as spam.
This comment was marked as spam.
|
Another symptom of the same bug, reported in #99245: after a full load at a fragment URL, same-route hash Links concatenate instead of replacing. Load
This case has been reported twice before, as #95671 and #98644. Both were closed automatically for having no repro link. It might be worth adding to the |
|
Another data point, from a production app hitting this. Still reproduces on A Same app and same clicks, measured on Fix check: changing only the write in Why it matters in production: the doubled URL is what A review here would help. Thanks for working on it. |
17594f8 to
6a8fd81
Compare
|
Hi do we know a stable release without this issue or a version which fixes this? We have production issue where chunks fail to load. This is affecting thousands of users. We are suspecting this as a root cause. |
|
We independently reproduced this issue in AMCore using Next.js 16.4.2 in a production build. Opening We applied a narrow local patch that strips the fragment from the canonical URL stored by This validates that normalization for our scenario, not this PR’s complete implementation or its separate server-action redirect change. An official fix would let us remove the temporary dependency patch. Thank you for working on this. |
Frontend deps: Next 16.4.6's App Router counts a navigation as hash-only only when the hash changed, so a link to the fragment the address already holds is treated as a refresh of the page and never scrolls (vercel/next.js#88986, open). A page loaded with a fragment also keeps that fragment in its route cache entry, to which the next fragment link appends its own, so the address reads `#a#b` (vercel/next.js#101168, open). 16.4.6 is the newest release, and the newest canary, 16.5.0-canary.49, carries neither fix: both of its lines read unchanged. The patch is pnpm's own mechanism, built with `pnpm patch` and `pnpm patch-commit` (pnpm.io/11.x/cli/patch), keyed to the exact version with both issues named beside the entry. It holds two fixes. - The navigate reducer, which every Link click and every router.push or replace enters, marks a navigation to a fragment with a new ScrollBehavior member, and completeSoftNavigation counts such a navigation as a fragment change even when the hash is the same. HTML's navigate algorithm scrolls to the fragment on every such navigation. Widening the hash comparison alone, as the research proposed, was rejected: a server action with no redirect and a server-patch retry navigate to the current URL with the default scroll behaviour, so every save on an address holding a fragment would have jumped the page to it. - discoverKnownRoute, the one writer of a route cache entry, stores the canonical URL without its fragment, the invariant Next's own comment at the unknown-route call states. That is where the open upstream fix vercel/next.js#96737 puts it. Trimming the hash where navigateUsingPrefetchedRouteTree reads it would leave the optimistic route entries, built from the same stored URL, carrying it. Only `dist/client/` is patched. In a production build the browser chunks and the server's SSR chunks both carry that copy: each patched hunk appears in them with the CommonJS export getters and footer that only `dist/client/` has, and no chunk holds the unpatched comparison, which `dist/esm/` would have brought. The server never runs a navigation. The resolving install `pnpm patch-commit` runs also wrote ajv-formats 3.0.1's optional peer on ajv into the lockfile, which the frozen install had not asked for. It is pnpm's own write, and the package declares that peer. Verified: `pnpm install --frozen-lockfile` in a clean copy of the manifest, lockfile, workspace file and patches exits 0 and installs the patched Next. `scripts/tests/test_patched_dependencies.py` exits 0. `pnpm build` with the gate's environment, `pnpm run typecheck`, `lint`, `knip` and `knip:prod` each exit 0. Whether the second click scrolls is established only in a browser on a rebuilt stack.
|
Summary
Strips any hash from the canonical URL inside discoverKnownRoute, the single function every route cache entry is written through. Entries are shared across hashes and readers append the target hash, so a stored hash produced Questions for the author (1)
Issues (1)
Checked (3)
Review steps
Read the full report ↗ (Download report.json • report.md) Confirmed: its repro failed on this commit and not the same way on the base. Unverified: a person should judge it. |
There was a problem hiding this comment.
Vercel Agent Verified Review reviewed a3325d0 and isn’t approving it: its findings need your judgment first. 1 open finding is listed in its review summary and inline. View the full report
|
|
||
| it('should keep the hash of a redirect issued by an action', async () => { | ||
| const browser = await next.browser('/') | ||
| await browser.elementById('to-action').click() |
There was a problem hiding this comment.
Note
Unverified · Minor — Action-redirect hash test passes without the server-action-reducer change
The PR says redirect('/foo#bar') from a server action loses its fragment, and adds 'should keep the hash of a redirect issued by an action' to cover it. The base already keeps the hash in the address bar: the reducer navigates to createHrefFromUrl(redirectUrl), which includes the hash by default. The edited redirectHref only goes into a RedirectError marked handled = true, which RedirectBoundary never navigates with. So the new test can't fail on the base, and the described bug isn't what this hunk changes. Its only visible effect is the URL in the rejected error's digest.
Needs your judgment: the review traced this through the code but couldn’t run anything to prove it.
There was a problem hiding this comment.
🤖 This reply was written with an AI agent, in a session where a human and the agent tested whether this hunk is needed with red/green e2e runs (pnpm test-start-turbo). The results below come from those runs.
Yes, it fails on the base. The reducer does navigate to the hashful URL, but the fixture calls the action from an onClick without awaiting it, so the rejected RedirectError reaches handleUnhandledRedirect in app-router.tsx. That listener doesn't check handled, and it pushes the error's URL again. On canary that URL is built with createHrefFromUrl(redirectLocation, false), so the second navigation drops the hash.
| Call style | without this hunk | with it |
|---|---|---|
onClick={() => action()} |
lands on /redirect-target with no hash |
#section |
<form action={action}> |
#section |
#section |
A <form action> goes through RedirectBoundary, which respects handled, so it already kept the hash. That case was fixed in #86367. The listener ignoring handled, and the double navigation that causes, will be a separate follow-up.
Route cache entries are keyed by pathname and search only, so one entry is shared across every hash of a route. A same-route navigation rebuilds the URL by appending the incoming hash to the entry's stored canonical URL:
next.js/packages/next/src/client/components/segment-cache/navigation.ts
Line 398 in 6f2db21
Which means that "the stored canonical URL is hashless". Every time we write to
RouteCacheEntry.canonicalUrlit has to be without hash. These places don't do that:create-initial-router-state.tspassescreateHrefFromUrl(location), so a direct entry to/p/123#modalseeds the entry with the hashserver-action-reducer.tsderives its URL fromstate.canonicalUrlwhen the action didn't redirect, so any revalidating action fired on a hash-ful URL poisons the entryrender-tree.ts(formerlyppr-navigations.ts, moved in Reorganize client router modules #97402) passes a retry URL thatfetchServerResponsereturns asoriginalUrlwhen the response wasn't redirectedAny client router navigation then conflicts with the stored hash. In #96714,
router.replace(pathname)restores a hash the app just removed, androuter.pushto a new hash produces#foo#bar, once again.Both happen on routes entered directly with a hash, and also after a server action revalidates.
Instead of going around patching
createHrefFromUrl, we should normalize insidediscoverKnownRoute, the single point all route cache entries are written through, rather than at each call site.I got an agent to look at
createHrefFromUrlusage further, and also noticed thatredirect('/foo#bar')from a server action called outside a form, such as from anonClick, loses its fragment.next.js/packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts
Lines 391 to 394 in 6f2db21
That dropped a fragment the action explicitly asked for, while the external-redirect branch four lines above kept it. Now both keep it.
The reducer itself navigates to the hashful URL, but when the action's rejection goes unhandled,
handleUnhandledRedirectinapp-router.tsxnavigates again with the error's URL and ignores itshandledmarker. A<form action>goes throughRedirectBoundary, which respects the marker, so that case already kept the hash.Also adding tests for the other places a fragment could plausibly end up on a
canonicalUrl.test/e2e/app-dir/hash-canonical-urlhas six of them: the hash not being cleared, and the double fragment, each on initial load and after a server action, plus the action redirect and a guard on the prefetch path.test/e2e/app-dir/hash-canonical-url-retryhas three more pinning the redirect-retry path.On turning prefetch off for the
hash-cross-path-pushtest, a prefetched entry is hashless, so as it stood it wouldn't have caught a regression.Fixes #96714
Fixes #99245
Fixes #95671
Fixes #96643
Related: #77101 (the
<form action>case was fixed by #86367 in 16.1.0-canary.17; this fixes theonClickcase)Verification
pnpm test-start-turboonhash-canonical-url,hash-canonical-url-retryand thenavigationcross-pathname hash test, all green on this branch. Red/green by reverting parts of the change:discoverKnownRoutestrip (canary's code): 4 of 6hash-canonical-urltests fail (direct entry with a hash, and after a revalidating action, for both replace and push)falseinnavigateToUnknownRoute: thehash-cross-path-pushtest and the retry suite's replace test also failserver-action-reducer.tschange: the action redirect test fails (url.hashis""); a<form action>variant of it passes either waynext devand webpack (covered by CI)