Skip to content

Commit d8489c7

Browse files
committed
ref: Conditional import for react cahce
This gives a warning about top-level await in Next's webpack config, which doesn't seem to actually be an issue (at least on 15). Pushing this to test in CI, might revert later.
1 parent 6afea91 commit d8489c7

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

packages/nuqs/src/cache.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
// @ts-ignore
2-
import { cache } from 'react'
31
import type { SearchParams } from './defs'
42
import { error } from './errors'
53
import type { ParserBuilder, inferParserType } from './parsers'
64

75
const $input: unique symbol = Symbol('Input')
86

7+
type ReactCache = <CachedFunction extends Function>(
8+
fn: CachedFunction
9+
) => CachedFunction
10+
11+
let cache: ReactCache = () => {
12+
throw new Error('cache not available')
13+
}
14+
if (process.env.NEXT_DEPLOYMENT_ID !== undefined) {
15+
cache = await import('react')
16+
.then(react => react.cache)
17+
.catch(() => {
18+
throw new Error(
19+
'The React cache is not available in this environment. ' +
20+
'Please ensure you are using React 18 or later.'
21+
)
22+
})
23+
}
24+
925
export function createSearchParamsCache<
1026
Parsers extends Record<string, ParserBuilder<any>>
1127
>(

0 commit comments

Comments
 (0)