Skip to content

Commit 12ec159

Browse files
committed
fix: Import React.cache only when needed
Having `import { cache } from 'react'` caused issues when using React 18 or 19 GA, because the `cache` function is only exported in canary builds (which the Next.js app router uses internally, regardless of what's in your app's package.json). This meant importing from `'nuqs/server'` caused an import error when done from non app-router code, like the pages router, or API route definitions, which would fallback to the version of React defined in the package.json (and likely a stable one). Changing the import to `import * as React from 'react'` is what is being highlighted in the React docs themselves, and allows to only call the cache function when actually creating a cache object. Closes #804, and supersedes #805.
1 parent b9926ca commit 12ec159

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

packages/nuqs/src/cache.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// @ts-ignore
2-
import { cache } from 'react'
1+
import * as React from 'react'
32
import type { SearchParams, UrlKeys } from './defs'
43
import { error } from './errors'
54
import { createLoader } from './loader'
@@ -27,7 +26,7 @@ export function createSearchParamsCache<Parsers extends ParserMap>(
2726
// whereas a simple object would be bound to the lifecycle of the process,
2827
// which may be reused between requests in a serverless environment
2928
// (warm lambdas on Vercel or AWS).
30-
const getCache = cache<() => Cache>(() => ({
29+
const getCache = React.cache<() => Cache>(() => ({
3130
searchParams: {}
3231
}))
3332

0 commit comments

Comments
 (0)