Skip to content

Commit da7cf40

Browse files
committed
doc: Add new path for the cache feature
1 parent 6d240c0 commit da7cf40

File tree

10 files changed

+38
-35
lines changed

10 files changed

+38
-35
lines changed

README.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -634,12 +634,9 @@ to do so in a type-safe manner.
634634
635635
```tsx
636636
// searchParams.ts
637-
import {
638-
createSearchParamsCache,
639-
parseAsInteger,
640-
parseAsString
641-
} from 'nuqs/server'
642-
// Note: import from 'nuqs/server' to avoid the "use client" directive
637+
import { createSearchParamsCache } from 'nuqs/server/cache'
638+
import { parseAsInteger, parseAsString } from 'nuqs/server'
639+
// Note: import parsers from 'nuqs/server' to avoid the "use client" directive
643640

644641
export const searchParamsCache = createSearchParamsCache({
645642
// List your search param keys and associated parsers here:
@@ -681,7 +678,8 @@ parser declaration with `useQueryStates` for type-safety in client components:
681678

682679
```tsx
683680
// searchParams.ts
684-
import { parseAsFloat, createSearchParamsCache } from 'nuqs/server'
681+
import { parseAsFloat } from 'nuqs/server'
682+
import { createSearchParamsCache } from 'nuqs/server/cache'
685683

686684
export const coordinatesParsers = {
687685
lat: parseAsFloat.withDefault(45.18),

errors/NUQS-500.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,8 @@ Run the `parse` method and feed it the page's `searchParams`:
2222

2323
```tsx
2424
// page.tsx
25-
import {
26-
createSearchParamsCache,
27-
parseAsInteger,
28-
parseAsString,
29-
type SearchParams
30-
} from 'nuqs/server'
25+
import { parseAsInteger, parseAsString, type SearchParams } from 'nuqs/server'
26+
import { createSearchParamsCache } from 'nuqs/server/cache'
3127

3228
const cache = createSearchParamsCache({
3329
q: parseAsString,

packages/docs/content/docs/server-side.mdx

+15-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Type-safe search params on the server
44
---
55

66
<Callout>
7-
This feature is available for Next.js only.
7+
The `next/server/cache` feature is available for Next.js app router only.
88
</Callout>
99

1010
If you wish to access the searchParams in a deeply nested Server Component
@@ -20,12 +20,9 @@ to do so in a type-safe manner.
2020
</Callout>
2121

2222
```ts title="searchParams.ts"
23-
import {
24-
createSearchParamsCache,
25-
parseAsInteger,
26-
parseAsString
27-
} from 'nuqs/server'
28-
// Note: import from 'nuqs/server' to avoid the "use client" directive
23+
import { createSearchParamsCache } from 'nuqs/server/cache'
24+
import { parseAsInteger, parseAsString } from 'nuqs/server'
25+
// Note: import parsers from 'nuqs/server' to avoid the "use client" directive
2926

3027
export const searchParamsCache = createSearchParamsCache({
3128
// List your search param keys and associated parsers here:
@@ -61,17 +58,24 @@ function Results() {
6158
}
6259
```
6360

61+
<Callout type="warn" title="Deprecation notice">
62+
The cache feature is also accessible from `nuqs/server` in nuqs@^2, but
63+
will be removed from that import in nuqs@3.0.0.
64+
Please update your imports to `nuqs/server/cache` for a smoother transition.
65+
66+
This is to allow non-Next.js server code to use the `nuqs/server` import
67+
without having to install React canary for the `cache` function.
68+
</Callout>
69+
6470
The cache will only be valid for the current page render
6571
(see React's [`cache`](https://react.dev/reference/react/cache) function).
6672

6773
Note: the cache only works for **server components**, but you may share your
6874
parser declaration with `useQueryStates` for type-safety in client components:
6975

7076
```ts title="searchParams.ts"
71-
import {
72-
parseAsFloat,
73-
createSearchParamsCache
74-
} from 'nuqs/server'
77+
import { parseAsFloat } from 'nuqs/server'
78+
import { createSearchParamsCache } from 'nuqs/server/cache'
7579

7680
export const coordinatesParsers = {
7781
lat: parseAsFloat.withDefault(45.18),

packages/docs/src/app/(pages)/stats/searchParams.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { createSearchParamsCache, parseAsStringLiteral } from 'nuqs/server'
1+
import { parseAsStringLiteral } from 'nuqs/server'
2+
import { createSearchParamsCache } from 'nuqs/server/cache'
23

34
export const pkgOptions = ['nuqs', 'next-usequerystate', 'both'] as const
45
export const pkgParser = parseAsStringLiteral(pkgOptions).withDefault('both')

packages/docs/src/app/playground/(demos)/pagination/searchParams.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2-
createSearchParamsCache,
32
createSerializer,
43
parseAsInteger,
54
parseAsStringLiteral
65
} from 'nuqs/server'
6+
import { createSearchParamsCache } from 'nuqs/server/cache'
77

88
export const renderingOptions = ['server', 'client'] as const
99
export type RenderingOptions = (typeof renderingOptions)[number]

packages/e2e/next/src/app/app/cache/searchParams.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import {
2-
createSearchParamsCache,
3-
parseAsBoolean,
4-
parseAsInteger,
5-
parseAsString
6-
} from 'nuqs/server'
1+
import { parseAsBoolean, parseAsInteger, parseAsString } from 'nuqs/server'
2+
import { createSearchParamsCache } from 'nuqs/server/cache'
73

84
export const parsers = {
95
str: parseAsString,

packages/e2e/next/src/app/app/push/searchParams.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { createSearchParamsCache, parseAsInteger } from 'nuqs/server'
1+
import { parseAsInteger } from 'nuqs/server'
2+
import { createSearchParamsCache } from 'nuqs/server/cache'
23

34
export const parser = parseAsInteger.withDefault(0).withOptions({
45
history: 'push'

packages/e2e/next/src/app/app/rewrites/destination/searchParams.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { createSearchParamsCache, parseAsString } from 'nuqs/server'
1+
import { parseAsString } from 'nuqs/server'
2+
import { createSearchParamsCache } from 'nuqs/server/cache'
23

34
export const searchParams = {
45
injected: parseAsString.withDefault('null'),

packages/nuqs/src/index.server.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/** @deprecated Import createSearchParamsCache from 'nuqs/server/cache' instead.
2+
*
3+
* This export will be removed from 'nuqs/server' in nuqs@3.0.0,
4+
* to allow non-Next.js server code to use the parsers, serializeres and other
5+
* server-side utilities without depending on React canary for the `cache` function.
6+
*/
17
export { createSearchParamsCache } from './cache'
28
export type { HistoryOptions, Nullable, Options, SearchParams } from './defs'
39
export * from './parsers'

packages/nuqs/src/tests/cache.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { expectError, expectNotAssignable, expectType } from 'tsd'
22
import {
3-
createSearchParamsCache,
43
parseAsBoolean,
54
parseAsInteger,
65
parseAsString
76
} from '../../dist/server'
7+
import { createSearchParamsCache } from '../../dist/server/cache'
88

99
{
1010
const cache = createSearchParamsCache({

0 commit comments

Comments
 (0)