@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.
Files changed (60) hide show
  1. package/dist/recipes.js +12 -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 +251 -218
  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
@@ -1,9 +1,9 @@
1
1
  # Navigation
2
2
 
3
- > What happens when someone clicks a link, and why state survives it.
3
+ > Moving between pages without a reload - visit, prefetch, progress, refresh, and measuring a click.
4
4
 
5
- `Link` does not reload the page. It asks the server for the part of the tree
6
- that actually changed, and swaps it in.
5
+ `Link` moves to another page without reloading the document. Only the part
6
+ below the layouts both pages share is fetched and rendered:
7
7
 
8
8
  ```tsx
9
9
  import Link from '@rsc-kit/core/Link'
@@ -11,142 +11,87 @@ import Link from '@rsc-kit/core/Link'
11
11
  <Link href="/about">About</Link>
12
12
  ```
13
13
 
14
- ## What a navigation actually sends
14
+ The `Link` props are listed in [Routing](/guides/routing#linking-between-pages).
15
+ This page covers navigating from code, prefetching, progress, refreshing, and
16
+ measuring a navigation.
15
17
 
16
- Going from `/posts/one` to `/posts/two`, the root layout and the posts layout
17
- are already mounted and identical. The client says which layouts it holds, the
18
- server compares that with the new route's chain, and answers with the deepest
19
- piece that differs — the page alone.
20
-
21
- You get this for free. There is nothing to configure and no `loading` prop to
22
- thread.
23
-
24
- Two things follow from it, and they are the reason it works this way:
25
-
26
- **State above the swap survives.** A sidebar's scroll position, an open menu, a
27
- half-typed form in a layout — none of it is touched, because none of it was
28
- re-rendered.
29
-
30
- **A page you return to is still there.** Recently visited pages are kept mounted
31
- and hidden rather than thrown away, so going back restores a half-filled form
32
- exactly as you left it. Only a handful are kept, ordered by last visit.
33
-
34
- ## Programmatic navigation
18
+ ## Navigating from code
35
19
 
36
20
  ```tsx
37
21
  'use client'
38
-
39
- import { visit, prefetch } from '@rsc-kit/core/router'
22
+ import { visit } from '@rsc-kit/core/router'
40
23
 
41
24
  <button onClick={() => visit('/checkout')}>Checkout</button>
42
25
  ```
43
26
 
44
- Links prefetch on their own — below. `prefetch` is for when you know where
45
- someone is going before they show it:
46
-
47
- ```tsx
48
- useEffect(() => { prefetch('/step-2') }, [])
49
- useEffect(() => { prefetch('/step-2', undefined, { intent: true }) }, []) // and decode it now
27
+ ```ts
28
+ await visit('/dashboard', { replace: true }) // no new history entry
50
29
  ```
51
30
 
52
- ## What a click finds already there
53
-
54
- A navigation is as fast as what is in the browser when the click lands, and
55
- a click that has to wait for the network is what a single-page app never
56
- does. So a `<Link>` fetches its page before it is asked to, in two steps,
57
- because the two cost differently:
58
-
59
- - **The bytes, as the link comes into view.** The first dozen links on
60
- screen per page, on every device, when the browser is idle, once per link,
61
- at low priority so a real navigation overtakes the queue — the way Next
62
- prefetches. A dozen, because a payload carries the whole page: a product
63
- page with its related products is 30 KB, and a listing shows twenty-four of
64
- them. A link past the budget is fetched on touch or on a settled hover, a
65
- round trip before the click rather than before the scroll. The payload is
66
- kept undecoded: thirty seconds for a page rendered per visitor, five
67
- minutes for one the build made (the host marks it `public`, and only a
68
- deploy changes it — which the [version handshake](/hosts/deployment#pages-across-a-deploy)
69
- catches on the click). `cacheFor` on the link sets its own. A link asks
70
- again when it comes back into view and when the tab is visible again, which
71
- costs a request only for a payload that has expired: the nav a visitor
72
- scrolls back to after a minute of reading is fresh when they tap it. Not on
73
- a timer, so a page left open fetches nothing.
74
- - **The pictures, as the bytes land.** Every `<img>` the page shows is a row
75
- in its payload, so the ones it would load at once — not `loading="lazy"` —
76
- are asked for at low priority the moment the payload arrives, the first six a
77
- page — a product's own picture comes before its related ones. On a phone a product picture takes longer than the touch-to-click
78
- a hidden render has, and the click should find it decoded. On intent the
79
- same pictures are asked for again as urgent: a listing's tiles each
80
- preload their product's picture, and the one tapped first was queued
81
- behind the rest; asked for as high priority it moves to the front.
82
- - **The code for its client components, as the bytes land.** A payload names
83
- each client component it renders, and the first eight a page renders are
84
- loaded at idle the moment the payload arrives — the code only, nothing
85
- rendered. Loaded at the touch instead, the first page of a visit waited on
86
- its form's chunk *after* it had arrived, and showed its skeleton for a
87
- quarter of a second; every later page found the chunk cached. Measured on an
88
- iPhone, tapping through a fresh session: the first product page went from
89
- ~325 ms with a skeleton to ~80 ms without one. Bounded per document, so a
90
- landing page with a link to a heavy editor does not load the editor for
91
- everyone.
92
- - **The decode, on intent.** Decoding the payload itself waits for the signal
93
- that says which link is meant: a pointer that has settled on the link for
94
- 100 ms, a press, a touch. Each of those leads its click by 60–300 ms — about
95
- a round trip — which is when the page is rendered hidden, so the click
96
- reveals it.
97
-
98
- The click then reads a decoded tree, changes the url, and hands the page to
99
- React. Nothing on the network is between the click and the page unless the
100
- link was never on screen, or the visitor asked for less data (Save-Data
101
- turns the viewport prefetch off; the hover and the touch still fetch).
102
-
103
- A link to a guarded page — every "Sign in" on a landing page points at the
104
- app, and a signed-out visitor is sent to `/login` — is prefetched the same
105
- way: the guard's answer is kept, and the destination is prefetched too, so
106
- the tap asks for nothing.
107
-
108
- ### Measuring one
109
-
110
- "Slow on my phone" needs a number, and the number it needs is the click to
111
- the page on screen — not a request in a network panel. Every navigation
112
- leaves it on the browser's timeline, in the User Timing API the Performance
113
- panel of any dev tools draws:
114
-
115
- | mark | when |
116
- | --- | --- |
117
- | `rsc-kit:navigate:start` | the click |
118
- | `rsc-kit:navigate:decoded` | the payload decoded and the chunks it names loaded |
119
- | `rsc-kit:navigate:applied` | handed to React |
120
- | `rsc-kit:navigate` (measure) | start to the commit that put the page on screen |
31
+ A default export is available too, if you prefer `router.visit('/about')`.
121
32
 
122
- In a console, after a few clicks:
33
+ ## What a navigation keeps
123
34
 
124
- ```js
125
- performance.getEntriesByName('rsc-kit:navigate').map((m) => Math.round(m.duration))
126
- ```
35
+ Going from `/posts/one` to `/posts/two`, the root layout and the posts layout
36
+ are already on screen, so only the page is replaced. That has two effects:
127
37
 
128
- A document load the router chose — a route.ts or a url named in
129
- `backendPaths`, a foreign origin, a newer build, a payload that was not one, a
130
- chunk the deploy no longer serves — is
131
- announced before it happens, as a `rsc-kit:document-load` event on `window`
132
- with the url and the reason, so a reload under a tester has a name:
38
+ - **State in a layout survives.** A sidebar's scroll position, an open menu or
39
+ a half-typed search box in a layout is left alone, because the layout isn't
40
+ rendered again.
41
+ - **A page you go back to is still there.** The last few pages visited are
42
+ kept mounted and hidden, so Back restores a half-filled form as it was left.
133
43
 
134
- ```js
135
- window.addEventListener('rsc-kit:document-load', (e) => console.log(e.detail))
44
+ ## Prefetching
45
+
46
+ A `<Link>` loads its page before it is clicked, so the click usually finds the
47
+ page ready. To prefetch a page you know someone is going to next:
48
+
49
+ ```tsx
50
+ 'use client'
51
+ import { prefetch } from '@rsc-kit/core/router'
52
+
53
+ useEffect(() => { prefetch('/step-2') }, [])
54
+ useEffect(() => { prefetch('/step-2', undefined, { intent: true }) }, []) // and decode it now
136
55
  ```
137
56
 
138
- A gap before `decoded` is the network or the chunks: the page was not on
139
- screen to be prefetched, or was prefetched and never decoded — a scripted
140
- click, a link with `prefetch={false}`. A gap after `applied` is the render,
141
- which is the page's own size on the device's own CPU. On a phone through
142
- Safari's Web Inspector (Develop → your iPhone → the page), the same entries
143
- are in the Timelines tab.
57
+ The second argument is how long the payload stays usable, in milliseconds.
58
+ `intent: true` also decodes it as it lands, which loads the page's client
59
+ code: use it for a page the visitor is about to open, not one they may never
60
+ open.
61
+
62
+ ### What a click finds already there
63
+
64
+ A link prefetches in steps, because each costs differently:
65
+
66
+ - **The page's data, as the link comes into view.** The first dozen links on
67
+ screen, on every device, when the browser is idle, at low priority so a
68
+ real navigation goes first. A link past the dozen is fetched on touch or on
69
+ a hover. The data is kept for thirty seconds for a page rendered per
70
+ visitor, and five minutes for a page the build made; `cacheFor` on the link
71
+ sets its own. A link asks again when it comes back into view, or the tab
72
+ does, only if what it holds has expired. Nothing is fetched on a timer, so
73
+ a page left open fetches nothing.
74
+ - **The images, as the data arrives.** The first six images the page shows at
75
+ once (not `loading="lazy"`) are requested at low priority, and again at high
76
+ priority when the visitor shows intent.
77
+ - **The client code, as the data arrives.** The code for the first eight client
78
+ components the page renders is loaded while the browser is idle.
79
+ - **The decode, on intent.** The page is decoded and rendered hidden when a
80
+ pointer rests on the link for 100 ms, or on a press or a touch. Each of
81
+ those comes 60-300 ms before the click, so the click shows a page that is
82
+ already rendered.
83
+
84
+ With Save-Data on, links don't prefetch as they come into view; hover and
85
+ touch still do. A link to a guarded page - "Sign in" to an app that sends a
86
+ signed-out visitor to `/login` - prefetches the destination too, so the tap
87
+ asks for nothing.
144
88
 
145
89
  ## Showing progress
146
90
 
91
+ `useLinkStatus` reports whether the nearest `Link` is loading:
92
+
147
93
  ```tsx
148
94
  'use client'
149
-
150
95
  import { useLinkStatus } from '@rsc-kit/core/useLinkStatus'
151
96
 
152
97
  function Spinner() {
@@ -156,44 +101,70 @@ function Spinner() {
156
101
  }
157
102
  ```
158
103
 
159
- A `loading.tsx` beside a page is shown while that page's data resolves:
104
+ A `Link` also has a `data-pending` attribute while it loads. For what the new
105
+ page shows while its data arrives, see
106
+ [showing a loading state](/guides/routing#showing-a-loading-state).
160
107
 
161
- ```tsx title="src/app/posts/loading.tsx"
162
- export default function Loading() {
163
- return <p>Loading posts…</p>
164
- }
108
+ ## Refreshing the current page
109
+
110
+ ```tsx
111
+ 'use client'
112
+ import { refresh } from '@rsc-kit/core/router'
113
+
114
+ <button onClick={() => refresh()}>Reload</button>
165
115
  ```
166
116
 
167
- ## A tap before the page can navigate
117
+ ```ts
118
+ refresh() // the page, leaving the layouts mounted
119
+ refresh('all') // the whole document, layouts included
120
+ refresh('orders') // one section or parallel slot
121
+ ```
168
122
 
169
- The document arrives well before the runtime hydrates — on a phone, with a
170
- tag manager or two on the main thread, seconds before. A tap on a link in
171
- that window used to be the browser's: a document load of a page the router
172
- would have swapped in, which is why a first tap on a slow connection felt
173
- like a full reload. The bootstrap script now listens before anything else
174
- loads: a tap on a `<Link>` is held, and the moment the router is wired it is
175
- navigated to. If the runtime never arrives, the browser gets the tap back
176
- after four seconds as a document load. Only `<Link>`s are held — a plain
177
- anchor, a `route.ts`, a file, a `mailto:` are the browser's as before.
123
+ The scroll positions are restored afterwards: the window's, and any element
124
+ that scrolls on its own, such as a sidebar.
178
125
 
179
- A form whose action is a server action is held the same way. Before
180
- hydration React renders it to post natively, so a page reloads with the
181
- action applied — right with no JavaScript, and a full reload with JavaScript
182
- a second away. Submitted in that window, the form waits for hydration and is
183
- submitted again to the handler React then has on it; a tap on a link after
184
- it wins, since the visitor left the form. With no runtime after three
185
- seconds, it posts natively as before.
126
+ ## Measuring a navigation
186
127
 
128
+ Every navigation leaves marks on the browser's timeline, which the Performance
129
+ panel of any dev tools shows:
187
130
 
188
- ## Refreshing the current page
131
+ | Mark | When |
132
+ | --- | --- |
133
+ | `rsc-kit:navigate:start` | the click |
134
+ | `rsc-kit:navigate:decoded` | the page decoded, and its client code loaded |
135
+ | `rsc-kit:navigate:applied` | handed to React |
136
+ | `rsc-kit:navigate` (measure) | from the click to the page on screen |
189
137
 
190
- ```tsx
191
- 'use client'
138
+ In a console, after a few clicks:
192
139
 
193
- import { refresh } from '@rsc-kit/core/router'
140
+ ```js
141
+ performance.getEntriesByName('rsc-kit:navigate').map((m) => Math.round(m.duration))
142
+ ```
194
143
 
195
- <button onClick={() => refresh()}>Reload</button>
144
+ - **A gap before `decoded`** is the network or the code: the page wasn't
145
+ prefetched, or was never decoded, as with a scripted click or
146
+ `prefetch={false}`.
147
+ - **A gap after `applied`** is rendering the page on that device.
148
+
149
+ On an iPhone, Safari's Web Inspector (Develop → your iPhone → the page) shows
150
+ the same marks in its Timelines tab.
151
+
152
+ When the router does a full document load instead - a `route.ts`, a URL in
153
+ `backendPaths`, another site, a newer build, a file the deploy no longer
154
+ serves - it says why first, with a `rsc-kit:document-load` event:
155
+
156
+ ```js
157
+ window.addEventListener('rsc-kit:document-load', (e) => console.log(e.detail))
196
158
  ```
197
159
 
198
- Scroll positions are restored afterwards — the window's and any element with
199
- its own overflow, such as a sidebar.
160
+ ## Clicks before the page has loaded its JavaScript
161
+
162
+ A tap on a `<Link>` before the page's JavaScript is running is held, and
163
+ navigated the moment the router is ready. If it never becomes ready, the
164
+ browser gets the tap back after four seconds as a normal page load. Only
165
+ `<Link>`s are held: a plain `<a>`, a `route.ts`, a file and `mailto:` behave
166
+ as usual.
167
+
168
+ A form whose action is a server action is held the same way and submitted once
169
+ the page is ready. If the page isn't ready after three seconds, the form posts
170
+ the way a form with no JavaScript does. A tap on a link after submitting wins.
@@ -2,54 +2,43 @@
2
2
 
3
3
  > The default is none. A client component is how a page asks for it.
4
4
 
5
- There is no JavaScript on a page until something on it needs some. A
6
- marketing page, a changelog, an article, a policy: the build stores them as
7
- HTML and stops. No bootstrap script, so no React, no Flight client, no router.
5
+ A page ships no JavaScript until something on it needs some. A marketing page,
6
+ a changelog, an article or a policy is stored as HTML, with no React and no
7
+ router. The build says so on the route's line:
8
8
 
9
9
  ```
10
10
  ○ /about no js
11
11
  no client components, so ships no javascript; stylesheet inlined
12
12
  ```
13
13
 
14
- **`"use client"` is the opt-in.** A route that freezes whole, renders none of
15
- your client components, and has no server action in its tree ships nothing.
16
- Put a client component in it — a counter, a `<Link>`, an update prompt — and
17
- that page has the runtime, because there is now something for the runtime to
18
- do. That is the whole rule, and there is no switch beside it in either
19
- direction, on purpose:
20
-
21
- - Nothing can *need* the runtime without a client component or an action in
22
- its tree, so there is nothing an "on" switch could say that the tree does
23
- not already say.
24
- - A page that must *stay* this way is an assertion, not a setting: the route's
25
- `clientJs` in `build-report.json` is `null` when it shipped none, and a CI
26
- step can hold it there.
27
-
28
- The check reads the rendered tree, not the source, so a client component
29
- reached through a shared layout counts. Most pages under a layout with a
30
- `<Link>` in its header keep the runtime; a route group with its own plain
31
- layout drops it. That is the honest shape of it: the pages that can be
32
- nothing are nothing, and the page with a nav is a page with a nav.
33
-
34
- ## What a page without the runtime still does
35
-
36
- **Its stylesheet is inlined** into the document when it is small on the wire
37
- (at most 10 kB gzipped), so the first paint waits on no request but the
38
- document itself. The build says so on the route's line. Fonts are the one
39
- thing left to you, because only you know which face the first paint needs —
40
- the [fonts guide](/guides/fonts) shows the two-line preload.
41
-
42
- Every other stored page gets the same, shells included. One that hydrates
43
- keeps its `<link>` as well, turned to `media="print"`: React looks for its
44
- stylesheets by href and would fetch a missing one again. The browser loads
45
- the print link at the lowest priority without holding up a paint, and never
46
- applies it on screen. A page rendered per request is not stored, and keeps
47
- its link.
48
-
49
- The default is not always the right call. A site of many small pages — a
50
- blog, documentation — may prefer one stylesheet the browser caches once over
51
- the same 4 kB repeated in every document, and a sheet just over the cap may
52
- be worth inlining anyway. It is one setting on the plugin:
14
+ ## When a page gets JavaScript
15
+
16
+ A page gets the runtime when its tree has one of these:
17
+
18
+ - a client component (`"use client"`), such as a counter, a `<Link>` or an
19
+ update prompt
20
+ - a form with a server action, `<form action={serverFn}>`
21
+ - a Suspense boundary left to fill after the page arrives
22
+
23
+ There is no setting for it in either direction. The check reads the rendered
24
+ tree, not the source, so a client component in a shared layout counts. Most
25
+ pages under a layout with a `<Link>` in its header keep the runtime. A
26
+ [route group](/guides/routing#route-groups) with its own plain layout drops it.
27
+
28
+ Only a page stored whole can have none. A partial prerender has holes to fill
29
+ after the shell arrives, and a page rendered per request is not stored, so both
30
+ keep the runtime.
31
+
32
+ ## Keeping a page this way
33
+
34
+ To make sure a page stays without JavaScript, check the build report in CI. A
35
+ route's `clientJs` in `build-report.json` is `null` when it shipped none.
36
+
37
+ ## Stylesheets
38
+
39
+ A page's stylesheet is inlined into the document when it is at most 10 kB
40
+ gzipped, so the first paint waits for nothing but the document. The build says
41
+ so on the route's line. Change it with one setting:
53
42
 
54
43
  ```ts title="vite.config.ts"
55
44
  rscKit({ inlineStylesheets: 'auto' }) // the default: when at most 10 kB gzipped
@@ -58,21 +47,24 @@ rscKit({ inlineStylesheets: true }) // always, whatever the size
58
47
  rscKit({ inlineStylesheets: 24_000 }) // your own cap, in gzipped bytes
59
48
  ```
60
49
 
61
- **It still registers the [service worker](/guides/offline)** when the app has
62
- one: the runtime's one-line registration is inlined in its place, so a visitor
63
- who lands here first gets the worker the second visit is for. What such a
64
- page cannot show is the update prompt — that is a client component, and a
65
- page that uses it has the runtime.
50
+ A site of many small pages, such as a blog or documentation, may prefer one
51
+ stylesheet the browser caches once to the same 4 kB in every document.
52
+
53
+ - Every stored page is inlined, shells included.
54
+ - A page that hydrates keeps its `<link>` too, as `media="print"`: React looks
55
+ for its stylesheets by href and would otherwise fetch the file again. The
56
+ browser loads it at the lowest priority and never applies it on screen.
57
+ - A page rendered per request keeps its link.
66
58
 
67
- **Navigation is unchanged in both directions.** A page with no client
68
- component has no `<Link>`, so its anchors were already full loads. A `<Link>`
69
- elsewhere that points *at* it still fetches its flight payload and swaps the
70
- segment, because that payload is written from the render with the runtime.
59
+ Fonts are left to you, since only you know which face the first paint needs.
60
+ The [fonts guide](/guides/fonts) shows the two-line preload.
71
61
 
72
- ## Only a page stored whole
62
+ ## What still works
73
63
 
74
- With no client runtime there is nothing to fill a Suspense hole after the
75
- shell arrives, so a partial prerender always keeps it, and so does a page
76
- rendered per request. The line is what the runtime would *do*: hydrate a
77
- client component, submit a `<form action={serverFn}>`, fill a hole. A page
78
- with none of those is HTML, and is stored as HTML.
64
+ - **The [service worker](/guides/offline) registers** when the app has one. Its
65
+ one-line registration is inlined, so a visitor who lands here first still
66
+ gets the worker. The update prompt is a client component, so a page that
67
+ shows it has the runtime.
68
+ - **Navigation works both ways.** The page's own links are ordinary anchors,
69
+ which load the next page in full. A `<Link>` on another page that points here
70
+ still navigates to it without a reload.