-
Notifications
You must be signed in to change notification settings - Fork 221
fix: return route pattern from router.pathname, not resolved path #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
james-elicx
merged 8 commits into
cloudflare:main
from
NathanDrake2406:fix/router-url-as-two-value
Mar 11, 2026
+16
−12
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
715fd44
fix: stabilize instrumentation state and hydration E2E tests
NathanDrake2406 ce20051
fix: honor the `as` parameter in router.push/replace
NathanDrake2406 3da95b4
fix: address PR review feedback
NathanDrake2406 fad6f97
fix: remove pathname assertion that tests unimplemented behavior
NathanDrake2406 090b76c
fix: return route pattern from router.pathname, not resolved path
NathanDrake2406 509e340
fix: set SSR pathname to route pattern to prevent hydration mismatch
NathanDrake2406 a5bb971
Merge remote-tracking branch 'origin/main' into fix/router-url-as-two…
james-elicx eeb5fdc
test: update entry-templates snapshots for pathname fix
james-elicx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -279,7 +279,11 @@ function getPathnameAndQuery(): { | |
| } | ||
| return { pathname: "/", query: {}, asPath: "/" }; | ||
| } | ||
| const pathname = stripBasePath(window.location.pathname, __basePath); | ||
| const resolvedPath = stripBasePath(window.location.pathname, __basePath); | ||
| // In Next.js, router.pathname is the route pattern (e.g., "/posts/[id]"), | ||
| // not the resolved path ("/posts/42"). __NEXT_DATA__.page holds the route | ||
| // pattern and is updated by navigateClient() on every client-side navigation. | ||
| const pathname = window.__NEXT_DATA__?.page ?? resolvedPath; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fallback to |
||
| const routeQuery: Record<string, string | string[]> = {}; | ||
| // Include dynamic route params from __NEXT_DATA__ (e.g., { id: "42" } from /posts/[id]). | ||
| // Only include keys that are part of the route pattern (not stale query params). | ||
|
|
@@ -302,7 +306,8 @@ function getPathnameAndQuery(): { | |
| addQueryParam(searchQuery, key, value); | ||
| } | ||
| const query = { ...searchQuery, ...routeQuery }; | ||
| const asPath = pathname + window.location.search + window.location.hash; | ||
| // asPath uses the resolved browser path, not the route pattern | ||
| const asPath = resolvedPath + window.location.search + window.location.hash; | ||
| return { pathname, query, asPath }; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switching client
pathnametowindow.__NEXT_DATA__.pagehere makes dynamic-route pages hydrate with a different value than the server markup, because SSR still seeds router state from the resolved URL path (e.g.setSSRContext({ pathname: routeUrl.split("?")[0], ... })in the pages server entries). When a page rendersrouter.pathnamein HTML (like/posts/[id]pages), SSR prints/posts/123but hydration computes/posts/[id], which triggers hydration mismatch warnings and DOM replacement on first load.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in 509e340. All three SSR entry points (dev-server, pages-server-entry, static-export) now use
patternToNextFormat(route.pattern)for the SSR context pathname, matching what__NEXT_DATA__.pagealready provides. SSR and hydration are now consistent.