@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.
Files changed (60) hide show
  1. package/dist/recipes.js +18 -2
  2. package/dist/recipes.js.map +1 -1
  3. package/guides/admin-plugins.md +206 -6
  4. package/guides/api-routes.md +159 -215
  5. package/guides/authorization.md +103 -114
  6. package/guides/backend-answered-pages.md +301 -252
  7. package/guides/bap-vs-inertia.md +67 -25
  8. package/guides/blueprints.md +300 -169
  9. package/guides/bun.md +42 -25
  10. package/guides/caching.md +46 -49
  11. package/guides/coming-from-inertia.md +2 -2
  12. package/guides/commands.md +251 -200
  13. package/guides/connection.md +50 -54
  14. package/guides/content-security-policy.md +133 -84
  15. package/guides/domains.md +190 -149
  16. package/guides/edge-caching.md +242 -268
  17. package/guides/emails.md +96 -45
  18. package/guides/errors.md +116 -137
  19. package/guides/feature-flags.md +100 -30
  20. package/guides/file-uploads.md +101 -26
  21. package/guides/fonts.md +139 -110
  22. package/guides/forms.md +520 -542
  23. package/guides/images.md +87 -28
  24. package/guides/index.json +13 -33
  25. package/guides/instrumentation.md +134 -95
  26. package/guides/isr.md +3 -3
  27. package/guides/live-data-in-production.md +695 -0
  28. package/guides/live-data.md +170 -839
  29. package/guides/mcp.md +83 -59
  30. package/guides/metadata.md +124 -179
  31. package/guides/navigation.md +118 -147
  32. package/guides/no-javascript.md +49 -57
  33. package/guides/offline.md +118 -153
  34. package/guides/openapi.md +100 -35
  35. package/guides/pages.md +160 -50
  36. package/guides/ppr.md +178 -256
  37. package/guides/pwa.md +270 -216
  38. package/guides/queries.md +171 -300
  39. package/guides/react-compiler.md +90 -50
  40. package/guides/redirects.md +141 -177
  41. package/guides/response-headers.md +76 -74
  42. package/guides/route-interception.md +112 -111
  43. package/guides/routing.md +216 -322
  44. package/guides/sections.md +63 -66
  45. package/guides/seo-files.md +127 -65
  46. package/guides/server-actions.md +285 -357
  47. package/guides/static-generation.md +131 -199
  48. package/guides/testing.md +269 -147
  49. package/guides/third-party-scripts.md +94 -47
  50. package/guides/typed-routes.md +115 -113
  51. package/guides/url-validation.md +113 -61
  52. package/guides/validation.md +53 -62
  53. package/guides/view-transitions.md +159 -132
  54. package/guides/where-it-runs.md +226 -162
  55. package/package.json +2 -2
  56. package/guides/admin.md +0 -520
  57. package/guides/deployment.md +0 -598
  58. package/guides/go.md +0 -539
  59. package/guides/laravel.md +0 -787
  60. package/guides/your-own-backend.md +0 -602
@@ -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
- Nothing here special-cases it. The build runs your project's own Vite config,
9
- so the compiler is enabled the way it is in any Vite app — by adding
10
- `@vitejs/plugin-react` after `rscKit()` and turning it on there.
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
- <Aside type="note" title="Order matters">
13
- `rscKit()` includes `@vitejs/plugin-rsc`, which has to see modules before
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
- ## Two ways to run it
27
+ ## Starting with it on
18
28
 
