Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
dc691433
Unverified
Commit
dc691433
authored
Dec 17, 2025
by
Sebastian "Sebbie" Silbermann
Committed by
GitHub
Dec 17, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure constructor for `useSearchParams` can be imported for `instanceof` checks (#87269)
parent
ca831cfc
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
78 additions
and
9 deletions
+78
-9
navigation-devtools.ts
packages/next/src/client/components/navigation-devtools.ts
+2
-2
navigation.ts
packages/next/src/client/components/navigation.ts
+9
-5
hooks-client-context.shared-runtime.ts
...ext/src/shared/lib/hooks-client-context.shared-runtime.ts
+5
-2
page.js
...-dir/hooks/app/hooks/use-search-params/instanceof/page.js
+42
-0
hooks.test.ts
test/e2e/app-dir/hooks/hooks.test.ts
+20
-0
No files found.
packages/next/src/client/components/navigation-devtools.ts
View file @
dc691433
...
...
@@ -2,6 +2,7 @@ import type { FlightRouterState } from '../../shared/lib/app-router-types'
import
type
{
Params
}
from
'../../server/request/params'
import
{
createDevToolsInstrumentedPromise
,
ReadonlyURLSearchParams
,
type
InstrumentedPromise
,
type
NavigationPromises
,
}
from
'../../shared/lib/hooks-client-context.shared-runtime'
...
...
@@ -9,7 +10,6 @@ import {
computeSelectedLayoutSegment
,
getSelectedLayoutSegmentPath
,
}
from
'../../shared/lib/segment'
import
{
ReadonlyURLSearchParams
}
from
'./readonly-url-search-params'
/**
* Promises are cached by tree to ensure stability across suspense retries.
...
...
@@ -28,7 +28,7 @@ const layoutSegmentPromisesCache = new WeakMap<
* Creates instrumented promises for layout segment hooks at a given tree level.
* This is dev-only code for React Suspense DevTools instrumentation.
*/
export
function
createLayoutSegmentPromises
(
function
createLayoutSegmentPromises
(
tree
:
FlightRouterState
):
LayoutSegmentPromisesCache
|
null
{
if
(
process
.
env
.
NODE_ENV
===
'production'
)
{
...
...
packages/next/src/client/components/navigation.ts
View file @
dc691433
...
...
@@ -11,12 +11,12 @@ import {
PathnameContext
,
PathParamsContext
,
NavigationPromisesContext
,
ReadonlyURLSearchParams
,
}
from
'../../shared/lib/hooks-client-context.shared-runtime'
import
{
computeSelectedLayoutSegment
,
getSelectedLayoutSegmentPath
,
}
from
'../../shared/lib/segment'
import
{
ReadonlyURLSearchParams
}
from
'./readonly-url-search-params'
const
useDynamicRouteParams
=
typeof
window
===
'undefined'
...
...
@@ -61,15 +61,15 @@ export function useSearchParams(): ReadonlyURLSearchParams {
// In the case where this is `null`, the compat types added in
// `next-env.d.ts` will add a new overload that changes the return type to
// include `null`.
const
readonlySearchParams
=
useMemo
(()
=>
{
const
readonlySearchParams
=
useMemo
(()
:
ReadonlyURLSearchParams
=>
{
if
(
!
searchParams
)
{
// When the router is not ready in pages, we won't have the search params
// available.
return
null
return
null
!
}
return
new
ReadonlyURLSearchParams
(
searchParams
)
},
[
searchParams
])
as
ReadonlyURLSearchParams
},
[
searchParams
])
// Instrument with Suspense DevTools (dev-only)
if
(
process
.
env
.
NODE_ENV
!==
'production'
&&
'use'
in
React
)
{
...
...
@@ -285,6 +285,11 @@ export function useSelectedLayoutSegment(
export
{
unstable_isUnrecognizedActionError
}
from
'./unrecognized-action-error'
// Shared components APIs
export
{
// We need the same class that was used to instantiate the context value
// Otherwise instanceof checks will fail in usercode
ReadonlyURLSearchParams
,
}
export
{
notFound
,
forbidden
,
...
...
@@ -292,6 +297,5 @@ export {
redirect
,
permanentRedirect
,
RedirectType
,
ReadonlyURLSearchParams
,
unstable_rethrow
,
}
from
'./navigation.react-server'
packages/next/src/shared/lib/hooks-client-context.shared-runtime.ts
View file @
dc691433
...
...
@@ -2,6 +2,7 @@
import
{
createContext
}
from
'react'
import
type
{
Params
}
from
'../../server/request/params'
import
{
ReadonlyURLSearchParams
}
from
'../../client/components/readonly-url-search-params'
export
const
SearchParamsContext
=
createContext
<
URLSearchParams
|
null
>
(
null
)
export
const
PathnameContext
=
createContext
<
string
|
null
>
(
null
)
...
...
@@ -17,8 +18,8 @@ export type InstrumentedPromise<T> = Promise<T> & {
export
type
NavigationPromises
=
{
pathname
:
InstrumentedPromise
<
string
>
searchParams
:
InstrumentedPromise
<
any
>
// ReadonlyURLSearchParams
params
:
InstrumentedPromise
<
any
>
// Params
searchParams
:
InstrumentedPromise
<
ReadonlyURLSearchParams
>
params
:
InstrumentedPromise
<
Params
>
// Layout segment hooks (updated at each layout boundary)
selectedLayoutSegmentPromises
?:
Map
<
string
,
...
...
@@ -44,6 +45,8 @@ export function createDevToolsInstrumentedPromise<T>(
return
promise
}
export
{
ReadonlyURLSearchParams
}
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
SearchParamsContext
.
displayName
=
'SearchParamsContext'
PathnameContext
.
displayName
=
'PathnameContext'
...
...
test/e2e/app-dir/hooks/app/hooks/use-search-params/instanceof/page.js
0 → 100644
View file @
dc691433
'use client'
import
{
useSearchParams
,
ReadonlyURLSearchParams
}
from
'next/navigation'
import
{
useSyncExternalStore
}
from
'react'
export
default
function
Page
()
{
const
searchParams
=
useSearchParams
()
const
searchParamsClient
=
useSyncExternalStore
(
()
=>
{
return
()
=>
{}
},
()
=>
{
return
searchParams
},
()
=>
{
return
null
}
)
return
(
<
ul
>
<
li
>
server
:{
' '
}
<
span
data
-
testid
=
"server"
suppressHydrationWarning
>
{
searchParams
instanceof
ReadonlyURLSearchParams
?
'PASS instanceof check'
:
`FAILED Received: "
${
String
(
searchParams
)}
" instead instanceof
${
searchParams
.
constructor
.
name
}
`
}
<
/span
>
<
/li
>
<
li
>
client
:{
' '
}
<
span
data
-
testid
=
"client"
>
{
searchParamsClient
===
null
?
'<pending>'
:
searchParamsClient
instanceof
ReadonlyURLSearchParams
?
'PASS instanceof check'
:
`FAILED Received: "
${
String
(
searchParamsClient
)}
" instead instanceof
${
searchParamsClient
.
constructor
.
name
}
`
}
<
/span
>
<
/li
>
<
/ul
>
)
}
test/e2e/app-dir/hooks/hooks.test.ts
View file @
dc691433
...
...
@@ -71,6 +71,26 @@ describe('app dir - hooks', () => {
expect
(
$
(
'#params-not-real'
).
text
()).
toBe
(
'N/A'
)
})
}
it
(
'should be able to use instanceof ReadonlyURLSearchParams'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/hooks/use-search-params/instanceof?foo=bar'
)
const
[
server
,
client
]
=
await
Promise
.
all
([
browser
.
elementByCss
(
'[data-testid="server"]'
)
.
then
((
handle
)
=>
handle
.
text
()),
browser
.
elementByCss
(
'[data-testid="client"]'
)
.
then
((
handle
)
=>
handle
.
text
()),
])
expect
({
client
,
server
}).
toEqual
({
server
:
'PASS instanceof check'
,
client
:
'PASS instanceof check'
,
})
})
})
describe
(
'useDraftMode'
,
()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment