@rsc-kit/mcp 0.41.0 → 0.42.1
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 +18 -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 +270 -216
- 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/react-compiler.md
CHANGED
|
@@ -3,49 +3,64 @@
|
|
|
3
3
|
> Enabling the compiler in the build.
|
|
4
4
|
|
|
5
5
|
The React Compiler memoises client components for you, so `useMemo`,
|
|
6
|
-
`useCallback` and `React.memo` mostly stop being things you write.
|
|
6
|
+
`useCallback` and `React.memo` mostly stop being things you write. Turn it on
|
|
7
|
+
in the React plugin, after `rscKit()`:
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
```ts title="vite.config.ts"
|
|
10
|
+
import { defineConfig } from 'vite';
|
|
11
|
+
import react from '@vitejs/plugin-react';
|
|
12
|
+
import { rscKit } from '@rsc-kit/core/vite';
|
|
13
|
+
import { nitro } from 'nitro/vite';
|
|
14
|
+
|
|
15
|
+
export default defineConfig({
|
|
16
|
+
plugins: [
|
|
17
|
+
nitro({ preset: 'bun', serveStatic: 'inline' }),
|
|
18
|
+
rscKit({ sourceDir: 'src' }),
|
|
19
|
+
react({ compiler: true }),
|
|
20
|
+
],
|
|
21
|
+
});
|
|
22
|
+
```
|
|
11
23
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
any React layer transforms them. The React plugin goes **after** it.
|
|
15
|
-
</Aside>
|
|
24
|
+
Nothing in rsc-kit special-cases it. The build runs your project's own Vite
|
|
25
|
+
config, so the compiler is enabled the way it is in any Vite app.
|
|
16
26
|
|
|
17
|
-
##
|
|
27
|
+
## Starting with it on
|
|
18
28
|
|
|
19
|
-
|
|
20
|
-
|
|
29
|
+
`create-rsc-kit` and `rsc-kit init` ask *React Compiler* and default to yes.
|
|
30
|
+
To choose without the prompt:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
bun create rsc-kit@latest my-app --compiler=oxc # native, the default
|
|
34
|
+
bun create rsc-kit@latest my-app --compiler=babel # the Babel plugin
|
|
35
|
+
bun create rsc-kit@latest my-app --compiler=none # off
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The generated `oxlint` config turns on the compiler's own rules too. See
|
|
39
|
+
[Checking your code first](#checking-your-code-first).
|
|
40
|
+
|
|
41
|
+
## Adding it to an existing app
|
|
42
|
+
|
|
43
|
+
The compiler has a native implementation and a Babel one. Both produce the
|
|
44
|
+
same transform. They differ in what they cost to run and how settled they are.
|
|
21
45
|
|
|
22
46
|
### Native, through oxc
|
|
23
47
|
|
|
24
48
|
The faster path, and the least to install. `compiler: true` is
|
|
25
49
|
[experimental](https://react.dev/learn/react-compiler/installation) and needs
|
|
26
|
-
`oxc-transform-react`
|
|
50
|
+
`oxc-transform-react` installed. The plugin looks for it by name:
|
|
27
51
|
|
|
28
52
|
<PackageManagers pkg="@vitejs/plugin-react oxc-transform-react" dev />
|
|
29
53
|
|
|
30
54
|
```ts title="vite.config.ts"
|
|
31
|
-
|
|
32
|
-
import react from '@vitejs/plugin-react';
|
|
33
|
-
import { rscKit } from '@rsc-kit/core/vite';
|
|
34
|
-
|
|
35
|
-
export default defineConfig({
|
|
36
|
-
plugins: [
|
|
37
|
-
rscKit({ sourceDir: 'src' }),
|
|
38
|
-
react({ compiler: true }),
|
|
39
|
-
],
|
|
40
|
-
});
|
|
55
|
+
react({ compiler: true })
|
|
41
56
|
```
|
|
42
57
|
|
|
43
58
|
Pass an object instead of `true` to configure it.
|
|
44
59
|
|
|
45
60
|
### Babel
|
|
46
61
|
|
|
47
|
-
The reference implementation.
|
|
48
|
-
option
|
|
62
|
+
The reference implementation. `@vitejs/plugin-react` 6 removed the inline
|
|
63
|
+
`babel` option, so the preset is applied through `@rolldown/plugin-babel`:
|
|
49
64
|
|
|
50
65
|
<PackageManagers pkg="@vitejs/plugin-react @rolldown/plugin-babel babel-plugin-react-compiler" dev />
|
|
51
66
|
|
|
@@ -54,9 +69,11 @@ import { defineConfig } from 'vite';
|
|
|
54
69
|
import react, { reactCompilerPreset } from '@vitejs/plugin-react';
|
|
55
70
|
import babel from '@rolldown/plugin-babel';
|
|
56
71
|
import { rscKit } from '@rsc-kit/core/vite';
|
|
72
|
+
import { nitro } from 'nitro/vite';
|
|
57
73
|
|
|
58
74
|
export default defineConfig({
|
|
59
75
|
plugins: [
|
|
76
|
+
nitro({ preset: 'bun', serveStatic: 'inline' }),
|
|
60
77
|
rscKit({ sourceDir: 'src' }),
|
|
61
78
|
react(),
|
|
62
79
|
babel({ presets: [reactCompilerPreset()] }),
|
|
@@ -72,31 +89,34 @@ react({
|
|
|
72
89
|
})
|
|
73
90
|
```
|
|
74
91
|
|
|
75
|
-
|
|
92
|
+
### Plugin order
|
|
76
93
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
94
|
+
`rscKit()` includes `@vitejs/plugin-rsc`, which has to see modules before any
|
|
95
|
+
React layer transforms them. Put the React plugin, and the Babel plugin if you
|
|
96
|
+
use it, **after** `rscKit()`.
|
|
80
97
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
98
|
+
## Checking your code first
|
|
99
|
+
|
|
100
|
+
The compiler only memoises components it can prove are safe, and skips the
|
|
101
|
+
rest silently. Run these before you turn it on, so you know which is which.
|
|
102
|
+
|
|
103
|
+
**Type-check.** The compiler assumes your code means what its types say, and
|
|
104
|
+
an untyped `any` threading through a component is the shape it gives up on:
|
|
84
105
|
|
|
85
106
|
```bash
|
|
86
107
|
tsc --noEmit
|
|
87
108
|
```
|
|
88
109
|
|
|
89
|
-
**
|
|
90
|
-
|
|
110
|
+
**Run the health check.** It reports how many components the compiler can
|
|
111
|
+
handle, and why the others are refused:
|
|
91
112
|
|
|
92
113
|
```bash
|
|
93
114
|
npx react-compiler-healthcheck
|
|
94
115
|
```
|
|
95
116
|
|
|
96
|
-
**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
build:
|
|
117
|
+
**Turn on the lint rules.** They report the Rules of React violations that
|
|
118
|
+
make a component uncompilable, as you write rather than as silence in the
|
|
119
|
+
build. With ESLint:
|
|
100
120
|
|
|
101
121
|
```js title="eslint.config.js"
|
|
102
122
|
import reactHooks from 'eslint-plugin-react-hooks';
|
|
@@ -104,19 +124,34 @@ import reactHooks from 'eslint-plugin-react-hooks';
|
|
|
104
124
|
export default [reactHooks.configs.recommended];
|
|
105
125
|
```
|
|
106
126
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
127
|
+
With oxlint, the same rules live under `react/`. A new app's `.oxlintrc.json`
|
|
128
|
+
has them on:
|
|
129
|
+
|
|
130
|
+
```json title=".oxlintrc.json"
|
|
131
|
+
{
|
|
132
|
+
"plugins": ["typescript", "react", "unicorn"],
|
|
133
|
+
"rules": {
|
|
134
|
+
"react/rules-of-hooks": "error",
|
|
135
|
+
"react/purity": "error",
|
|
136
|
+
"react/set-state-in-render": "error",
|
|
137
|
+
"react/immutability": "error",
|
|
138
|
+
"react/preserve-manual-memoization": "error",
|
|
139
|
+
"react/exhaustive-deps": "off"
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
110
143
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
144
|
+
`exhaustive-deps` is off with the compiler, which infers dependencies itself.
|
|
145
|
+
|
|
146
|
+
**Keep StrictMode on.** Its double-invoked renders surface impure components
|
|
147
|
+
at runtime, which breaks the compiler's assumptions.
|
|
148
|
+
|
|
149
|
+
The compiler only applies to client components. A server component renders
|
|
150
|
+
once and is thrown away, so there is nothing to memoise in it.
|
|
116
151
|
|
|
117
152
|
## Confirming it ran
|
|
118
153
|
|
|
119
|
-
The compiler
|
|
154
|
+
The compiler puts a cache array at the top of every component it compiled.
|
|
120
155
|
Build without minification and look for it:
|
|
121
156
|
|
|
122
157
|
```js
|
|
@@ -130,11 +165,13 @@ function Counter() {
|
|
|
130
165
|
}
|
|
131
166
|
```
|
|
132
167
|
|
|
133
|
-
No `_c(...)` and no `memo_cache_sentinel` means
|
|
134
|
-
|
|
168
|
+
No `_c(...)` and no `memo_cache_sentinel` means the component was skipped.
|
|
169
|
+
The health check says why.
|
|
135
170
|
|
|
136
171
|
## Opting a component out
|
|
137
172
|
|
|
173
|
+
Add `"use no memo"` at the top of the component:
|
|
174
|
+
|
|
138
175
|
```tsx
|
|
139
176
|
"use client";
|
|
140
177
|
|
|
@@ -145,9 +182,12 @@ export default function LegacyWidget() {
|
|
|
145
182
|
}
|
|
146
183
|
```
|
|
147
184
|
|
|
185
|
+
Use it for a component that breaks with the compiler while you fix it, such as
|
|
186
|
+
one that mutates a value it received.
|
|
187
|
+
|
|
148
188
|
## Turning it off
|
|
149
189
|
|
|
150
|
-
|
|
151
|
-
depends on it
|
|
190
|
+
Remove `compiler: true`, or the Babel plugin. Neither the router nor the build
|
|
191
|
+
depends on it.
|
|
152
192
|
|
|
153
193
|
Further reading: [React Compiler installation](https://react.dev/learn/react-compiler/installation).
|
package/guides/redirects.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# Redirects
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Sending the visitor somewhere else from a page, a middleware or a server action.
|
|
4
|
+
|
|
5
|
+
Call `redirect()` to send the visitor to another URL:
|
|
4
6
|
|
|
5
7
|
```tsx title="src/app/products/[slug]/page.tsx"
|
|
6
8
|
import { redirect } from '@rsc-kit/core/redirect';
|
|
@@ -16,169 +18,92 @@ export default async function ProductPage({ params }: { params: Promise<{ slug:
|
|
|
16
18
|
}
|
|
17
19
|
```
|
|
18
20
|
|
|
19
|
-
`redirect` never returns
|
|
20
|
-
default status is `307`, because it preserves the method: a redirect out of a
|
|
21
|
-
`POST` does not silently become a `GET` of the target.
|
|
21
|
+
`redirect` never returns: it throws, which stops the component.
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
the exception: a redirect out of it is answered `303 See Other`, whatever
|
|
25
|
-
status it was given. The browser follows it with a `GET`, as it should after a
|
|
26
|
-
form. A `307` would post the form again at the destination, and run the action
|
|
27
|
-
again there.
|
|
23
|
+
## Redirecting from a page
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
The default status is `307`, which keeps the request method, so a redirect out
|
|
26
|
+
of a `POST` doesn't turn into a `GET` of the destination. For a permanent one,
|
|
27
|
+
pass the status:
|
|
31
28
|
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
```ts
|
|
30
|
+
redirect('/old', { status: 308 });
|
|
31
|
+
redirect('/old', 308); // the same
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
- **During a navigation** the redirect is a navigation too. The document
|
|
35
|
+
doesn't reload, and the layouts you are inside stay mounted.
|
|
36
|
+
- **The URL that redirected is replaced in history**, so Back doesn't land on
|
|
37
|
+
it and redirect again.
|
|
38
|
+
- **A form posted without JavaScript** - before the page has loaded it, or in a
|
|
39
|
+
browser without it - gets `303 See Other`, whatever status you gave. The
|
|
40
|
+
browser follows it with a `GET`; a `307` would post the form again at the
|
|
41
|
+
destination and run the action twice.
|
|
34
42
|
|
|
35
43
|
## With a query string
|
|
36
44
|
|
|
37
|
-
`search
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
Pass `search`. It is typed by the destination page's own `searchParams`
|
|
46
|
+
schema, as `Link`'s `search` prop is, so a key the page never reads doesn't
|
|
47
|
+
compile:
|
|
40
48
|
|
|
41
49
|
```ts
|
|
42
50
|
redirect('/', { search: { auth: true } }); // → /?auth=true
|
|
43
51
|
redirect('/search', { search: { q, page: 2 } }); // checked against /search's schema
|
|
44
|
-
redirect('/old', { status: 308 }); // a permanent one
|
|
45
52
|
```
|
|
46
53
|
|
|
47
|
-
A bare second argument is still the status: `redirect('/old', 308)`.
|
|
48
|
-
|
|
49
54
|
## Leaving the app
|
|
50
55
|
|
|
51
|
-
A
|
|
52
|
-
and a redirect to either is a full page load rather than a navigation — the
|
|
53
|
-
router does not fetch them as pages, because neither answers one:
|
|
56
|
+
A redirect can go to another site, or to a URL your backend answers:
|
|
54
57
|
|
|
55
58
|
```tsx
|
|
56
|
-
// Another site
|
|
59
|
+
// Another site: any http(s) URL type-checks as written.
|
|
57
60
|
redirect('https://gitlab.com/oauth/authorize?client_id=…');
|
|
58
61
|
|
|
59
|
-
// A
|
|
60
|
-
// prefix, so this needs no cast either.
|
|
62
|
+
// A URL your backend owns, named in backendPaths.
|
|
61
63
|
redirect('/gitlab/connect');
|
|
62
64
|
```
|
|
63
65
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
on the page before it rather than on one that sends you out again.
|
|
68
|
-
|
|
69
|
-
The second kind needs its prefix named, because the app cannot otherwise tell
|
|
70
|
-
the backend's url from a page it has not built yet:
|
|
66
|
+
Both are full page loads rather than navigations. Name the backend's prefixes
|
|
67
|
+
in the config, since the app can't otherwise tell them from a page it hasn't
|
|
68
|
+
built yet:
|
|
71
69
|
|
|
72
70
|
```ts title="vite.config.ts"
|
|
73
71
|
rscKit({ backendPaths: ['/auth', '/github', '/gitlab'] })
|
|
74
72
|
```
|
|
75
73
|
|
|
76
|
-
A
|
|
77
|
-
as a
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
stays and the url does not move. Named, a `Link`, `visit()` or `redirect()` to
|
|
82
|
-
it loads the document, and it is never prefetched — a hover cannot start the
|
|
83
|
-
flow. `backendPaths` is [described with the backend](/hosts/backend-answered-pages/).
|
|
84
|
-
|
|
85
|
-
## Where you call it matters
|
|
86
|
-
|
|
87
|
-
This is the part worth understanding, because it is also the security-relevant
|
|
88
|
-
part. Headers flush early on purpose — that is what makes the first paint fast
|
|
89
|
-
— so a redirect decided late has no status line left to use. There are two
|
|
90
|
-
windows, and nothing you write chooses between them:
|
|
91
|
-
|
|
92
|
-
| Called | Answered with | What the browser saw first |
|
|
93
|
-
| --- | --- | --- |
|
|
94
|
-
| Above every `<Suspense>` boundary | A real `3xx`, or `X-RSC-Redirect` on a navigation | Nothing at all |
|
|
95
|
-
| Inside a boundary | The shell, then the redirect | Layouts, and the fallbacks standing in for what never arrived |
|
|
96
|
-
|
|
97
|
-
Neither buffers the response. Before anything is written the host is still
|
|
98
|
-
waiting on the shell, so a component that redirects instead of rendering is
|
|
99
|
-
caught there. After that, React already carries an error digest to the client
|
|
100
|
-
and the destination rides along in it, and the browser performs it as a
|
|
101
|
-
navigation — the layouts above stay mounted, the url being left is replaced
|
|
102
|
-
rather than kept in history. An `error.tsx` on the route never sees it: a
|
|
103
|
-
redirect is the page's answer, not a failure, whether it is thrown by the
|
|
104
|
-
page, by a component under its own `<Suspense>`, or by a parallel route slot.
|
|
105
|
-
|
|
106
|
-
<Aside type="caution" title="Middleware belongs above the boundaries">
|
|
107
|
-
A `loading.tsx` wraps the whole page in `<Suspense>`. That is usually what you
|
|
108
|
-
want — it is how a page gets a frozen shell — but it also means an `await` at
|
|
109
|
-
the top of that page is *inside* a boundary, and a redirect after it arrives
|
|
110
|
-
in the second window.
|
|
111
|
-
|
|
112
|
-
Nothing from inside the boundary was shown, so a not-found redirect is fine
|
|
113
|
-
there. An authorization check is different: the layouts above it already
|
|
114
|
-
rendered and already went out. If a page must reveal nothing at all, the
|
|
115
|
-
check has to run somewhere that blocks the shell.
|
|
116
|
-
</Aside>
|
|
117
|
-
|
|
118
|
-
## Where to put an authorization check
|
|
119
|
-
|
|
120
|
-
In order of preference:
|
|
74
|
+
A URL in `backendPaths` is typed for `redirect()`, `Link` and `visit()`, loads
|
|
75
|
+
as a document, and is never prefetched, so hovering a link can't start an OAuth
|
|
76
|
+
flow. Without it, the router fetches the URL as a page, the backend redirects
|
|
77
|
+
to another site, and the browser refuses that hop: the click does nothing.
|
|
78
|
+
See [backend-answered pages](/hosts/backend-answered-pages/).
|
|
121
79
|
|
|
122
|
-
|
|
123
|
-
|
|
80
|
+
It works the same above a `<Suspense>` boundary or inside one, on a first visit
|
|
81
|
+
or on a click. Back from the other site lands on the page before the redirect.
|
|
124
82
|
|
|
83
|
+
## Redirecting from a server action
|
|
125
84
|
|
|
126
|
-
|
|
127
|
-
It is tempting — a layout renders above the page's boundary, so on a full
|
|
128
|
-
page load a redirect there happens before anything is sent. But a navigation
|
|
129
|
-
tells the server which layouts the client already has, in `X-RSC-Segments`,
|
|
130
|
-
and the server skips re-rendering those. A client that *claims* to hold your
|
|
131
|
-
layout skips the check:
|
|
132
|
-
|
|
133
|
-
```bash
|
|
134
|
-
curl -H 'X-RSC: 1' -H 'X-RSC-Segments: app/layout' /guarded
|
|
135
|
-
# 204, X-RSC-Redirect: /orders ← the middleware ran
|
|
85
|
+
Call `redirect()` in the action:
|
|
136
86
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
```
|
|
87
|
+
```ts title="src/actions.ts"
|
|
88
|
+
'use server'
|
|
140
89
|
|
|
141
|
-
|
|
142
|
-
beside the layout instead — it runs before anything below it renders, on
|
|
143
|
-
every path. See [Authorization](/guides/authorization).
|
|
144
|
-
</Aside>
|
|
90
|
+
import { redirect } from '@rsc-kit/core/redirect';
|
|
145
91
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
way: adding a `loading.tsx` later silently moves the check into the second
|
|
149
|
-
window, and nothing warns.
|
|
150
|
-
|
|
151
|
-
<Aside type="danger" title="Never catch it and continue">
|
|
152
|
-
`redirect` communicates by throwing. A `try`/`catch` that swallows everything
|
|
153
|
-
turns the redirect into a blank region — the component stops, and nothing
|
|
154
|
-
takes its place. If you must wrap the call, rethrow what you do not
|
|
155
|
-
recognise:
|
|
156
|
-
|
|
157
|
-
```ts
|
|
158
|
-
import { isRedirectSignal } from '@rsc-kit/core/redirect';
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
await mightRedirect();
|
|
162
|
-
} catch (error) {
|
|
163
|
-
if (isRedirectSignal(error)) throw error;
|
|
164
|
-
// …
|
|
165
|
-
}
|
|
166
|
-
```
|
|
167
|
-
</Aside>
|
|
92
|
+
export async function createPost(title: string) {
|
|
93
|
+
const post = await savePost(title);
|
|
168
94
|
|
|
169
|
-
|
|
95
|
+
redirect(`/posts/${post.slug}`);
|
|
96
|
+
}
|
|
97
|
+
```
|
|
170
98
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
`
|
|
174
|
-
layouts stay mounted and the history entry the form was on is replaced, so
|
|
175
|
-
Back does not return to the submitted form.
|
|
99
|
+
The client follows it as a navigation, and the form's history entry is
|
|
100
|
+
replaced, so Back doesn't return to the submitted form. This works for a plain
|
|
101
|
+
`"use server"` function and for one built with `createActionClient()`.
|
|
176
102
|
|
|
177
|
-
The layouts
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
the destination whole, layouts included
|
|
181
|
-
can live in a layout. A team switch whose name is in the sidebar:
|
|
103
|
+
The layouts aren't rendered again, as on any navigation. When the action
|
|
104
|
+
changed something a layout shows - a team switch, a sign-in, a count in the
|
|
105
|
+
sidebar - say so: `revalidate('all')` before the `redirect()` asks for
|
|
106
|
+
the destination whole, layouts included:
|
|
182
107
|
|
|
183
108
|
```ts
|
|
184
109
|
'use server'
|
|
@@ -190,40 +115,45 @@ export async function switchTeam(id: string) {
|
|
|
190
115
|
}
|
|
191
116
|
```
|
|
192
117
|
|
|
193
|
-
Without it the page
|
|
194
|
-
|
|
195
|
-
before the redirect. A `<Form>`
|
|
196
|
-
|
|
118
|
+
Without it, the page shows the new team and the sidebar the old one until a
|
|
119
|
+
reload. A named section counts too, since it can live in a layout. For a
|
|
120
|
+
backend action it is `Rsc::revalidate('all')` before the redirect. A `<Form>`
|
|
121
|
+
shows nothing for a redirect: arriving is the success.
|
|
197
122
|
|
|
198
|
-
|
|
199
|
-
once the navigation is under way. It does not throw: a `startTransition(async
|
|
200
|
-
() => { await logOut() })` with no `catch` around it would otherwise reject
|
|
201
|
-
into React, which unmounts the root, and a logout ended on a white page.
|
|
202
|
-
There is nothing to catch; a component that awaited the action is on its
|
|
203
|
-
way off the screen.
|
|
123
|
+
### Checking for a redirect in the caller
|
|
204
124
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
125
|
+
A redirected action **resolves** with `{ redirected: '/where' }` instead of
|
|
126
|
+
its result. It doesn't throw, so a `startTransition` with no `catch` can't
|
|
127
|
+
unmount the page.
|
|
128
|
+
|
|
129
|
+
A stub generated for a backend action is typed `Promise<T | Redirected>`.
|
|
130
|
+
Narrow it with `isRedirected` before reading the value:
|
|
210
131
|
|
|
211
132
|
```ts
|
|
212
133
|
import { isRedirected } from '@rsc-kit/core/errors'
|
|
213
134
|
|
|
214
|
-
const secret = await
|
|
135
|
+
const secret = await databases.reveal(id)
|
|
215
136
|
|
|
216
137
|
if (isRedirected(secret)) return // on its way to /login
|
|
217
138
|
|
|
218
139
|
copy(secret.Password)
|
|
219
140
|
```
|
|
220
141
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
`
|
|
225
|
-
|
|
226
|
-
|
|
142
|
+
An action built on `createActionClient()` always resolves to an object with
|
|
143
|
+
`redirected` among its fields. A plain `"use server"` function is typed as what
|
|
144
|
+
it returns, so check `isRedirected` on its result, or build it on
|
|
145
|
+
`createActionClient()`.
|
|
146
|
+
|
|
147
|
+
An action that returns nothing needs the check too: in
|
|
148
|
+
`await stub(); toast.success('Saved')` the toast runs after the redirect has
|
|
149
|
+
started, for a write that never happened. No type or lint rule catches that,
|
|
150
|
+
so cover it in review and with a [test](/guides/testing#components) that
|
|
151
|
+
clicks it.
|
|
152
|
+
|
|
153
|
+
`tsc` also misses a redirect read as text: `` visit(`/t/${id}`) `` on a
|
|
154
|
+
`string | Redirected` compiles and goes to `/t/[object Object]`. Three
|
|
155
|
+
type-aware lint rules catch it, and a project from `create-rsc-kit` with
|
|
156
|
+
`--lint` has them on. Elsewhere:
|
|
227
157
|
|
|
228
158
|
```sh
|
|
229
159
|
npm i -D oxlint-tsgolint
|
|
@@ -241,41 +171,75 @@ npm i -D oxlint-tsgolint
|
|
|
241
171
|
|
|
242
172
|
and run `oxlint src --type-aware`. Type-aware rules need the flag.
|
|
243
173
|
|
|
244
|
-
|
|
245
|
-
`await stub(); toast.success('Saved')` runs after the redirect has started and
|
|
246
|
-
reports a write that never happened. Nothing checks this one: no type and no
|
|
247
|
-
stock lint rule flags a result that is awaited and thrown away, so it is a
|
|
248
|
-
matter for review and for a [test](/guides/testing#components) that clicks it. A plain `"use server"` function you wrote
|
|
249
|
-
is typed as what it returns, which cannot say this; build it on
|
|
250
|
-
`createActionClient()`, or check `isRedirected` on its result.
|
|
174
|
+
## A route that only redirects
|
|
251
175
|
|
|
252
|
-
|
|
253
|
-
|
|
176
|
+
A page that always redirects is stored by the build, status and location, and
|
|
177
|
+
answered without rendering. A static export carries it too:
|
|
254
178
|
|
|
255
|
-
|
|
179
|
+
```text
|
|
180
|
+
○ /old-pricing (redirects to /pricing)
|
|
181
|
+
```
|
|
256
182
|
|
|
257
|
-
|
|
258
|
-
const post = await savePost(title);
|
|
183
|
+
## Don't catch a redirect
|
|
259
184
|
|
|
260
|
-
|
|
185
|
+
`redirect` works by throwing. A `try`/`catch` that swallows everything turns it
|
|
186
|
+
into a blank region. If you have to wrap the call, rethrow it:
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
import { isRedirectSignal } from '@rsc-kit/core/redirect';
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
await mightRedirect();
|
|
193
|
+
} catch (error) {
|
|
194
|
+
if (isRedirectSignal(error)) throw error;
|
|
195
|
+
// …
|
|
261
196
|
}
|
|
262
197
|
```
|
|
263
198
|
|
|
264
|
-
##
|
|
199
|
+
## Redirects and Suspense
|
|
265
200
|
|
|
266
|
-
|
|
201
|
+
Where the redirect is called decides what the browser has already seen:
|
|
267
202
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
203
|
+
| Called | Answered with | What the browser saw first |
|
|
204
|
+
| --- | --- | --- |
|
|
205
|
+
| Above every `<Suspense>` boundary | A real `3xx`, or `X-RSC-Redirect` on a navigation | Nothing |
|
|
206
|
+
| Inside a boundary | The shell, then the redirect | The layouts, and the fallbacks |
|
|
207
|
+
|
|
208
|
+
Either way the browser ends up at the destination. A page sends its shell as
|
|
209
|
+
early as it can, so a redirect decided inside a boundary arrives after it, as
|
|
210
|
+
a navigation. An `error.tsx` never sees a redirect, from a page, a component
|
|
211
|
+
under its own `<Suspense>` or a parallel slot.
|
|
212
|
+
|
|
213
|
+
That matters for an authorization check. A redirect for a missing record is
|
|
214
|
+
fine inside a boundary, but a page that must reveal nothing needs its check
|
|
215
|
+
before the shell is sent. A `loading.tsx` puts the whole page inside a
|
|
216
|
+
boundary, so an `await` at the top of that page is inside it.
|
|
217
|
+
|
|
218
|
+
Put the check in a `middleware.ts`. It runs before anything at or below its
|
|
219
|
+
folder renders, on every path. See [Authorization](/guides/authorization).
|
|
220
|
+
|
|
221
|
+
<Aside type="danger" title="A layout is not a security boundary">
|
|
222
|
+
A navigation tells the server which layouts the client already has, in
|
|
223
|
+
`X-RSC-Segments`, and the server skips rendering those. A client that
|
|
224
|
+
*claims* to hold your layout skips a check in it:
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
curl -H 'X-RSC: 1' -H 'X-RSC-Segments: app/layout' /guarded
|
|
228
|
+
# 204, X-RSC-Redirect: /orders ← the middleware ran
|
|
229
|
+
|
|
230
|
+
curl -H 'X-RSC: 1' -H 'X-RSC-Segments: app/layout,app/guarded/layout' /guarded
|
|
231
|
+
# 200, and the page's content ← it did not
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
The header can't be verified. Use a `middleware.ts` beside the layout.
|
|
235
|
+
</Aside>
|
|
271
236
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
export can carry it.
|
|
237
|
+
A check at the top of a page with no `loading.tsx` also runs on every render.
|
|
238
|
+
It is fragile: adding a `loading.tsx` later moves it inside a boundary, and
|
|
239
|
+
nothing warns.
|
|
276
240
|
|
|
277
|
-
##
|
|
241
|
+
## Redirect loops
|
|
278
242
|
|
|
279
|
-
A navigation follows at most **8** redirects
|
|
280
|
-
redirects to itself is
|
|
281
|
-
|
|
243
|
+
A navigation follows at most **8** redirects, then throws, so a page that
|
|
244
|
+
redirects to itself is an error you can see rather than an endless run of
|
|
245
|
+
renders.
|