mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #355] [MERGED] fix: nav context hydration mismatch (usePathname/useSearchParams React error) #505
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#505
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/cloudflare/vinext/pull/355
Author: @james-elicx
Created: 3/8/2026
Status: ✅ Merged
Merged: 3/8/2026
Merged by: @james-elicx
Base:
main← Head:j-branch-9📝 Commits (2)
4e4d567fix: nav context hydration mismatchde10162fix: address bonk review — add VINEXT_RSC_NAV type, serialise searchParams as entry pairs📊 Changes
6 files changed (+473 additions, -26 deletions)
View changed files
📝
packages/vinext/src/global.d.ts(+13 -0)📝
packages/vinext/src/server/app-dev-server.ts(+23 -2)➕
tests/fixtures/app-basic/app/nextjs-compat/nav-context-hydration/[id]/page.tsx(+27 -0)➕
tests/fixtures/app-basic/app/nextjs-compat/nav-context-hydration/nav-info.tsx(+33 -0)➕
tests/fixtures/app-basic/app/nextjs-compat/nav-context-hydration/page.tsx(+26 -0)📝
tests/nextjs-compat/rsc-context-lazy-stream.test.ts(+351 -24)📄 Description
Problem
useSyncExternalStorerequires thatgetServerSnapshotreturns the same value the server rendered. ForusePathname()anduseSearchParams(), the server snapshot reads from the navigation context — populated during RSC rendering from the request URL and passed through to the SSR environment.The browser entry had no way to know what pathname/searchParams were used on the server. So
getServerSnapshotalways fell back to"/"and an emptyURLSearchParams, regardless of the actual request URL. React would compare this against the SSR-rendered HTML (which correctly reflected the real URL) and throw hydration error #418:This surfaced whenever a
"use client"component calledusePathname()oruseSearchParams()— for example, any nav bar, breadcrumb, or active-link component.Fix
Server (
generateSsrEntry): embed the navigation context as two<script>tags in<head>during HTML generation:searchParamsis serialised as a plain object viaObject.fromEntries()(safe to JSON-encode; reconstructed on the client withnew URLSearchParams()). Both values are passed throughsafeJsonStringifyto prevent</script>injection via crafted query strings.Browser entry (
generateBrowserEntry): readself.__VINEXT_RSC_NAV__before callinghydrateRoot()and callsetNavigationContext()so the client snapshot matches what the server rendered. All three code paths are covered: progressive chunks (new format), legacy single-blob (__VINEXT_RSC__), and the rare fresh-fetch fallback.Regression tests
15 new Vitest tests in
rsc-context-lazy-stream.test.ts, exercising:__VINEXT_RSC_NAV__and__VINEXT_RSC_PARAMS__are present in the HTMLpathnamematches the request path and agrees with the SSR-renderedusePathname()outputsearchParamsis{}with no query string; carries real params when present; round-trips correctly throughnew URLSearchParams()<,>,&) in query params are safely encoded bysafeJsonStringify[id]routes:__VINEXT_RSC_PARAMS__carries the matched segment value and__VINEXT_RSC_NAV__.pathnameis correct</head>, guaranteeing they're available beforehydrateRoot()is calledpathnamecontains only the path, never the query string🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.