-
Notifications
You must be signed in to change notification settings - Fork 275
fix: usePathname() returns "/" during SSR of "use client" page components #689
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
southpolesteve
merged 2 commits into
cloudflare:main
from
Divkix:fix/usePathname-ssr-hydration-688
Mar 26, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
23 changes: 23 additions & 0 deletions
23
tests/fixtures/app-basic/app/use-client-page-pathname/[slug]/page.tsx
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| "use client"; | ||
|
|
||
| import { usePathname, useSearchParams, useParams } from "next/navigation"; | ||
|
|
||
| /** | ||
| * Dynamic-segment variant of the "use client" page regression fixture. | ||
| * | ||
| * Exercises useParams() in addition to usePathname() / useSearchParams() | ||
| * when the page component itself is a client component. | ||
| */ | ||
| export default function UseClientPagePathnameSlug() { | ||
| const pathname = usePathname(); | ||
| const searchParams = useSearchParams(); | ||
| const params = useParams(); | ||
|
|
||
| return ( | ||
| <div id="client-page-dynamic-info"> | ||
| <span id="client-page-dynamic-pathname">{pathname}</span> | ||
| <span id="client-page-dynamic-slug">{String(params.slug ?? "")}</span> | ||
| <span id="client-page-dynamic-search">{searchParams.toString()}</span> | ||
| </div> | ||
| ); | ||
| } |
28 changes: 28 additions & 0 deletions
28
tests/fixtures/app-basic/app/use-client-page-pathname/page.tsx
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| "use client"; | ||
|
|
||
| import { usePathname, useSearchParams } from "next/navigation"; | ||
|
|
||
| /** | ||
| * Regression fixture for issue #688. | ||
| * | ||
| * The page component itself is "use client" and calls usePathname() / | ||
| * useSearchParams(). This exercises a different import chain than a | ||
| * Server Component page rendering a "use client" child — the page module | ||
| * is resolved as a client reference by the RSC environment and rendered | ||
| * entirely in the SSR environment. | ||
| * | ||
| * Without the fix, usePathname() returns "/" during SSR (instead of the | ||
| * actual request pathname), causing a React hydration mismatch. | ||
| */ | ||
| export default function UseClientPagePathname() { | ||
| const pathname = usePathname(); | ||
| const searchParams = useSearchParams(); | ||
|
|
||
| return ( | ||
| <div id="client-page-info"> | ||
| <span id="client-page-pathname">{pathname}</span> | ||
| <span id="client-page-search-q">{searchParams.get("q") ?? ""}</span> | ||
| <span id="client-page-search-string">{searchParams.toString()}</span> | ||
| </div> | ||
| ); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /** | ||
| * Regression test for issue #688: usePathname() returns "/" during SSR when | ||
| * the page component itself is a "use client" component. | ||
| * | ||
| * Unlike the nav-context-hydration fixture (where the page is a Server | ||
| * Component rendering a "use client" child), this fixture has the page | ||
| * component itself marked as "use client". This exercises a different module | ||
| * resolution path in Vite's SSR environment — the page module is resolved as | ||
| * a client reference by the RSC entry and rendered entirely in the SSR module | ||
| * runner. | ||
| * | ||
| * Without the fix, usePathname() falls back to "/" because the "use client" | ||
| * page's module instance of navigation.ts may not have the ALS-backed state | ||
| * accessors registered (they were only registered on the SSR entry's instance). | ||
| * | ||
| * Fixture: tests/fixtures/app-basic/app/use-client-page-pathname/ | ||
| * page.tsx — "use client" page: usePathname(), useSearchParams() | ||
| * [slug]/page.tsx — "use client" dynamic page: + useParams() | ||
| */ | ||
|
|
||
| import { describe, it, expect, beforeAll, afterAll } from "vite-plus/test"; | ||
| import type { ViteDevServer } from "vite-plus"; | ||
| import { APP_FIXTURE_DIR, startFixtureServer } from "../helpers.js"; | ||
|
|
||
| let _server: ViteDevServer; | ||
| let _baseUrl: string; | ||
|
|
||
| const ROUTE = "/use-client-page-pathname"; | ||
|
|
||
| beforeAll(async () => { | ||
| ({ server: _server, baseUrl: _baseUrl } = await startFixtureServer(APP_FIXTURE_DIR, { | ||
| appRouter: true, | ||
| })); | ||
|
|
||
| // Warm up so first test doesn't pay cold-start cost. | ||
| const warmup = await fetch(`${_baseUrl}${ROUTE}`); | ||
| expect(warmup.ok).toBe(true); | ||
| }, 60_000); | ||
|
|
||
| afterAll(async () => { | ||
| await _server?.close(); | ||
| }); | ||
|
|
||
| // ── Static route: "use client" page with usePathname() ──────────────────── | ||
|
|
||
| describe('"use client" page component: usePathname() SSR (issue #688)', () => { | ||
| it("SSR HTML contains correct pathname (not /)", async () => { | ||
| const res = await fetch(`${_baseUrl}${ROUTE}`); | ||
| expect(res.status).toBe(200); | ||
| const html = await res.text(); | ||
|
|
||
| // The page component calls usePathname() and renders it into #client-page-pathname. | ||
| // During SSR, this must be the actual request pathname, not "/". | ||
| expect(html).toContain(`<span id="client-page-pathname">${ROUTE}</span>`); | ||
| }); | ||
|
|
||
| it("__VINEXT_RSC_NAV__ pathname matches SSR-rendered usePathname()", async () => { | ||
| const res = await fetch(`${_baseUrl}${ROUTE}`); | ||
| const html = await res.text(); | ||
|
|
||
| const match = html.match(/self\.__VINEXT_RSC_NAV__=(\{[^<]+\})/); | ||
| expect(match, "__VINEXT_RSC_NAV__ script tag not found").toBeTruthy(); | ||
| const nav = JSON.parse(match![1]); | ||
|
|
||
| expect(nav.pathname).toBe(ROUTE); | ||
| }); | ||
|
|
||
| it("SSR HTML contains correct searchParams with query string", async () => { | ||
| const res = await fetch(`${_baseUrl}${ROUTE}?q=hello&page=2`); | ||
| const html = await res.text(); | ||
|
|
||
| expect(html).toContain('<span id="client-page-search-q">hello</span>'); | ||
| expect(html).toContain('<span id="client-page-search-string">q=hello&page=2</span>'); | ||
| }); | ||
|
|
||
| it("__VINEXT_RSC_NAV__ searchParams matches query string", async () => { | ||
| const res = await fetch(`${_baseUrl}${ROUTE}?q=test`); | ||
| const html = await res.text(); | ||
|
|
||
| const match = html.match(/self\.__VINEXT_RSC_NAV__=(\{[^<]+\})/); | ||
| expect(match).toBeTruthy(); | ||
| const nav = JSON.parse(match![1]); | ||
| const sp = new URLSearchParams(nav.searchParams); | ||
|
|
||
| expect(sp.get("q")).toBe("test"); | ||
| }); | ||
| }); | ||
|
|
||
| // ── Dynamic route: "use client" page with useParams() ───────────────────── | ||
|
|
||
| describe('"use client" dynamic page: usePathname() + useParams() SSR', () => { | ||
| const dynamicPath = `${ROUTE}/my-slug`; | ||
|
|
||
| it("SSR HTML contains correct pathname for dynamic route", async () => { | ||
| const res = await fetch(`${_baseUrl}${dynamicPath}`); | ||
| expect(res.status).toBe(200); | ||
| const html = await res.text(); | ||
|
|
||
| expect(html).toContain(`<span id="client-page-dynamic-pathname">${dynamicPath}</span>`); | ||
| }); | ||
|
|
||
| it("SSR HTML contains correct slug param", async () => { | ||
| const res = await fetch(`${_baseUrl}${dynamicPath}`); | ||
| const html = await res.text(); | ||
|
|
||
| expect(html).toContain('<span id="client-page-dynamic-slug">my-slug</span>'); | ||
| }); | ||
|
|
||
| it("__VINEXT_RSC_PARAMS__ contains slug for dynamic route", async () => { | ||
| const res = await fetch(`${_baseUrl}${dynamicPath}`); | ||
| const html = await res.text(); | ||
|
|
||
| const match = html.match(/self\.__VINEXT_RSC_PARAMS__=(\{[^<]*\})/); | ||
| expect(match, "__VINEXT_RSC_PARAMS__ script tag not found").toBeTruthy(); | ||
| const params = JSON.parse(match![1]); | ||
|
|
||
| expect(params.slug).toBe("my-slug"); | ||
| }); | ||
|
|
||
| it("__VINEXT_RSC_NAV__ pathname is correct for dynamic route", async () => { | ||
| const res = await fetch(`${_baseUrl}${dynamicPath}`); | ||
| const html = await res.text(); | ||
|
|
||
| const match = html.match(/self\.__VINEXT_RSC_NAV__=(\{[^<]+\})/); | ||
| expect(match).toBeTruthy(); | ||
| const nav = JSON.parse(match![1]); | ||
|
|
||
| expect(nav.pathname).toBe(dynamicPath); | ||
| }); | ||
| }); |
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.
Nit for future readers: after
_registerStateAccessorsruns on the SSR entry's module instance, theseletvariables are overwritten to point directly at the ALS-backed functions. The_getGlobalAccessors()fallback in these defaults only fires from the other module instance that never had_registerStateAccessorscalled. The cost of the extra property lookup onglobalThisper hook invocation in the unregistered instance is negligible.