Skip to content

Commit

Permalink
ref: Conditional import for react cahce
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
franky47 committed Dec 20, 2024
1 parent 6afea91 commit d8489c7
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/nuqs/src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
// @ts-ignore
import { cache } from 'react'
import type { SearchParams } from './defs'
import { error } from './errors'
import type { ParserBuilder, inferParserType } from './parsers'

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

type ReactCache = <CachedFunction extends Function>(
fn: CachedFunction
) => CachedFunction

let cache: ReactCache = () => {
throw new Error('cache not available')
}
if (process.env.NEXT_DEPLOYMENT_ID !== undefined) {
cache = await import('react')
.then(react => react.cache)
.catch(() => {
throw new Error(
'The React cache is not available in this environment. ' +
'Please ensure you are using React 18 or later.'
)
})
}

export function createSearchParamsCache<
Parsers extends Record<string, ParserBuilder<any>>
>(
Expand Down

0 comments on commit d8489c7

Please sign in to comment.