Skip to content

Commit e32d8da

Browse files
committed
doc: Add new path for the cache feature
1 parent 402ae5d commit e32d8da

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
@@ -124,7 +124,7 @@ The loader function will accept the following input types to parse search params
124124
## Cache
125125

126126
<Callout>
127-
This feature is available for Next.js only.
127+
The `next/server/cache` feature is available for Next.js app router only.
128128
</Callout>
129129

130130
If you wish to access the searchParams in a deeply nested Server Component
@@ -135,12 +135,9 @@ Think of it as a loader combined with a way to propagate the parsed values down
135135
the RSC tree, like Context would on the client.
136136

137137
```ts title="searchParams.ts"
138-
import {
139-
createSearchParamsCache,
140-
parseAsInteger,
141-
parseAsString
142-
} from 'nuqs/server'
143-
// Note: import from 'nuqs/server' to avoid the "use client" directive
138+
import { createSearchParamsCache } from 'nuqs/server/cache'
139+
import { parseAsInteger, parseAsString } from 'nuqs/server'
140+
// Note: import parsers from 'nuqs/server' to avoid the "use client" directive
144141

145142
export const searchParamsCache = createSearchParamsCache({
146143
// List your search param keys and associated parsers here:
@@ -176,17 +173,24 @@ function Results() {
176173
}
177174
```
178175

176+
<Callout type="warn" title="Deprecation notice">
177+
The cache feature is also accessible from `nuqs/server` in nuqs@^2, but
178+
will be removed from that import in nuqs@3.0.0.
179+
Please update your imports to `nuqs/server/cache` for a smoother transition.
180+
181+
This is to allow non-Next.js server code to use the `nuqs/server` import
182+
without having to install React canary for the `cache` function.
183+
</Callout>
184+
179185
The cache will only be valid for the current page render
180186
(see React's [`cache`](https://react.dev/reference/react/cache) function).
181187

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

185191
```ts title="searchParams.ts"
186-
import {
187-
parseAsFloat,
188-
createSearchParamsCache
189-
} from 'nuqs/server'
192+
import { parseAsFloat } from 'nuqs/server'
193+
import { createSearchParamsCache } from 'nuqs/server/cache'
190194

191195
export const coordinatesParsers = {
192196
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 {
39
HistoryOptions,

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)