@rsc-kit/mcp 0.40.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 +37 -20
- 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 -250
- package/guides/bap-vs-inertia.md +67 -25
- package/guides/blueprints.md +307 -174
- package/guides/bun.md +42 -25
- package/guides/caching.md +46 -49
- package/guides/coming-from-inertia.md +5 -5
- 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 -538
- 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 +272 -150
- 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 -538
- package/guides/laravel.md +0 -786
- package/guides/your-own-backend.md +0 -587
|
@@ -2,33 +2,58 @@
|
|
|
2
2
|
|
|
3
3
|
> Analytics, tag managers and widgets — and why there is no Script component.
|
|
4
4
|
|
|
5
|
-
Write
|
|
6
|
-
component
|
|
7
|
-
|
|
8
|
-
## An external script
|
|
5
|
+
Write a `<script>` tag. React 19 puts it in the `<head>` and sends it once, so
|
|
6
|
+
there is no `Script` component to import:
|
|
9
7
|
|
|
10
8
|
```tsx title="src/app/layout.tsx"
|
|
11
|
-
export default function RootLayout({ children }) {
|
|
9
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
12
10
|
return (
|
|
13
|
-
<html>
|
|
11
|
+
<html lang="en">
|
|
14
12
|
<body>
|
|
15
13
|
{children}
|
|
16
|
-
<script async src="https://
|
|
14
|
+
<script async src="https://plausible.io/js/script.js" data-domain="example.com" />
|
|
17
15
|
</body>
|
|
18
16
|
</html>
|
|
19
17
|
)
|
|
20
18
|
}
|
|
21
19
|
```
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
## Adding an external script
|
|
22
|
+
|
|
23
|
+
Render the tag with `async` from a server component:
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
<script async src="https://www.clarity.ms/tag/abc123" />
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
React **hoists** it into `<head>` and **deduplicates** it. Render the same
|
|
30
|
+
`src` in three components and one tag goes out. `async` means it never blocks
|
|
31
|
+
the page.
|
|
27
32
|
|
|
28
|
-
|
|
33
|
+
Put site-wide scripts in the root layout. The root layout renders once and is
|
|
34
|
+
kept across navigations, so the script runs once. In a page, a client-side
|
|
35
|
+
navigation renders the tag again, and whether the browser runs a script
|
|
36
|
+
inserted that way isn't something to rely on.
|
|
37
|
+
|
|
38
|
+
## Adding an inline snippet
|
|
39
|
+
|
|
40
|
+
Most analytics ship a few lines that stub a global and load the real script.
|
|
41
|
+
Google Analytics:
|
|
42
|
+
|
|
43
|
+
```tsx title="src/app/layout.tsx"
|
|
44
|
+
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX" />
|
|
45
|
+
<script
|
|
46
|
+
id="gtag-init"
|
|
47
|
+
dangerouslySetInnerHTML={{
|
|
48
|
+
__html: `window.dataLayer = window.dataLayer || [];
|
|
49
|
+
function gtag(){dataLayer.push(arguments);}
|
|
50
|
+
gtag('js', new Date());
|
|
51
|
+
gtag('config', 'G-XXXXXXX');`,
|
|
52
|
+
}}
|
|
53
|
+
/>
|
|
54
|
+
```
|
|
29
55
|
|
|
30
|
-
|
|
31
|
-
thing:
|
|
56
|
+
Microsoft Clarity:
|
|
32
57
|
|
|
33
58
|
```tsx title="src/app/layout.tsx"
|
|
34
59
|
<script
|
|
@@ -42,36 +67,15 @@ thing:
|
|
|
42
67
|
/>
|
|
43
68
|
```
|
|
44
69
|
|
|
45
|
-
|
|
46
|
-
a snippet whose job is to start recording as early
|
|
47
|
-
|
|
48
|
-
`
|
|
49
|
-
|
|
50
|
-
:::note[Put it in the root layout]
|
|
51
|
-
The root layout renders once and is kept across navigations, so the snippet
|
|
52
|
-
runs once. In a *page*, a client-side navigation to it re-renders the tag, and
|
|
53
|
-
whether the browser runs a script inserted that way is not something to rely on
|
|
54
|
-
either way. Site-wide scripts go in the layout; that is where they belong
|
|
55
|
-
anyway.
|
|
56
|
-
:::
|
|
57
|
-
|
|
58
|
-
## Porting from Next
|
|
59
|
-
|
|
60
|
-
| Next's `Script strategy` | here |
|
|
61
|
-
| --- | --- |
|
|
62
|
-
| `beforeInteractive` | `<script src>` without `async` — the browser blocks on it, which is what that strategy meant |
|
|
63
|
-
| `afterInteractive` | `<script async src>`, or the inline snippet above |
|
|
64
|
-
| `lazyOnload` | the effect below, inside `requestIdleCallback` |
|
|
65
|
-
| `worker` | not supported — that was Partytown, a separate project |
|
|
66
|
-
|
|
67
|
-
The `id` prop carries across unchanged. `onLoad` and `onReady` are the one
|
|
68
|
-
thing that needs a component, because they need to run in the browser:
|
|
70
|
+
An inline snippet renders where you wrote it and runs during parse,
|
|
71
|
+
**before hydration**. For a snippet whose job is to start recording as early
|
|
72
|
+
as possible, that is what its authors intended. Next's `Script` with
|
|
73
|
+
`strategy="afterInteractive"` would have run it later.
|
|
69
74
|
|
|
70
|
-
##
|
|
75
|
+
## Running a script after hydration
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
yours rather than ours because there is nothing to abstract:
|
|
77
|
+
Some scripts touch DOM that React rendered, and find it missing if they run
|
|
78
|
+
during parse. Load those from a small client component of your own:
|
|
75
79
|
|
|
76
80
|
```tsx title="src/components/AfterHydration.tsx"
|
|
77
81
|
'use client'
|
|
@@ -95,11 +99,54 @@ export function AfterHydration({ src, onLoad }: { src: string; onLoad?: () => vo
|
|
|
95
99
|
}
|
|
96
100
|
```
|
|
97
101
|
|
|
98
|
-
|
|
102
|
+
```tsx title="src/app/support/page.tsx"
|
|
103
|
+
<AfterHydration src="https://widget.example.com/chat.js" />
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
This is also where `onLoad` and `onReady` go: they run in the browser, so they
|
|
107
|
+
need a component.
|
|
108
|
+
|
|
109
|
+
## Loading a script when the page is idle
|
|
110
|
+
|
|
111
|
+
Wrap the effect's body in `requestIdleCallback`:
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
const id = requestIdleCallback(() => {
|
|
116
|
+
const tag = document.createElement('script')
|
|
117
|
+
|
|
118
|
+
tag.src = src
|
|
119
|
+
tag.async = true
|
|
120
|
+
document.head.append(tag)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
return () => cancelIdleCallback(id)
|
|
124
|
+
}, [src])
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
That is Next's `lazyOnload`.
|
|
128
|
+
|
|
129
|
+
## Under a Content Security Policy
|
|
130
|
+
|
|
131
|
+
A script loaded by `src` needs its origin in `script-src`. An inline snippet in
|
|
132
|
+
a page the build stores is hashed and runs. On a page rendered per request,
|
|
133
|
+
render it from a client component and give it `useNonce()`'s nonce. See
|
|
134
|
+
[Content Security Policy](/guides/content-security-policy#your-own-inline-code).
|
|
135
|
+
|
|
136
|
+
## Porting from Next
|
|
137
|
+
|
|
138
|
+
| Next's `Script strategy` | Here |
|
|
139
|
+
| --- | --- |
|
|
140
|
+
| `beforeInteractive` | `<script src>` without `async`. The browser blocks on it, which is what that strategy meant. |
|
|
141
|
+
| `afterInteractive` | `<script async src>`, or an inline snippet |
|
|
142
|
+
| `lazyOnload` | [The idle effect](#loading-a-script-when-the-page-is-idle) |
|
|
143
|
+
| `worker` | Not supported. That was Partytown, a separate project. |
|
|
144
|
+
|
|
145
|
+
The `id` prop carries across unchanged.
|
|
99
146
|
|
|
100
147
|
## Why nothing ships for this
|
|
101
148
|
|
|
102
|
-
A `Script` component would
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
149
|
+
A `Script` component would wrap a `script` tag whose two useful behaviours,
|
|
150
|
+
hoisting and deduplication, React already provides. The package's own tests
|
|
151
|
+
pin that React does, so the day it stops, this page is what changes rather
|
|
152
|
+
than your app.
|
package/guides/typed-routes.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
> Links that fail the typecheck instead of the browser.
|
|
4
4
|
|
|
5
|
-
Every build writes the
|
|
6
|
-
`
|
|
5
|
+
Every build writes the URLs it found to `.rsc-kit/rsc-routes.d.ts`. `Link`,
|
|
6
|
+
`visit`, `prefetch`, `redirect` and `Form` accept only those:
|
|
7
7
|
|
|
8
8
|
```tsx
|
|
9
9
|
import Link from '@rsc-kit/core/Link'
|
|
@@ -12,31 +12,32 @@ import Link from '@rsc-kit/core/Link'
|
|
|
12
12
|
<Link href="/abuot">About</Link> // ❌ typecheck fails
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
A link to a page you renamed or deleted stops compiling, instead of becoming a
|
|
16
|
+
404 someone finds after you deploy.
|
|
17
|
+
|
|
18
|
+
## Linking to a dynamic route
|
|
19
|
+
|
|
20
|
+
Use an ordinary template literal:
|
|
16
21
|
|
|
17
22
|
```tsx
|
|
18
23
|
<Link href={`/posts/${post.slug}`}>{post.title}</Link> // ✅
|
|
19
24
|
<Link href={`/postz/${post.slug}`}>{post.title}</Link> // ❌
|
|
20
25
|
```
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Deliberately. A template literal is already checked the same way a builder
|
|
25
|
-
would check it, so a builder would only wrap what the language does for free.
|
|
26
|
-
One existed and was removed.
|
|
27
|
+
There is no `route()` helper. A template literal is already checked the way a
|
|
28
|
+
builder would check it.
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
`encodeURIComponent` when the value is not yours:
|
|
30
|
+
Encode a value that isn't yours. `` `/posts/${'a / b'}` `` type-checks, but it
|
|
31
|
+
means three path segments:
|
|
31
32
|
|
|
32
33
|
```tsx
|
|
33
34
|
<Link href={`/posts/${encodeURIComponent(slug)}`}>…</Link>
|
|
34
35
|
```
|
|
35
36
|
|
|
36
|
-
##
|
|
37
|
+
## Typed search params
|
|
37
38
|
|
|
38
|
-
A page that [exports a `searchParams` schema](/guides/url-validation)
|
|
39
|
-
|
|
39
|
+
A page that [exports a `searchParams` schema](/guides/url-validation) types
|
|
40
|
+
every link to it:
|
|
40
41
|
|
|
41
42
|
```tsx title="src/app/search/page.tsx"
|
|
42
43
|
export const searchParams = z.object({
|
|
@@ -52,84 +53,49 @@ export const searchParams = z.object({
|
|
|
52
53
|
<Link href="/search" search={{ page: '2' }}>…</Link> // does not compile: page is a number
|
|
53
54
|
```
|
|
54
55
|
|
|
55
|
-
A key
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
- **A required key is required on the link.** A `q: z.string()` with no default
|
|
57
|
+
makes `search` itself required, so a missing `q` is caught by the compiler,
|
|
58
|
+
not by the page's error boundary.
|
|
59
|
+
- **Values are typed by what the page sees.** `z.coerce.number()` accepts
|
|
60
|
+
anything, but the link takes a number. Whether a key is required comes from
|
|
61
|
+
the schema's input; its type comes from the output.
|
|
62
|
+
- **Arrays repeat the key.** `tags: ['a', 'b']` becomes `?tags=a&tags=b`, which
|
|
63
|
+
is what `z.array()` parses back.
|
|
64
|
+
- **A page with no schema takes any scalars**, and so does an href that isn't
|
|
65
|
+
one route (`path as Route`), because there's nothing to check it against.
|
|
60
66
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
`page: 'two'`. Required-ness comes from the schema's input, the value from its
|
|
64
|
-
output. Arrays repeat the key — `tags: ['a', 'b']` is `?tags=a&tags=b`, which
|
|
65
|
-
is what `z.array()` parses back.
|
|
66
|
-
|
|
67
|
-
For `visit()`, `prefetch()` and anything else that wants the string,
|
|
68
|
-
`href()` runs the same check:
|
|
67
|
+
For `visit()`, `prefetch()` and anything else that wants a string, `href()`
|
|
68
|
+
runs the same check:
|
|
69
69
|
|
|
70
70
|
```ts
|
|
71
71
|
import { href } from '@rsc-kit/core/routes';
|
|
72
72
|
|
|
73
73
|
visit(href('/search', { q: 'shoes', page: 2 }));
|
|
74
|
+
redirect(href('/search', { q: 'shoes' }));
|
|
74
75
|
```
|
|
75
76
|
|
|
76
|
-
|
|
77
|
-
page module's `searchParams` export as a type. Nothing runs; a page without
|
|
78
|
-
the export costs nothing.
|
|
79
|
-
|
|
80
|
-
## Two limits
|
|
77
|
+
## A list of links
|
|
81
78
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
**A list widens to `string`** unless you say what it is:
|
|
79
|
+
TypeScript widens an array's `href` to `string` unless you say what it is. Use
|
|
80
|
+
`satisfies` with `Route`:
|
|
86
81
|
|
|
87
82
|
```tsx
|
|
83
|
+
import type { Route } from '@rsc-kit/core/routes'
|
|
84
|
+
|
|
88
85
|
const nav = [
|
|
89
86
|
{ href: '/', label: 'Home' },
|
|
90
87
|
{ href: '/about', label: 'About' },
|
|
91
88
|
] satisfies { href: Route; label: string }[]
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
Without `satisfies`, TypeScript infers `string` for `href` and you lose the
|
|
95
|
-
check.
|
|
96
89
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
`vite build` runs the project's typecheck before it bundles, and stops on an
|
|
100
|
-
error — so a link to a route that does not exist is a failed build, not a 404
|
|
101
|
-
found after deploying. It is the same `tsc --noEmit` the `check` script runs,
|
|
102
|
-
on the same tsconfig, and it costs about a second. `rscKit({ typecheck: false })`
|
|
103
|
-
turns it off; a project with no `tsconfig.json`, or no `typescript`
|
|
104
|
-
installed, is skipped without being asked.
|
|
105
|
-
|
|
106
|
-
## After adding a route
|
|
107
|
-
|
|
108
|
-
The file is written whenever Vite starts — a build, or the dev server, which
|
|
109
|
-
also rewrites it when it sees a route added. A typecheck run with neither, by
|
|
110
|
-
an agent or in CI, reads what the last start wrote, so a link to a route
|
|
111
|
-
added since fails with `"/admin"` is not assignable to type `Route`.
|
|
112
|
-
`rsc-kit-typegen` writes it without starting Vite, from the app's own
|
|
113
|
-
`vite.config`. The scaffold's `typecheck` and `check` scripts run it first; an
|
|
114
|
-
older project adds it the same way:
|
|
115
|
-
|
|
116
|
-
```json title="package.json"
|
|
117
|
-
"typecheck": "rsc-kit-typegen && tsc --noEmit"
|
|
90
|
+
nav.map((item) => <Link key={item.href} href={item.href}>{item.label}</Link>)
|
|
118
91
|
```
|
|
119
92
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
`.rsc-kit/rsc-routes.d.ts` is written by the build. Without it — or with a
|
|
123
|
-
tsconfig whose `include` does not cover `.rsc-kit` — nothing is registered,
|
|
124
|
-
every url-taking prop stays exactly as permissive as a plain `string`, and
|
|
125
|
-
nothing breaks. There is no flag to turn this on.
|
|
93
|
+
Without `satisfies`, `href` is a `string` and you lose the check.
|
|
126
94
|
|
|
127
|
-
##
|
|
95
|
+
## A URL computed at runtime
|
|
128
96
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
destination that is computed — read from a cookie, handed over by middleware —
|
|
132
|
-
is a string the compiler cannot check, so it takes the cast:
|
|
97
|
+
A destination read from a cookie or handed over by middleware is a string the
|
|
98
|
+
compiler can't check. Cast it, and you vouch for it:
|
|
133
99
|
|
|
134
100
|
```ts
|
|
135
101
|
redirect('/login'); // ✅
|
|
@@ -137,51 +103,42 @@ redirect('/logn'); // ❌
|
|
|
137
103
|
redirect(returnTo as Route); // computed: you vouch for it
|
|
138
104
|
```
|
|
139
105
|
|
|
140
|
-
A
|
|
141
|
-
`searchParams` schema
|
|
106
|
+
A redirect's `search` is checked too, against the destination's own
|
|
107
|
+
`searchParams` schema. See [redirects](/guides/redirects/#with-a-query-string).
|
|
142
108
|
|
|
143
|
-
##
|
|
109
|
+
## Fetching an API route
|
|
144
110
|
|
|
145
|
-
A `route.ts` is a `Route`
|
|
146
|
-
and `redirect('/files/export.csv')` all typecheck
|
|
147
|
-
differs is what the browser does with one: a route answers with a `Response`,
|
|
148
|
-
not a page, so the client treats a link to it as the anchor it is — never
|
|
149
|
-
prefetched (a hover must not sign someone out) and a full navigation rather
|
|
150
|
-
than a payload fetch. The build hands the client every route's pattern for
|
|
151
|
-
that.
|
|
111
|
+
A `route.ts` is a `Route` as well, so `<Link href="/logout">`,
|
|
112
|
+
`visit('/agent-account')` and `redirect('/files/export.csv')` all typecheck.
|
|
152
113
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
compiling:
|
|
114
|
+
For `fetch`, wrap the URL in `apiUrl`. It accepts only `route.ts` URLs, so a
|
|
115
|
+
fetch to an endpoint that no longer exists stops compiling:
|
|
156
116
|
|
|
157
117
|
```ts
|
|
158
118
|
import { apiUrl } from '@rsc-kit/core/routes'
|
|
159
119
|
|
|
160
120
|
await fetch(apiUrl(`/api/orders/${id}`))
|
|
161
121
|
await fetch(apiUrl('/api/ordrs')) // does not compile
|
|
122
|
+
await fetch(apiUrl('/orders')) // does not compile: that's a page
|
|
162
123
|
```
|
|
163
124
|
|
|
164
125
|
`apiUrl` returns what it was given. It exists because `fetch` takes any
|
|
165
|
-
`string`, so without
|
|
166
|
-
|
|
126
|
+
`string`, so without it there's nowhere to put the type. A page URL doesn't
|
|
127
|
+
compile, because fetching a page gets HTML where JSON was expected.
|
|
167
128
|
|
|
168
|
-
|
|
169
|
-
|
|
129
|
+
This checks the URL, not what the endpoint returns. API routes are plain web
|
|
130
|
+
standards, with nothing of ours in them. For types end to end, a [server
|
|
131
|
+
action or query](/guides/queries/) is already typed across the boundary: the
|
|
132
|
+
return type is the function's.
|
|
170
133
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
api routes are deliberately web standards with nothing of ours required in them.
|
|
175
|
-
|
|
176
|
-
For end-to-end types without a fetch at all, a [server action or
|
|
177
|
-
query](/guides/queries/) is already typed across the boundary: the return type
|
|
178
|
-
is the function's, because it is the same function.
|
|
179
|
-
:::
|
|
134
|
+
The browser treats a link to a `route.ts` as an ordinary anchor. It is never
|
|
135
|
+
prefetched, since a hover must not sign someone out, and clicking it is a full
|
|
136
|
+
navigation rather than a payload fetch.
|
|
180
137
|
|
|
181
138
|
## The backend's urls
|
|
182
139
|
|
|
183
140
|
A prefix named in `backendPaths` is a `Route` too, so a link or a redirect to a
|
|
184
|
-
|
|
141
|
+
URL the backend owns compiles as written:
|
|
185
142
|
|
|
186
143
|
```tsx
|
|
187
144
|
// vite.config.ts: rscKit({ backendPaths: ['/gitlab'] })
|
|
@@ -190,29 +147,74 @@ redirect('/gitlab/connect'); // ✅
|
|
|
190
147
|
<Link href="/gitlabs">…</Link> // ❌ not the prefix
|
|
191
148
|
```
|
|
192
149
|
|
|
193
|
-
A dynamic segment in one
|
|
150
|
+
A dynamic segment in one matches any string. `backendPaths: ['/login/[provider]']`
|
|
194
151
|
types `/login/google` and `/login/google/callback`, and leaves `/login` to the
|
|
195
152
|
page.
|
|
196
153
|
|
|
197
|
-
The prefix
|
|
198
|
-
under it, and a query or fragment, never `/gitlabs`.
|
|
199
|
-
|
|
200
|
-
loads it as a document and never prefetches it
|
|
154
|
+
The prefix ends at a segment boundary, as it does on the server: `/gitlab`,
|
|
155
|
+
anything under it, and a query or fragment, never `/gitlabs`. Nothing past the
|
|
156
|
+
prefix is checked, because it's the backend's. Like a `route.ts`, the browser
|
|
157
|
+
loads it as a document and never prefetches it. See [leaving the
|
|
201
158
|
app](/guides/redirects/#leaving-the-app).
|
|
202
159
|
|
|
203
|
-
##
|
|
160
|
+
## Refreshing a section by name
|
|
204
161
|
|
|
205
|
-
`revalidate()` in an action and `refresh()` in the browser take a region
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
`'all'`. A typo, or a section renamed since, stops compiling instead of being
|
|
209
|
-
refused by the renderer at runtime:
|
|
162
|
+
`revalidate()` in an action and `refresh()` in the browser take a region name.
|
|
163
|
+
The names are typed to the ones the build found: every `section('orders', …)`,
|
|
164
|
+
every `@slot` folder, `'page'` and `'all'`:
|
|
210
165
|
|
|
211
166
|
```ts
|
|
212
167
|
revalidate('orders') // a section the build found
|
|
213
168
|
revalidate('order') // does not compile
|
|
214
169
|
```
|
|
215
170
|
|
|
216
|
-
A name computed at runtime is cast, as an href is:
|
|
217
|
-
RevalidateTarget)`. A name
|
|
218
|
-
refused with the names it does know.
|
|
171
|
+
A name computed at runtime is cast, as an href is:
|
|
172
|
+
`revalidate(name as RevalidateTarget)`. A name the renderer doesn't know is
|
|
173
|
+
still refused, with the names it does know.
|
|
174
|
+
|
|
175
|
+
## Keeping the types current
|
|
176
|
+
|
|
177
|
+
The file is written whenever Vite starts: a build, or the dev server, which
|
|
178
|
+
also rewrites it when you add a route. A typecheck run without either, by an
|
|
179
|
+
agent or in CI, reads what the last start wrote. A link to a route added since
|
|
180
|
+
then fails with `"/admin"` is not assignable to type `Route`.
|
|
181
|
+
|
|
182
|
+
`rsc-kit-typegen` writes the file without starting Vite, from the app's own
|
|
183
|
+
`vite.config`. The scaffold's `typecheck` and `check` scripts run it first. In
|
|
184
|
+
an older project, add it the same way:
|
|
185
|
+
|
|
186
|
+
```json title="package.json"
|
|
187
|
+
"typecheck": "rsc-kit-typegen && tsc --noEmit"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## The build checks
|
|
191
|
+
|
|
192
|
+
`vite build` runs the project's typecheck before it bundles, and stops on an
|
|
193
|
+
error. A link to a route that doesn't exist is a failed build. It's the same
|
|
194
|
+
`tsc --noEmit` the `check` script runs, on the same tsconfig, and it takes
|
|
195
|
+
about a second.
|
|
196
|
+
|
|
197
|
+
```ts title="vite.config.ts"
|
|
198
|
+
rscKit({ typecheck: false }) // turn it off
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
A project with no `tsconfig.json`, or without `typescript` installed, is
|
|
202
|
+
skipped.
|
|
203
|
+
|
|
204
|
+
## Limits
|
|
205
|
+
|
|
206
|
+
- **A dynamic segment widens.** `/posts/[slug]` becomes `` `/posts/${string}` ``,
|
|
207
|
+
so `/posts/a/b` type-checks even though it doesn't match at runtime.
|
|
208
|
+
- **Without the generated file, nothing is checked.** If
|
|
209
|
+
`.rsc-kit/rsc-routes.d.ts` is missing, or your tsconfig's `include` doesn't
|
|
210
|
+
cover `.rsc-kit`, every URL prop accepts any `string` and nothing breaks.
|
|
211
|
+
There's no flag to turn the check on; the file is the switch.
|
|
212
|
+
|
|
213
|
+
## How it works
|
|
214
|
+
|
|
215
|
+
The build writes one line per route into `rsc-routes.d.ts`. For a page with a
|
|
216
|
+
`searchParams` export, that line reads the export's type from the page module.
|
|
217
|
+
Nothing runs, and a page without the export costs nothing. The build also
|
|
218
|
+
writes the `route.ts` files into their own narrower union, `ApiRoute`, which is
|
|
219
|
+
what `apiUrl` takes, and hands the client every route's pattern so it knows
|
|
220
|
+
which links to treat as documents.
|