@rsc-kit/mcp 0.41.0 → 0.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/recipes.js +12 -2
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +206 -6
- package/guides/api-routes.md +159 -215
- package/guides/authorization.md +103 -114
- package/guides/backend-answered-pages.md +301 -252
- package/guides/bap-vs-inertia.md +67 -25
- package/guides/blueprints.md +300 -169
- package/guides/bun.md +42 -25
- package/guides/caching.md +46 -49
- package/guides/coming-from-inertia.md +2 -2
- package/guides/commands.md +251 -200
- package/guides/connection.md +50 -54
- package/guides/content-security-policy.md +133 -84
- package/guides/domains.md +190 -149
- package/guides/edge-caching.md +242 -268
- package/guides/emails.md +96 -45
- package/guides/errors.md +116 -137
- package/guides/feature-flags.md +100 -30
- package/guides/file-uploads.md +101 -26
- package/guides/fonts.md +139 -110
- package/guides/forms.md +520 -542
- package/guides/images.md +87 -28
- package/guides/index.json +13 -33
- package/guides/instrumentation.md +134 -95
- package/guides/isr.md +3 -3
- package/guides/live-data-in-production.md +695 -0
- package/guides/live-data.md +170 -839
- package/guides/mcp.md +83 -59
- package/guides/metadata.md +124 -179
- package/guides/navigation.md +118 -147
- package/guides/no-javascript.md +49 -57
- package/guides/offline.md +118 -153
- package/guides/openapi.md +100 -35
- package/guides/pages.md +160 -50
- package/guides/ppr.md +178 -256
- package/guides/pwa.md +251 -218
- package/guides/queries.md +171 -300
- package/guides/react-compiler.md +90 -50
- package/guides/redirects.md +141 -177
- package/guides/response-headers.md +76 -74
- package/guides/route-interception.md +112 -111
- package/guides/routing.md +216 -322
- package/guides/sections.md +63 -66
- package/guides/seo-files.md +127 -65
- package/guides/server-actions.md +285 -357
- package/guides/static-generation.md +131 -199
- package/guides/testing.md +269 -147
- package/guides/third-party-scripts.md +94 -47
- package/guides/typed-routes.md +115 -113
- package/guides/url-validation.md +113 -61
- package/guides/validation.md +53 -62
- package/guides/view-transitions.md +159 -132
- package/guides/where-it-runs.md +226 -162
- package/package.json +2 -2
- package/guides/admin.md +0 -520
- package/guides/deployment.md +0 -598
- package/guides/go.md +0 -539
- package/guides/laravel.md +0 -787
- package/guides/your-own-backend.md +0 -602
package/guides/routing.md
CHANGED
|
@@ -1,298 +1,279 @@
|
|
|
1
1
|
# Routing
|
|
2
2
|
|
|
3
|
-
> File-based routes, layouts, loading states and parallel slots.
|
|
3
|
+
> File-based routes, route parameters, layouts, loading states and parallel slots.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
no route table to keep in sync with the files.
|
|
5
|
+
A route is a folder with a `page.tsx` in it. The build reads your source tree
|
|
6
|
+
and finds every route itself, so there is no route file to register them in.
|
|
8
7
|
|
|
9
|
-
##
|
|
8
|
+
## Defining a route
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
The default source directory is `src/app`. A `page.tsx` there serves `/`:
|
|
11
|
+
|
|
12
|
+
```tsx title="src/app/page.tsx"
|
|
13
|
+
export default function HomePage() {
|
|
14
|
+
return <h1>Welcome</h1>;
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Every folder below it adds a segment to the URL:
|
|
13
19
|
|
|
14
20
|
```text
|
|
15
21
|
src/app/
|
|
16
|
-
layout.tsx
|
|
17
|
-
|
|
18
|
-
page.tsx GET /
|
|
19
|
-
@sidebar/
|
|
20
|
-
page.tsx parallel slot "sidebar"
|
|
22
|
+
layout.tsx wraps every page
|
|
23
|
+
page.tsx /
|
|
21
24
|
about/
|
|
22
|
-
page.tsx
|
|
25
|
+
page.tsx /about
|
|
23
26
|
docs/
|
|
24
|
-
layout.tsx
|
|
25
|
-
page.tsx
|
|
26
|
-
sidebar.tsx
|
|
27
|
+
layout.tsx wraps every page under /docs
|
|
28
|
+
page.tsx /docs
|
|
29
|
+
sidebar.tsx an ordinary component, not a route
|
|
27
30
|
[slug]/
|
|
28
|
-
page.tsx
|
|
31
|
+
page.tsx /docs/:slug
|
|
29
32
|
```
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
elsewhere — everything below works the same, only the prefix differs.
|
|
34
|
-
</Aside>
|
|
35
|
-
|
|
36
|
-
### Special files
|
|
34
|
+
Only a file named `page.tsx` makes a route. Anything else in the folder can be
|
|
35
|
+
imported by the pages beside it and is never served.
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
- `layout.tsx` — wraps every page in the same directory and below. Receives `children`.
|
|
40
|
-
- `loading.tsx` — Suspense fallback. Wraps the page in `<Suspense>` automatically. Hierarchical: the nearest one to the page wins.
|
|
41
|
-
- `default.tsx` — what a parallel slot renders when no page matches it.
|
|
42
|
-
- `middleware.ts` — runs before anything at or below the directory renders, on every path. Not a component: it returns nothing and decides, by redirecting or throwing. Composes up the tree like layouts. See [Authorization](/guides/authorization).
|
|
43
|
-
- `@folder/` — a parallel route slot. Rendered as a named prop on the layout that declares it. Adds no URL segment.
|
|
37
|
+
To keep your pages somewhere else, set `sourceDir` in the plugin options.
|
|
44
38
|
|
|
45
|
-
|
|
46
|
-
and layouts, but not a route.
|
|
39
|
+
## Dynamic segments
|
|
47
40
|
|
|
48
|
-
|
|
41
|
+
Put a folder name in square brackets to capture that part of the URL. The page
|
|
42
|
+
receives it in `params`:
|
|
49
43
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
```tsx title="src/app/docs/[slug]/page.tsx"
|
|
54
|
-
export default async function DocsPage({ params }: { params: Promise<{ slug: string }> }) {
|
|
44
|
+
```tsx title="src/app/posts/[slug]/page.tsx"
|
|
45
|
+
export default async function PostPage({ params }: { params: Promise<{ slug: string }> }) {
|
|
55
46
|
const { slug } = await params;
|
|
56
47
|
|
|
57
48
|
return <h1>{slug}</h1>;
|
|
58
49
|
}
|
|
59
50
|
```
|
|
60
51
|
|
|
61
|
-
`params` is a promise
|
|
62
|
-
|
|
63
|
-
the route matches. Reading it synchronously would force a fresh render for every
|
|
64
|
-
visitor. `searchParams` works the same way.
|
|
52
|
+
`params` is a promise. Await it in the component that needs the value, so the
|
|
53
|
+
rest of the page can render without waiting for it.
|
|
65
54
|
|
|
66
|
-
|
|
55
|
+
A route can take several parameters, one per folder:
|
|
67
56
|
|
|
68
|
-
|
|
69
|
-
|
|
57
|
+
```text
|
|
58
|
+
src/app/teams/[team]/members/[member]/page.tsx /teams/:team/members/:member
|
|
59
|
+
```
|
|
70
60
|
|
|
71
|
-
|
|
72
|
-
URL: `app/(home)/page.tsx` and `app/(preview)/(home)/page.tsx` are both `/`.
|
|
73
|
-
That stops the dev server and the build with an error naming both files, so
|
|
74
|
-
one of them can't silently go unreachable. Give one a directory that is part
|
|
75
|
-
of the URL, like `app/(preview)/home/page.tsx`.
|
|
61
|
+
### Catch-all segments
|
|
76
62
|
|
|
77
|
-
|
|
63
|
+
`[...path]` captures the rest of the URL as an array:
|
|
78
64
|
|
|
79
|
-
|
|
80
|
-
|
|
65
|
+
```tsx title="src/app/files/[...path]/page.tsx"
|
|
66
|
+
export default async function FilePage({ params }: { params: Promise<{ path: string[] }> }) {
|
|
67
|
+
const { path } = await params; // /files/a/b/c → ['a', 'b', 'c']
|
|
81
68
|
|
|
82
|
-
|
|
83
|
-
|
|
69
|
+
return <h1>{path.join('/')}</h1>;
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Query strings
|
|
74
|
+
|
|
75
|
+
A server component reads the query string from `searchParams`, which is a
|
|
76
|
+
promise in the same way:
|
|
84
77
|
|
|
85
|
-
|
|
86
|
-
|
|
78
|
+
```tsx title="src/app/search/page.tsx"
|
|
79
|
+
export default async function SearchPage({ searchParams }: { searchParams: Promise<{ q?: string }> }) {
|
|
80
|
+
const { q } = await searchParams;
|
|
87
81
|
|
|
82
|
+
return <h1>Results for {q}</h1>;
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
A client component uses [`useSearchParams`](#reading-the-url-in-a-client-component)
|
|
87
|
+
instead.
|
|
88
|
+
|
|
89
|
+
## Layouts
|
|
90
|
+
|
|
91
|
+
A `layout.tsx` wraps every page in its folder and the folders below it. It
|
|
92
|
+
receives the page as `children`:
|
|
93
|
+
|
|
94
|
+
```tsx title="src/app/layout.tsx"
|
|
95
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
88
96
|
return (
|
|
89
|
-
<
|
|
90
|
-
<
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
97
|
+
<html lang="en">
|
|
98
|
+
<body>
|
|
99
|
+
<nav>…</nav>
|
|
100
|
+
{children}
|
|
101
|
+
</body>
|
|
102
|
+
</html>
|
|
94
103
|
);
|
|
95
104
|
}
|
|
96
105
|
```
|
|
97
106
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
107
|
+
Layouts nest. `/docs/routing` renders inside `docs/layout.tsx`, which renders
|
|
108
|
+
inside the root layout. Moving between two pages that share a layout keeps
|
|
109
|
+
that layout as it is, without rendering it again, so a half-typed search box in
|
|
110
|
+
it survives the navigation.
|
|
101
111
|
|
|
102
|
-
##
|
|
112
|
+
## Route groups
|
|
103
113
|
|
|
104
|
-
|
|
105
|
-
|
|
114
|
+
A folder name in parentheses groups routes without adding to the URL. Use it to
|
|
115
|
+
give some pages a different layout:
|
|
106
116
|
|
|
107
|
-
|
|
117
|
+
```text
|
|
118
|
+
src/app/
|
|
119
|
+
(marketing)/
|
|
120
|
+
layout.tsx wraps /pricing and /about only
|
|
121
|
+
pricing/
|
|
122
|
+
page.tsx /pricing
|
|
123
|
+
about/
|
|
124
|
+
page.tsx /about
|
|
125
|
+
(app)/
|
|
126
|
+
layout.tsx wraps /dashboard only
|
|
127
|
+
dashboard/
|
|
128
|
+
page.tsx /dashboard
|
|
129
|
+
```
|
|
108
130
|
|
|
109
|
-
|
|
110
|
-
|
|
131
|
+
Two groups can't put a page at the same URL. `(home)/page.tsx` and
|
|
132
|
+
`(preview)/(home)/page.tsx` are both `/`, and the dev server and the build stop
|
|
133
|
+
with an error naming both files.
|
|
111
134
|
|
|
112
|
-
|
|
113
|
-
"use client";
|
|
135
|
+
## Showing a loading state
|
|
114
136
|
|
|
115
|
-
|
|
137
|
+
Wrap the part of a page that waits in `<Suspense>`. Everything outside it
|
|
138
|
+
renders straight away, and the fallback shows until the waiting part is ready:
|
|
139
|
+
|
|
140
|
+
```tsx title="src/app/dashboard/page.tsx"
|
|
141
|
+
import { Suspense } from 'react';
|
|
142
|
+
|
|
143
|
+
export default function DashboardPage() {
|
|
144
|
+
return (
|
|
145
|
+
<>
|
|
146
|
+
<h1>Dashboard</h1>
|
|
147
|
+
<Suspense fallback={<Skeleton className="h-24" />}>
|
|
148
|
+
<Revenue />
|
|
149
|
+
</Suspense>
|
|
150
|
+
</>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
116
153
|
|
|
117
|
-
|
|
118
|
-
const
|
|
154
|
+
async function Revenue() {
|
|
155
|
+
const amount = await revenue();
|
|
119
156
|
|
|
120
|
-
return <
|
|
157
|
+
return <p>Revenue: {amount}</p>;
|
|
121
158
|
}
|
|
122
159
|
```
|
|
123
160
|
|
|
124
|
-
|
|
125
|
-
|
|
161
|
+
Give each part its own boundary, so a slow one doesn't hold up the rest. The
|
|
162
|
+
heading above is in the HTML the build stores, and only `Revenue` waits.
|
|
163
|
+
|
|
164
|
+
A `loading.tsx` beside a page wraps that whole page in one boundary. It suits a
|
|
165
|
+
page where nothing is worth showing until all of it is ready. Don't put one in
|
|
166
|
+
the root folder: it becomes the fallback for every page in the app, including
|
|
167
|
+
the ones that should have shown their own. [Partial prerendering](/guides/ppr#where-to-put-the-boundaries)
|
|
168
|
+
explains where boundaries go.
|
|
169
|
+
|
|
170
|
+
## Special files
|
|
171
|
+
|
|
172
|
+
| File | What it does |
|
|
173
|
+
| --- | --- |
|
|
174
|
+
| `page.tsx` | Makes the folder a route. |
|
|
175
|
+
| `layout.tsx` | Wraps the pages in its folder and below. Receives `children`. |
|
|
176
|
+
| `loading.tsx` | Wraps the page beside it in `<Suspense>` with this fallback. |
|
|
177
|
+
| `error.tsx` | What renders when the page throws. See [Errors](/guides/errors). |
|
|
178
|
+
| `not-found.tsx` | What renders for `notFound()` and unknown URLs. |
|
|
179
|
+
| `default.tsx` | What a parallel slot renders when nothing matches it. |
|
|
180
|
+
| `middleware.ts` | Runs before anything in its folder and below renders. See [Authorization](/guides/authorization). |
|
|
181
|
+
| `route.ts` | An API route instead of a page. See [API routes](/guides/api-routes). |
|
|
126
182
|
|
|
127
|
-
##
|
|
183
|
+
## Linking between pages
|
|
128
184
|
|
|
129
|
-
`Link`
|
|
130
|
-
without a reload.
|
|
185
|
+
`Link` navigates without a full page reload:
|
|
131
186
|
|
|
132
187
|
```tsx
|
|
133
188
|
import Link from '@rsc-kit/core/Link';
|
|
134
189
|
|
|
135
|
-
<Link href="/
|
|
190
|
+
<Link href="/about">About</Link>
|
|
191
|
+
```
|
|
136
192
|
|
|
137
|
-
|
|
138
|
-
|
|
193
|
+
Only the part of the page below the layouts both pages share is fetched and
|
|
194
|
+
rendered.
|
|
139
195
|
|
|
140
|
-
|
|
196
|
+
```tsx
|
|
197
|
+
// Start loading the page as soon as the link is on screen
|
|
141
198
|
<Link href="/dashboard" prefetch="mount">Dashboard</Link>
|
|
142
199
|
|
|
143
200
|
// Keep the scroll position instead of jumping to the top
|
|
144
201
|
<Link href="/docs/rsc" preserveScroll>Stay here</Link>
|
|
202
|
+
|
|
203
|
+
// Replace the history entry instead of adding one
|
|
204
|
+
<Link href="/settings" replace>Settings</Link>
|
|
145
205
|
```
|
|
146
206
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
207
|
+
| Prop | Default | What it does |
|
|
208
|
+
| --- | --- | --- |
|
|
209
|
+
| `href` | | Where to go. A URL on another site is left to the browser. |
|
|
210
|
+
| `prefetch` | `"hover"` | When to load the page ahead of the click: `"hover"`, `"mount"`, `"click"` or `"none"`. |
|
|
211
|
+
| `cacheFor` | `30000` | How long a prefetched page stays usable, in milliseconds. |
|
|
212
|
+
| `replace` | `false` | Replace the history entry instead of adding one. |
|
|
213
|
+
| `preserveScroll` | `false` | Stay where you are instead of scrolling to the top. |
|
|
151
214
|
|
|
152
|
-
|
|
215
|
+
Any other prop, `ref` included, is passed to the `<a>`.
|
|
216
|
+
|
|
217
|
+
To navigate from code, call `visit`:
|
|
153
218
|
|
|
154
219
|
```tsx
|
|
155
220
|
"use client";
|
|
156
|
-
import { visit
|
|
221
|
+
import { visit } from '@rsc-kit/core/router';
|
|
157
222
|
|
|
158
|
-
await visit('/
|
|
159
|
-
await visit('/dashboard', { replace: true }); // no back-button entry
|
|
160
|
-
prefetch('/settings'); // warm the cache
|
|
223
|
+
await visit('/dashboard', { replace: true });
|
|
161
224
|
```
|
|
162
225
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
### Link props
|
|
226
|
+
[Navigation](/guides/navigation) covers prefetching, progress and refreshing
|
|
227
|
+
the current page.
|
|
166
228
|
|
|
167
|
-
|
|
168
|
-
| --- | --- | --- | --- |
|
|
169
|
-
| `href` | `string` | — | The destination. An off-origin URL is left to the browser. |
|
|
170
|
-
| `prefetch` | `"hover" \| "mount" \| "click" \| "none"` | `"hover"` | When to warm the cache. `hover` fetches the payload as the link comes into view, on every device, when the browser is idle, and again when it comes back into view or the tab does once the payload has expired — and decodes it, which loads the page's chunks, on intent: a pointer settled for 100 ms (so crossing a nav bar decodes nothing), a press, a touch. A click then finds everything there — [Navigation](/guides/navigation#what-a-click-finds-already-there). `true`/`false` are accepted as aliases for `hover`/`none`. |
|
|
171
|
-
| `cacheFor` | `number` | `30000` | How long a prefetched payload stays usable, in milliseconds. |
|
|
172
|
-
| `replace` | `boolean` | `false` | Replace the history entry instead of pushing one. |
|
|
173
|
-
| `preserveScroll` | `boolean` | `false` | Stay where you are instead of scrolling to the top. |
|
|
229
|
+
### Showing that a link is loading
|
|
174
230
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
### Loading state
|
|
179
|
-
|
|
180
|
-
Each `Link` carries a `data-pending` attribute while its navigation is in
|
|
181
|
-
flight, which is usually enough on its own:
|
|
231
|
+
A `Link` has a `data-pending` attribute while its page loads:
|
|
182
232
|
|
|
183
233
|
```css
|
|
184
234
|
a[data-pending] {
|
|
185
235
|
opacity: 0.5;
|
|
186
|
-
pointer-events: none;
|
|
187
236
|
}
|
|
188
237
|
```
|
|
189
238
|
|
|
190
|
-
|
|
191
|
-
parent `Link`'s state:
|
|
239
|
+
To show it somewhere inside the link, use `useLinkStatus`:
|
|
192
240
|
|
|
193
241
|
```tsx
|
|
194
242
|
"use client";
|
|
195
|
-
|
|
196
243
|
import { useLinkStatus } from '@rsc-kit/core/useLinkStatus';
|
|
197
244
|
|
|
198
|
-
function
|
|
245
|
+
function NavLabel({ children }: { children: React.ReactNode }) {
|
|
199
246
|
const { pending } = useLinkStatus();
|
|
200
247
|
|
|
201
|
-
return
|
|
202
|
-
<span className={pending ? 'opacity-50' : 'opacity-100'}>
|
|
203
|
-
{children}
|
|
204
|
-
{pending && <span className="spinner" />}
|
|
205
|
-
</span>
|
|
206
|
-
);
|
|
248
|
+
return <span className={pending ? 'opacity-50' : undefined}>{children}</span>;
|
|
207
249
|
}
|
|
208
250
|
```
|
|
209
251
|
|
|
210
252
|
## Typed links
|
|
211
253
|
|
|
212
|
-
|
|
213
|
-
`href` is checked against them rather than being any old string:
|
|
254
|
+
`href` is checked against your routes, so a typo fails the typecheck:
|
|
214
255
|
|
|
215
256
|
```tsx
|
|
216
|
-
<Link href="/orders">Orders</Link>
|
|
217
257
|
<Link href="/ordres">Orders</Link>
|
|
218
258
|
// ^ Type '"/ordres"' is not assignable. Did you mean '"/orders"'?
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
For a route with params, write the url — a template literal is checked the same
|
|
222
|
-
way, including the static part around the value:
|
|
223
|
-
|
|
224
|
-
```tsx
|
|
225
|
-
<Link href={`/posts/${post.slug}`}>Read</Link>
|
|
226
|
-
<Link href={`/postz/${post.slug}`}>Read</Link>
|
|
227
|
-
// ^ Type '`/postz/${string}`' is not assignable
|
|
228
|
-
```
|
|
229
259
|
|
|
230
|
-
|
|
231
|
-
`action`. Nothing to
|
|
232
|
-
configure and no flag — the file is regenerated on every build, so deleting a
|
|
233
|
-
page makes every link to it stop compiling.
|
|
234
|
-
|
|
235
|
-
One thing you will hit immediately: a list of links widens to `string` unless
|
|
236
|
-
you say otherwise. `satisfies` keeps each literal *and* checks it, so the typo
|
|
237
|
-
fails at the list rather than at the `Link` that renders it:
|
|
238
|
-
|
|
239
|
-
```tsx title="src/components/Nav.tsx"
|
|
240
|
-
// `satisfies` rather than a type annotation: an annotation would widen href to
|
|
241
|
-
// Route and lose which one each entry is, while this keeps the literals and
|
|
242
|
-
// still checks them — so a typo fails here, at the list, rather than at the
|
|
243
|
-
// Link that renders it.
|
|
244
|
-
const links = [
|
|
245
|
-
{ href: '/', label: 'Home' },
|
|
246
|
-
{ href: '/dashboard', label: 'Dashboard' },
|
|
247
|
-
{ href: '/posts/hello-world', label: 'A Post' },
|
|
248
|
-
] satisfies { href: Route; label: string }[]
|
|
260
|
+
<Link href={`/posts/${post.slug}`}>Read</Link> // checked too
|
|
249
261
|
```
|
|
250
262
|
|
|
251
|
-
|
|
263
|
+
The same goes for `redirect()`, `visit()` and `Form`'s `action`. The build
|
|
264
|
+
writes the types to `.rsc-kit/rsc-routes.d.ts`, so a deleted page makes every
|
|
265
|
+
link to it stop compiling.
|
|
252
266
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
title used as an identifier quietly produces a url that means something else:
|
|
256
|
-
|
|
257
|
-
```ts
|
|
258
|
-
`/posts/${'Q3 report / draft?v=2'}`
|
|
259
|
-
// '/posts/Q3 report / draft?v=2' — three segments and a query string
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
Encode it in the template, the same as anywhere else. There is no url builder
|
|
263
|
-
to reach for and nothing new to remember:
|
|
264
|
-
|
|
265
|
-
```tsx
|
|
266
|
-
<Link href={`/posts/${encodeURIComponent(post.title)}`}>Read</Link>
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
<Aside type="note" title="Why there is no route() helper">
|
|
270
|
-
The url *is* the file path, and a template literal is already checked against
|
|
271
|
-
it. A builder would only give you a second way to write the same line.
|
|
272
|
-
</Aside>
|
|
273
|
-
|
|
274
|
-
When a destination is computed rather than written, cast it — that is the seam
|
|
275
|
-
where you are telling the typechecker something it cannot know:
|
|
276
|
-
|
|
277
|
-
```ts
|
|
278
|
-
import type { Route } from '@rsc-kit/core/routes';
|
|
279
|
-
|
|
280
|
-
<Link href={savedPath as Route}>Resume</Link>
|
|
281
|
-
```
|
|
267
|
+
[Typed routes](/guides/typed-routes) covers lists of links, encoding a value
|
|
268
|
+
into a URL, and a URL computed at runtime.
|
|
282
269
|
|
|
283
|
-
|
|
284
|
-
cannot say "no slashes here" — so `/posts/a/b` type-checks against
|
|
285
|
-
`/posts/[slug]`. And before your first build there are no route types yet, so
|
|
286
|
-
every href falls back to `string`.
|
|
270
|
+
## Reading the URL in a client component
|
|
287
271
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
`usePathname` and `useSearchParams` read the current url and follow
|
|
291
|
-
navigations:
|
|
272
|
+
`usePathname` and `useSearchParams` read the current URL and update when it
|
|
273
|
+
changes:
|
|
292
274
|
|
|
293
275
|
```tsx title="src/components/Filters.tsx"
|
|
294
276
|
"use client";
|
|
295
|
-
|
|
296
277
|
import { useSearchParams } from '@rsc-kit/core/useSearchParams';
|
|
297
278
|
|
|
298
279
|
export function Filters() {
|
|
@@ -302,178 +283,91 @@ export function Filters() {
|
|
|
302
283
|
}
|
|
303
284
|
```
|
|
304
285
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
A **query string** is not — the same route is asked for with `?q=shoes` and
|
|
309
|
-
`?q=hats` — so what `useSearchParams` does on the server depends on whether
|
|
310
|
-
the HTML can be stored:
|
|
311
|
-
|
|
312
|
-
- **Rendered for a request** — a page rendered per visitor, or the hole of a
|
|
313
|
-
stored shell filled per request — it answers with that request's query. The
|
|
314
|
-
component renders on the server like any other, with no flash, and a form
|
|
315
|
-
that keeps its state in the url (a folder picker on `?root=`) arrives
|
|
316
|
-
filled in.
|
|
317
|
-
- **Rendered at build**, for a page or shell that is stored, there is no honest
|
|
318
|
-
answer, and it throws rather than pretending the query is empty.
|
|
319
|
-
|
|
320
|
-
The dev server renders every page per request, so in development it always
|
|
321
|
-
answers. A read in the stored part of a page still shows its fallback first in
|
|
322
|
-
a build, and the build says so: under a `<Suspense>` you wrote, nothing is
|
|
323
|
-
said; with nothing closer than a `loading.tsx`, one line naming the component.
|
|
324
|
-
|
|
325
|
-
Where the page is stored, wrap it, and the throw becomes the fallback:
|
|
286
|
+
On a page the build stores, the query string isn't known yet, so wrap the
|
|
287
|
+
component in `<Suspense>`. The stored page shows the fallback, and the browser
|
|
288
|
+
fills in the real value:
|
|
326
289
|
|
|
327
290
|
```tsx title="src/app/search/page.tsx"
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
<Filters />
|
|
332
|
-
</Suspense>
|
|
333
|
-
);
|
|
334
|
-
}
|
|
291
|
+
<Suspense fallback={<p>Loading filters…</p>}>
|
|
292
|
+
<Filters />
|
|
293
|
+
</Suspense>
|
|
335
294
|
```
|
|
336
295
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
<Aside type="note" title="Without a boundary it still works, and that is the problem">
|
|
341
|
-
A root `loading.tsx` catches it, so the page is stored showing the fallback
|
|
342
|
-
the whole app shares — for a search page, the word "Loading" where the query
|
|
343
|
-
should be. The build says so rather than leaving you to find it:
|
|
344
|
-
|
|
345
|
-
```text
|
|
346
|
-
○ /search
|
|
347
|
-
⚠ nothing painted without the root loading.tsx — the fallback the whole
|
|
348
|
-
app shares is standing in for this page. Put a boundary where the
|
|
349
|
-
waiting is.
|
|
350
|
-
```
|
|
351
|
-
</Aside>
|
|
352
|
-
|
|
353
|
-
For a **server** component, the query arrives as a prop instead — `searchParams`,
|
|
354
|
-
awaited like `params`. Reach for the hook only when the component is
|
|
355
|
-
`"use client"` and has to follow navigations.
|
|
296
|
+
A page rendered for each request answers with that request's query string, and
|
|
297
|
+
there is no fallback. `usePathname` needs no boundary: the path is known for a
|
|
298
|
+
stored page.
|
|
356
299
|
|
|
357
300
|
## Search params as state
|
|
358
301
|
|
|
359
|
-
[nuqs](https://nuqs.dev)
|
|
360
|
-
in the
|
|
302
|
+
For a filter or a tab kept in the URL, use [nuqs](https://nuqs.dev) with this
|
|
303
|
+
adapter in the root layout:
|
|
361
304
|
|
|
362
305
|
```tsx title="src/app/layout.tsx"
|
|
363
|
-
import { NuqsAdapter } from '@rsc-kit/core/nuqs'
|
|
306
|
+
import { NuqsAdapter } from '@rsc-kit/core/nuqs';
|
|
364
307
|
|
|
365
|
-
export default function RootLayout({ children }) {
|
|
366
|
-
return <NuqsAdapter>{children}</NuqsAdapter
|
|
308
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
309
|
+
return <NuqsAdapter>{children}</NuqsAdapter>;
|
|
367
310
|
}
|
|
368
311
|
```
|
|
369
312
|
|
|
370
313
|
```tsx
|
|
371
|
-
const [kind, setKind] = useQueryState('kind')
|
|
314
|
+
const [kind, setKind] = useQueryState('kind');
|
|
372
315
|
```
|
|
373
316
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
in place and every bit of client state survives. On a page rendered per request, a
|
|
381
|
-
`useQueryState` renders on the server with the visitor's query, as
|
|
382
|
-
`useSearchParams` does - nuqs reads through it. It also tells our own
|
|
383
|
-
`useSearchParams` about a shallow change — that hook listens for a navigation
|
|
384
|
-
event rather than watching history, so an adapter that only writes the url
|
|
385
|
-
would leave the rest of the page reading the old query.
|
|
386
|
-
|
|
387
|
-
`nuqs` is an optional peer dependency. Nothing else imports it.
|
|
388
|
-
|
|
389
|
-
:::note[Where this should live]
|
|
390
|
-
In nuqs, alongside its adapters for Next, Remix and TanStack Router — that is
|
|
391
|
-
how every other framework's works, and the `custom` API this is built on is
|
|
392
|
-
their escape hatch for a framework that does not have one in-tree yet. It is
|
|
393
|
-
here until there are enough people using it to make that contribution a
|
|
394
|
-
reasonable ask. When `nuqs/adapters/rsc-kit` exists, this import will point at
|
|
395
|
-
it and then be removed.
|
|
396
|
-
:::
|
|
317
|
+
With `shallow: false`, a change renders the server components again with the
|
|
318
|
+
new query, and client state on the page is kept. nuqs's own
|
|
319
|
+
`nuqs/adapters/react` reloads the whole page in that case.
|
|
320
|
+
|
|
321
|
+
`nuqs` is an optional peer dependency. Once nuqs ships an adapter for rsc-kit,
|
|
322
|
+
this import will point to it.
|
|
397
323
|
|
|
398
324
|
## Parallel routes
|
|
399
325
|
|
|
400
|
-
A
|
|
401
|
-
|
|
402
|
-
innermost one.
|
|
326
|
+
A folder whose name starts with `@` is a slot. It renders beside the page and is
|
|
327
|
+
passed to the layout in the same folder as a prop of that name:
|
|
403
328
|
|
|
404
329
|
```text
|
|
405
330
|
src/app/
|
|
406
|
-
layout.tsx receives { children, sidebar
|
|
407
|
-
page.tsx
|
|
331
|
+
layout.tsx receives { children, sidebar }
|
|
332
|
+
page.tsx children
|
|
408
333
|
@sidebar/
|
|
409
|
-
page.tsx
|
|
410
|
-
@modal/
|
|
411
|
-
default.tsx arrives as "modal" — empty until something fills it
|
|
334
|
+
page.tsx sidebar
|
|
412
335
|
```
|
|
413
336
|
|
|
414
337
|
```tsx title="src/app/layout.tsx"
|
|
415
|
-
export default function Layout({
|
|
416
|
-
children,
|
|
417
|
-
sidebar,
|
|
418
|
-
modal,
|
|
419
|
-
}: {
|
|
338
|
+
export default function Layout({ children, sidebar }: {
|
|
420
339
|
children: React.ReactNode;
|
|
421
340
|
sidebar: React.ReactNode;
|
|
422
|
-
modal: React.ReactNode;
|
|
423
341
|
}) {
|
|
424
342
|
return (
|
|
425
343
|
<div className="flex">
|
|
426
|
-
<aside
|
|
427
|
-
<main
|
|
428
|
-
{modal}
|
|
344
|
+
<aside>{sidebar}</aside>
|
|
345
|
+
<main>{children}</main>
|
|
429
346
|
</div>
|
|
430
347
|
);
|
|
431
348
|
}
|
|
432
349
|
```
|
|
433
350
|
|
|
434
|
-
A
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
## Streaming
|
|
439
|
-
|
|
440
|
-
On a full page load the HTML streams. React flushes the shell — layouts, static
|
|
441
|
-
markup, and every Suspense fallback in it — immediately, and injects the real
|
|
442
|
-
content as each boundary resolves.
|
|
443
|
-
|
|
444
|
-
SPA navigations stream too: the Flight payload is read with
|
|
445
|
-
`createFromReadableStream()` as it arrives, so React reconciles progressively
|
|
446
|
-
rather than waiting for the last byte.
|
|
351
|
+
A slot adds nothing to the URL. Give it a `default.tsx` for the URLs it has no
|
|
352
|
+
page for. Render every slot the layout receives: one that isn't rendered is
|
|
353
|
+
left out of the page without a warning.
|
|
447
354
|
|
|
448
|
-
|
|
355
|
+
[Route interception](/guides/route-interception) uses slots to open a page in a
|
|
356
|
+
modal.
|
|
449
357
|
|
|
450
|
-
|
|
358
|
+
## When the build can't store a route
|
|
451
359
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
| A redirect | The route only redirects. The redirect itself is stored — status and location — and served without rendering. |
|
|
457
|
-
|
|
458
|
-
There is no fourth outcome. A route that could not be stored, and did not say
|
|
459
|
-
it wanted to be rendered per request, **fails the build** and names the fix:
|
|
360
|
+
The build renders every route and stores what it can: the whole page, or a
|
|
361
|
+
shell with the waiting parts filled in per request. A route that reads
|
|
362
|
+
`params`, cookies or headers before anything can paint has nothing to store,
|
|
363
|
+
and the build fails with the fix:
|
|
460
364
|
|
|
461
365
|
```text
|
|
462
366
|
Some routes could not be prerendered:
|
|
463
367
|
|
|
464
368
|
/photo/[id] — renders its params before it can paint, and lists no urls to build
|
|
465
|
-
|
|
466
|
-
Each one reads request data — params, headers, cookies, or the host —
|
|
467
|
-
above every Suspense boundary, so nothing can paint without it.
|
|
468
|
-
|
|
469
|
-
Put the part that waits inside <Suspense>, or add a loading.tsx beside
|
|
470
|
-
the page, so there is something to store while the rest arrives.
|
|
471
369
|
```
|
|
472
370
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
See [Static generation](/guides/static-generation) for listing the urls of a
|
|
478
|
-
parameterised route, and [Partial prerendering](/guides/ppr) for where the
|
|
479
|
-
boundaries go.
|
|
371
|
+
Wrap the part that reads the request in `<Suspense>`, or list the URLs to build
|
|
372
|
+
with `generateStaticParams`. [Static generation](/guides/static-generation)
|
|
373
|
+
and [Partial prerendering](/guides/ppr) cover both.
|