19
- The compiler has a native implementation and a Babel one. Both produce the same
20
- transform; they differ in what they cost to run and how settled they are.
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` present — the plugin looks for it by name:
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
- import { defineConfig } from 'vite';
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. In `@vitejs/plugin-react` 6 the inline `babel`
48
- option was removed, so the preset is applied through `@rolldown/plugin-babel`:
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
- ## Checks worth running first
92
+ ### Plugin order
76
93
 
77
- The compiler only memoises components it can prove are safe to memoise, and it
78
- skips the rest silently. These are how you find out which is which, and they
79
- are worth running **before** you turn it on rather than after.
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
- **Type checking.** The compiler assumes your code means what its types say. Run
82
- `tsc --noEmit` and fix what it reports first — an untyped `any` threading
83
- through a component is exactly the shape the compiler has to give up on.
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
- **The health check.** Reports how many components in your codebase the compiler
90
- can handle, and why the others are refused:
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
- **The lint rule.** `eslint-plugin-react-hooks` includes the compiler's own
97
- diagnostics — the Rules of React violations that make a component
98
- uncompilable — so they surface as you write rather than as silence in the
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
- **StrictMode.** The compiler's assumptions are the Rules of React, and
108
- StrictMode is what surfaces breaking them at runtime — double-invoked renders
109
- catch the impure ones.
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
- <Aside type="tip" title="It applies to client components">
112
- Server components render once and are thrown away, so there is nothing to
113
- memoise in them. The compiler earns its keep in the `"use client"` half of
114
- the app.
115
- </Aside>
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 leaves a cache array at the top of every component it compiled.
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 that component was skipped —
134
- which the health check will explain.
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
- Drop `compiler: true`, or remove the plugin. Neither the router nor the build
151
- depends on it being there.
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).
@@ -1,6 +1,8 @@
1
1
  # Redirects
2
2
 
3
- > Leaving a page from inside the render, and what that costs.
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 — it throws, which is what stops the component. The
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
- A form posted before the page has javascript - or by a browser with none - is
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
- A redirect during a navigation stays a navigation. The document does not reload,
30
- the layouts you are inside stay mounted, and only the part below them changes.
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
- The url that redirected replaces its history entry rather than adding one, so
33
- Back does not land on it and redirect you again.
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` is typed to the destination page's own `searchParams` schema, the
38
- same check `Link` puts on its `search` prop — a key the page never reads, or
39
- a number written as text, does not compile:
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 destination does not have to be one of this app's pages. Two kinds are not,
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. Any http(s) url; it type-checks as written.
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 url your backend owns, named in backendPaths (below). The build types the
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
- It works the same wherever the redirect is called, above a `<Suspense>`
65
- boundary or inside one, and on a first visit as on a click. In every case the
66
- url that redirected is replaced in history, so Back from the other site lands
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 name in `backendPaths` is also what keeps the router from asking for that url
77
- as a page. Without it, the router fetches `/gitlab/connect` for its payload, the
78
- backend answers with a redirect to another site (an OAuth start does), and the
79
- browser refuses that hop — a fetch cannot follow a redirect to another origin
80
- with the headers the router sends. The click then does nothing: the old page
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
- **In `middleware.ts`.** A file beside the layout, run before anything at or below it
123
- renders, on every path. This is the one built for the job.
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
- <Aside type="danger" title="A layout is not a security boundary">
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
- curl -H 'X-RSC: 1' -H 'X-RSC-Segments: app/layout,app/guarded/layout' /guarded
138
- # 200, and the page's content ← it did not
139
- ```
87
+ ```ts title="src/actions.ts"
88
+ 'use server'
140
89
 
141
- The header is not verified and cannot be. Put the check in a `middleware.ts`
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
- **At the top of a page with no `loading.tsx`.** Runs on every render of that
147
- page, so it is not skippable the way a layout is — but fragile in a different
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
- ## From a server action
95
+ redirect(`/posts/${post.slug}`);
96
+ }
97
+ ```
170
98
 
171
- An action is not a render, so there is no shell to be on either side of. Throw
172
- from the action — a plain `"use server"` function or one built from
173
- `createActionClient()` — and the client follows it as a navigation: the
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 are not rendered again, as on any navigation: most redirects —
178
- save, then the list — change nothing a layout reads. One that does says so,
179
- the way any action does. `revalidate('all')` before the `redirect()` asks for
180
- the destination whole, layouts included; a named section counts too, since it
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 is the new team's and the sidebar still names the old one
194
- until a reload. The same holds for a backend action: `Rsc::revalidate('all')`
195
- before the redirect. A `<Form>` shows nothing for it;
196
- signing in and arriving is the success.
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
- To the caller, the action **resolves** — with `{ redirected: '/where' }` —
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
- What it got is not the action's result, though, and only an action built on
206
- `createActionClient()` says so in its type: its result is always an object with
207
- `redirected` among the fields. A stub the build generated for a backend action
208
- is typed `Promise<T | Redirected>`, so reading the value without narrowing
209
- is a type error, and `isRedirected` is the narrowing:
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 databasesReveal(id)
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
- `tsc` does not see the value read as text. A template literal accepts any type,
222
- so `` visit(`/t/${id}`) `` on a `string | Redirected` compiles and navigates to
223
- `/t/[object Object]`. Three type-aware lint rules do see it:
224
- `typescript/restrict-template-expressions`, `typescript/no-base-to-string` and
225
- `typescript/restrict-plus-operands`. A project from `create-rsc-kit` with
226
- `--lint` has them on. In one that does not:
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
- An action that returns nothing needs the same check, because
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
- ```ts title="src/actions.ts"
253
- 'use server'
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
- import { redirect } from '@rsc-kit/core/redirect';
179
+ ```text
180
+ ○ /old-pricing (redirects to /pricing)
181
+ ```
256
182
 
257
- export async function createPost(title: string) {
258
- const post = await savePost(title);
183
+ ## Don't catch a redirect
259
184
 
260
- redirect(`/posts/${post.slug}`);
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
- ## A route that only redirects
199
+ ## Redirects and Suspense
265
200
 
266
- The redirect itself is stored, so it costs no render at all:
201
+ Where the redirect is called decides what the browser has already seen:
267
202
 
268
- ```text
269
- ○ /old-pricing (redirects to /pricing)
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
- The build writes the status and the location, and the host answers from that
273
- file — a document gets the status code, a navigation gets `X-RSC-Redirect` and
274
- does it as an SPA navigation. Nothing is rendered per request, and a static
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
- ## Loops
241
+ ## Redirect loops
278
242
 
279
- A navigation follows at most **8** redirects before throwing. A page that
280
- redirects to itself is a mistake someone will make, and without a ceiling it is
281
- an unbounded run of full renders rather than an error you can see.
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.