@rsc-kit/mcp 0.30.39 → 0.30.42
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 +192 -13
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +5 -5
- package/guides/admin.md +121 -17
- package/guides/api-routes.md +21 -0
- package/guides/caching.md +85 -3
- package/guides/coming-from-inertia.md +1 -1
- package/guides/coming-from-next.md +1 -0
- package/guides/edge-caching.md +57 -0
- package/guides/index.json +13 -3
- package/guides/laravel.md +8 -0
- package/guides/pages.md +177 -0
- package/guides/public-forms.md +113 -0
- package/guides/redirects.md +21 -1
- package/guides/your-own-backend.md +17 -0
- package/package.json +2 -2
package/guides/admin-plugins.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Admin plugins
|
|
2
2
|
|
|
3
|
-
> Writing an @rsc-kit/
|
|
3
|
+
> Writing an @rsc-kit/press plugin, publishing it as a package, and selling it.
|
|
4
4
|
|
|
5
5
|
A plugin is a plain object that adds to an [admin panel](/guides/admin/)
|
|
6
6
|
without changing its code. Everything in one is something an app could write
|
|
@@ -22,8 +22,8 @@ An audit log: it records every save and delete, and shows the latest on the
|
|
|
22
22
|
dashboard.
|
|
23
23
|
|
|
24
24
|
```tsx title="src/index.tsx"
|
|
25
|
-
import { definePlugin } from '@rsc-kit/
|
|
26
|
-
import type { RecordChange, Tenant } from '@rsc-kit/
|
|
25
|
+
import { definePlugin } from '@rsc-kit/press'
|
|
26
|
+
import type { RecordChange, Tenant } from '@rsc-kit/press'
|
|
27
27
|
|
|
28
28
|
export function auditLog({ keep = 50 }: { keep?: number } = {}) {
|
|
29
29
|
const entries: (RecordChange & { at: Date })[] = []
|
|
@@ -74,11 +74,11 @@ an app writes `plugins: [auditLog({ keep: 500 })]`.
|
|
|
74
74
|
"type": "module",
|
|
75
75
|
"keywords": ["rsc-kit", "rsc-kit-admin-plugin"],
|
|
76
76
|
"exports": { ".": "./dist/index.js", "./styles.css": "./dist/styles.css" },
|
|
77
|
-
"peerDependencies": { "@rsc-kit/
|
|
77
|
+
"peerDependencies": { "@rsc-kit/press": "^0.30.40", "react": "^19" }
|
|
78
78
|
}
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
- `@rsc-kit/
|
|
81
|
+
- `@rsc-kit/press` and React are **peers**, never dependencies: two copies of
|
|
82
82
|
either in one app break.
|
|
83
83
|
- Ship compiled JavaScript, not TypeScript source.
|
|
84
84
|
- Style with Tailwind classes and the shadcn theme's colours (`text-muted-foreground`,
|
package/guides/admin.md
CHANGED
|
@@ -1,28 +1,37 @@
|
|
|
1
1
|
# Admin panels
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> An admin panel over any data - a database, an ORM, a REST API, a Laravel or Go backend - with server or client tables, tenants and plugins.
|
|
4
4
|
|
|
5
|
-
`@rsc-kit/
|
|
6
|
-
table, a form and a data source. The panel
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
`@rsc-kit/press` builds an admin panel from a list of resources. Each one is a
|
|
6
|
+
table, a form and a data source. The panel decides what each page shows and
|
|
7
|
+
who may see it; a [theme](#themes) draws it - shadcn's sidebar, data table and
|
|
8
|
+
dialogs is the one that ships - and the app keeps everything that is its own:
|
|
9
|
+
where the data lives, who may see it, and whose it is.
|
|
10
|
+
|
|
11
|
+
The package has three entry points:
|
|
12
|
+
|
|
13
|
+
- `@rsc-kit/press` - resources, data sources, tenants, plugins and blocks. Nothing here draws.
|
|
14
|
+
- `@rsc-kit/press/admin` - `definePanel`, and the url schemas its routes export.
|
|
15
|
+
- `@rsc-kit/press/forms` - `defineForm`, for [public forms](/guides/public-forms/) drawn from a schema.
|
|
16
|
+
- `@rsc-kit/press/pages` - `definePages`, for [pages an editor builds from blocks](/guides/pages/).
|
|
17
|
+
- `@rsc-kit/press/shadcn` - the shadcn theme, which draws both.
|
|
9
18
|
|
|
10
19
|
## Install
|
|
11
20
|
|
|
12
|
-
The
|
|
13
|
-
shadcn set up (`create-rsc-kit --tailwind --shadcn` does both):
|
|
21
|
+
The shadcn theme is styled by the app's shadcn variables, so the app has
|
|
22
|
+
Tailwind v4 and shadcn set up (`create-rsc-kit --tailwind --shadcn` does both):
|
|
14
23
|
|
|
15
24
|
```sh
|
|
16
|
-
bun add @rsc-kit/
|
|
25
|
+
bun add @rsc-kit/press
|
|
17
26
|
```
|
|
18
27
|
|
|
19
28
|
```css title="src/app/styles.css"
|
|
20
29
|
@import "tailwindcss";
|
|
21
30
|
@import "shadcn/tailwind.css";
|
|
22
|
-
@import "@rsc-kit/
|
|
31
|
+
@import "@rsc-kit/press/shadcn.css";
|
|
23
32
|
```
|
|
24
33
|
|
|
25
|
-
The last line tells Tailwind to scan the
|
|
34
|
+
The last line tells Tailwind to scan the theme's components. Without it the
|
|
26
35
|
panel renders unstyled.
|
|
27
36
|
|
|
28
37
|
The header's theme menu is Light, Dark or System, and System - the default -
|
|
@@ -35,7 +44,7 @@ with its own.
|
|
|
35
44
|
|
|
36
45
|
```ts title="src/admin/resources/products.ts"
|
|
37
46
|
import * as z from 'zod'
|
|
38
|
-
import { defineResource } from '@rsc-kit/
|
|
47
|
+
import { defineResource } from '@rsc-kit/press'
|
|
39
48
|
|
|
40
49
|
export const products = defineResource<Product>()({
|
|
41
50
|
name: 'products',
|
|
@@ -107,7 +116,7 @@ A data source is five functions. The panel calls nothing else, so it is a
|
|
|
107
116
|
database, an ORM, a REST API or a backend behind `rpc()` alike:
|
|
108
117
|
|
|
109
118
|
```ts title="src/admin/resources/customers.ts"
|
|
110
|
-
import type { DataSource } from '@rsc-kit/
|
|
119
|
+
import type { DataSource } from '@rsc-kit/press'
|
|
111
120
|
|
|
112
121
|
const customersSource: DataSource<Customer, Values> = {
|
|
113
122
|
async list({ q, sort, dir, page, perPage, filters }, { tenant }) {
|
|
@@ -171,6 +180,64 @@ fields: { status: { options: ['draft', { value: 'active', label: 'Live' }] } },
|
|
|
171
180
|
|
|
172
181
|
An optional select starts on "None", which saves nothing.
|
|
173
182
|
|
|
183
|
+
## Files and images
|
|
184
|
+
|
|
185
|
+
A field the schema holds as a string - the url of what was uploaded - can be
|
|
186
|
+
an upload instead:
|
|
187
|
+
|
|
188
|
+
```ts title="src/admin/resources/products.ts"
|
|
189
|
+
form: z.object({ name: z.string(), photo: z.url().nullable().optional() }),
|
|
190
|
+
fields: {
|
|
191
|
+
photo: { type: 'image', storage: uploads, maxSize: 2 * 1024 * 1024 },
|
|
192
|
+
},
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
`storage` is where the file goes. It is one function, and press ships none,
|
|
196
|
+
as it ships no database:
|
|
197
|
+
|
|
198
|
+
```ts title="src/storage.ts"
|
|
199
|
+
import type { Storage } from '@rsc-kit/press'
|
|
200
|
+
|
|
201
|
+
export const uploads: Storage = {
|
|
202
|
+
async put(file, { field, tenant }) {
|
|
203
|
+
const key = `uploads/${crypto.randomUUID()}`
|
|
204
|
+
|
|
205
|
+
await Bun.s3.write(key, file, { type: file.type })
|
|
206
|
+
|
|
207
|
+
return `https://cdn.example.com/${key}`
|
|
208
|
+
},
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
What `put` answers is what the record saves. On a Worker the same function
|
|
213
|
+
writes to an R2 binding; on a server, to a disk the app serves. A Laravel or Go
|
|
214
|
+
backend gets the url, not the file - `rpc()` carries JSON - so point `put` at
|
|
215
|
+
the bucket the backend's own disk reads, and the url means the same on both
|
|
216
|
+
sides.
|
|
217
|
+
|
|
218
|
+
The form shows what the field holds, a preview of a new image as it is chosen,
|
|
219
|
+
and, when the schema allows `null`, a **Remove** button that saves `null`.
|
|
220
|
+
|
|
221
|
+
- **Upload last.** The form is checked first with the file still a file; only
|
|
222
|
+
when nothing else is wrong is it stored. A refused save leaves nothing in
|
|
223
|
+
the bucket.
|
|
224
|
+
- **Checked twice.** `accept` and `maxSize` are checked in the browser as the
|
|
225
|
+
file is chosen and again on the server. An image field takes PNG, JPEG,
|
|
226
|
+
WebP, GIF and AVIF unless it says `accept`. Not SVG: an SVG can carry script,
|
|
227
|
+
and served from your own origin it runs as your app.
|
|
228
|
+
- **Kept values are checked.** A save that chooses nothing sends back what
|
|
229
|
+
the field held, and the server keeps it only if the record already had it.
|
|
230
|
+
A posted url is not an upload.
|
|
231
|
+
- **The body limit.** Files arrive in the action's body, 8 MB unless the host's
|
|
232
|
+
`maxActionBody` says more. Set `maxSize` below it, so the visitor reads which
|
|
233
|
+
field is too large rather than a refused request.
|
|
234
|
+
- **Replaced files stay.** Removing or replacing an upload does not delete the
|
|
235
|
+
old file; a bucket lifecycle rule, or a job that sweeps keys no record
|
|
236
|
+
names, does that.
|
|
237
|
+
|
|
238
|
+
The field works the same in a [block](/guides/pages/) and in a
|
|
239
|
+
[public form](/guides/public-forms/).
|
|
240
|
+
|
|
174
241
|
## Relations
|
|
175
242
|
|
|
176
243
|
A belongsTo is a field that names another resource of the panel:
|
|
@@ -200,7 +267,7 @@ done to them below the table, with the panel's own **Delete**. Add your own:
|
|
|
200
267
|
|
|
201
268
|
```ts
|
|
202
269
|
bulkActions: [
|
|
203
|
-
{ name: 'publish', label: 'Publish', run: (rows) => rpc('Products.publish', rows.map((row) => row.id)) },
|
|
270
|
+
{ name: 'publish', label: 'Publish', done: 'published', run: (rows) => rpc('Products.publish', rows.map((row) => row.id)) },
|
|
204
271
|
{ name: 'archive', label: 'Archive', destructive: true, run: archiveAll },
|
|
205
272
|
],
|
|
206
273
|
```
|
|
@@ -209,7 +276,8 @@ bulkActions: [
|
|
|
209
276
|
visitor: an id the browser sent that is not theirs, or is already gone, never
|
|
210
277
|
reaches it. It runs after `authorize`, like every save. A `destructive` action
|
|
211
278
|
asks first, as Delete does. When it is done the table refreshes and a toast
|
|
212
|
-
says "
|
|
279
|
+
says "3 products published." from `done`, as Delete says "deleted" - or
|
|
280
|
+
whatever `run` returns as a string.
|
|
213
281
|
|
|
214
282
|
## Server or client tables
|
|
215
283
|
|
|
@@ -232,10 +300,12 @@ data that stays small - hundreds of rows, not thousands.
|
|
|
232
300
|
The panel has no login of its own. It asks the app's:
|
|
233
301
|
|
|
234
302
|
```ts title="src/admin/panel.ts"
|
|
235
|
-
import { definePanel } from '@rsc-kit/admin'
|
|
303
|
+
import { definePanel } from '@rsc-kit/press/admin'
|
|
304
|
+
import { shadcn } from '@rsc-kit/press/shadcn'
|
|
236
305
|
|
|
237
306
|
export const panel = definePanel({
|
|
238
307
|
path: '/admin',
|
|
308
|
+
theme: shadcn,
|
|
239
309
|
resources: [products, customers],
|
|
240
310
|
authorize: async () => (await currentUser())?.isAdmin === true,
|
|
241
311
|
user: currentUser,
|
|
@@ -287,8 +357,42 @@ src/app/admin/[resource]/[id]/page.tsx export const params = recordPar
|
|
|
287
357
|
export default panel.Edit
|
|
288
358
|
```
|
|
289
359
|
|
|
290
|
-
|
|
291
|
-
|
|
360
|
+
`resourceParams`, `recordParams`, `listSearch` and `pluginsSearch` come from
|
|
361
|
+
`@rsc-kit/press/admin`. The section must be named `admin-table.section.tsx`: a
|
|
362
|
+
delete refreshes the table by that name.
|
|
363
|
+
|
|
364
|
+
## Themes
|
|
365
|
+
|
|
366
|
+
`theme` is what draws the panel. The panel works out everything a page needs -
|
|
367
|
+
whether the visitor may see it, which tenant's rows, the save already bound to
|
|
368
|
+
its record - and hands the theme the result as props. A theme is an object of
|
|
369
|
+
components, typed `AdminTheme` from `@rsc-kit/press/admin`:
|
|
370
|
+
|
|
371
|
+
| Component | Draws |
|
|
372
|
+
| --- | --- |
|
|
373
|
+
| `Shell` | The frame: `brand`, `nav`, `user` and `crumbs` arrive drawn, and the page as `children` |
|
|
374
|
+
| `Brand`, `TenantSwitcher`, `Nav`, `Breadcrumbs`, `UserMenu` | What goes in the frame |
|
|
375
|
+
| `Dashboard`, `Index`, `Form`, `Plugins` | Each page |
|
|
376
|
+
| `Table` | A resource's rows, in server or client mode |
|
|
377
|
+
| `Cell` | A `money`, `badge`, `boolean` or `date` value |
|
|
378
|
+
| `Loading` | What shows while a page waits for the request |
|
|
379
|
+
|
|
380
|
+
Every prop is data, a server action already bound, or an element already
|
|
381
|
+
drawn - never a component - so any of them may be a client component. The
|
|
382
|
+
panel keeps the Suspense boundaries, because it knows which parts read the
|
|
383
|
+
request; a theme only draws what shows meanwhile.
|
|
384
|
+
|
|
385
|
+
To change one page, spread the shipped theme and replace it:
|
|
386
|
+
|
|
387
|
+
```tsx title="src/admin/panel.ts"
|
|
388
|
+
import { shadcn } from '@rsc-kit/press/shadcn'
|
|
389
|
+
import { BrandedDashboard } from './branded-dashboard'
|
|
390
|
+
|
|
391
|
+
export const panel = definePanel({
|
|
392
|
+
theme: { ...shadcn, Dashboard: BrandedDashboard },
|
|
393
|
+
// ...
|
|
394
|
+
})
|
|
395
|
+
```
|
|
292
396
|
|
|
293
397
|
## Plugins
|
|
294
398
|
|
package/guides/api-routes.md
CHANGED
|
@@ -63,6 +63,27 @@ Reading a body is the web api: `request.json()`, `.formData()`, `.text()`.
|
|
|
63
63
|
A route that exports no method at all fails the build rather than becoming a url
|
|
64
64
|
that answers 405 to everything.
|
|
65
65
|
|
|
66
|
+
## Posts from another site
|
|
67
|
+
|
|
68
|
+
A `POST`, `PUT`, `PATCH` or `DELETE` that a browser sends from another site is
|
|
69
|
+
answered **403** before your handler runs. Without that, any page on the web
|
|
70
|
+
could post a form to your route with your visitor's cookies, and a handler
|
|
71
|
+
reading `cookies()` would act as them. No token is involved: the browser's
|
|
72
|
+
`Sec-Fetch-Site` header says where the request came from, and an older browser's
|
|
73
|
+
`Origin` is compared with your host. It is the rule Go's
|
|
74
|
+
`http.CrossOriginProtection` and Laravel 13 use, and actions and forms are held
|
|
75
|
+
to it too.
|
|
76
|
+
|
|
77
|
+
A caller that is not a browser - a webhook, a cron job, another server - sends
|
|
78
|
+
neither header and is never refused. A browser app on another origin that should
|
|
79
|
+
be able to post is named:
|
|
80
|
+
|
|
81
|
+
```ts title="vite.config.ts"
|
|
82
|
+
rscKit({ trustedOrigins: ['https://admin.example.com'] })
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`GET`, `HEAD` and `OPTIONS` change nothing and are answered from anywhere.
|
|
86
|
+
|
|
66
87
|
## Dynamic segments
|
|
67
88
|
|
|
68
89
|
`[name]` and `[...rest]` work as they do for pages, and bind into the second
|
package/guides/caching.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Asking once per request
|
|
2
2
|
|
|
3
|
-
> cache() — one lookup, however many places need it.
|
|
3
|
+
> cache() — one lookup, however many places need it, and with a key and a lifetime, however many requests.
|
|
4
4
|
|
|
5
5
|
Middleware checks who you are. The layout wants their name. The page wants their
|
|
6
6
|
permissions. That is three calls and one answer.
|
|
@@ -30,8 +30,8 @@ export default async function Page() {
|
|
|
30
30
|
|
|
31
31
|
## One request, and no further
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
the next one. The scope opens when the request arrives and is torn down with it.
|
|
33
|
+
Without options, two requests in flight never see each other's answers, and
|
|
34
|
+
nothing survives into the next one. The scope opens when the request arrives and is torn down with it.
|
|
35
35
|
|
|
36
36
|
That is the whole safety story: a table that outlived its request would not be a
|
|
37
37
|
stale cache, it would be one visitor seeing another's data.
|
|
@@ -55,3 +55,85 @@ than an object when you want the reuse.
|
|
|
55
55
|
Same idea, wider scope. Middleware runs before any component does, which is
|
|
56
56
|
exactly where the duplicate lookups start — and React has no scope open yet.
|
|
57
57
|
</Aside>
|
|
58
|
+
|
|
59
|
+
## Across requests
|
|
60
|
+
|
|
61
|
+
Some answers are the same for every visitor: a page built in the panel, a menu,
|
|
62
|
+
a price list. Give `cache()` a key and a lifetime, and the answer is kept
|
|
63
|
+
between requests:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { cache } from '@rsc-kit/core/cache'
|
|
67
|
+
|
|
68
|
+
export const findPage = cache(
|
|
69
|
+
async (slug: string) => db.pages.findBySlug(slug),
|
|
70
|
+
{ key: 'pages.find', revalidate: 3600, tags: ['pages'] },
|
|
71
|
+
)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Each request still asks once, as above. Across requests, the answer for each
|
|
75
|
+
argument list is kept for `revalidate` seconds. When the data changes, say so,
|
|
76
|
+
and the next ask misses at once:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
'use server'
|
|
80
|
+
import { changed } from '@rsc-kit/core/changed'
|
|
81
|
+
|
|
82
|
+
export async function savePage(values: PageValues) {
|
|
83
|
+
await db.pages.save(values)
|
|
84
|
+
await changed('pages')
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`changed()` is the same call that refreshes open tabs for
|
|
89
|
+
[live data](/guides/live-data). Nothing is purged. Each kept answer's key
|
|
90
|
+
includes its tags' versions, so a change makes a new key, and the old entry is
|
|
91
|
+
never read again. `tags` can also be a function of the arguments, for a tag per
|
|
92
|
+
record:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
cache(findBySlug, { key: 'pages.find', revalidate: 3600, tags: (slug) => [`page:${slug}`] })
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
- **`key`** names the function in the store. Use the same key on every
|
|
99
|
+
instance, and a different key for every function.
|
|
100
|
+
- **Arguments and answers are JSON.** A `Date` comes back as a string, on the
|
|
101
|
+
first call as well as later ones.
|
|
102
|
+
- **It runs as nobody.** `cookies()` and `headers()` throw inside it, and a call
|
|
103
|
+
to Laravel or Go carries no session. A kept answer is handed to every visitor,
|
|
104
|
+
so it must not be about one of them.
|
|
105
|
+
- **A deploy starts empty.** Keys include the build, so a new deploy never reads
|
|
106
|
+
what an old one kept.
|
|
107
|
+
|
|
108
|
+
### Where answers are kept
|
|
109
|
+
|
|
110
|
+
In memory by default: the 1,000 most recently used answers, one copy per
|
|
111
|
+
instance. To share them across instances, install a store. It needs only a
|
|
112
|
+
`get` and a `set`:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
// instrumentation.ts
|
|
116
|
+
import { installCacheStore } from '@rsc-kit/core/cache'
|
|
117
|
+
|
|
118
|
+
// Redis
|
|
119
|
+
installCacheStore(() => ({
|
|
120
|
+
get: (key) => redis.get(key),
|
|
121
|
+
set: (key, value, ttl) => redis.set(key, value, 'EX', ttl),
|
|
122
|
+
}))
|
|
123
|
+
|
|
124
|
+
// Workers KV (60 seconds is KV's shortest expiry)
|
|
125
|
+
installCacheStore(() => ({
|
|
126
|
+
get: (key) => env.CACHE.get(key),
|
|
127
|
+
set: (key, value, ttl) => env.CACHE.put(key, value, { expirationTtl: Math.max(60, ttl) }),
|
|
128
|
+
}))
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
A store that fails makes pages slower, not broken: the engine warns once and
|
|
132
|
+
does the lookup itself.
|
|
133
|
+
|
|
134
|
+
<Aside type="caution" title="Changes on other instances">
|
|
135
|
+
`changed()` reaches every instance only when the versions are shared, as
|
|
136
|
+
set up for [live data](/guides/live-data#where-the-versions-live). With the
|
|
137
|
+
default in-memory versions, another instance sees a change once its kept
|
|
138
|
+
answer expires, after at most `revalidate` seconds.
|
|
139
|
+
</Aside>
|
|
@@ -48,7 +48,7 @@ out of `package.json` when the last page that imports them is gone.
|
|
|
48
48
|
| `Route::get('/orders', [OrdersController::class, 'index'])` + `Inertia::render('Orders/Index', ['orders' => …])` | nothing. The controller's body moves to `app/Rsc/Orders.php::recent()`, and the page calls `rpc('Orders.recent')` — [Calling PHP](/hosts/laravel#calling-php) |
|
|
49
49
|
| props from the controller | `await rpc<T>('Class.method', …args)` inside the server component — as many reads as the page wants; siblings in one render are one batched request |
|
|
50
50
|
| `usePage().props.auth.user` and the rest of `HandleInertiaRequests::share()` | a read in the layout that needs it — `rpc('Shared.auth')` — passed down as props; there is no global props bag |
|
|
51
|
-
| `usePage().props.flash` | the action's own return value
|
|
51
|
+
| `usePage().props.flash` | the action's own return value, for the form that submitted; for the page after a redirect, `Rsc::flash('Saved.')` (or `flash()` in a JS action) and `useFlash()`, which shows it once |
|
|
52
52
|
| `<Link href={route('orders.show', id)}>` (Ziggy) | `<Link href="/orders/1">` from `@rsc-kit/core/Link` — `href` is typed to the route tree, so Ziggy and `@routes` go |
|
|
53
53
|
| `router.visit()`, `router.get()` | `visit(url)` from `@rsc-kit/core/router` |
|
|
54
54
|
| `router.reload({ only: ['orders'] })` | `revalidate('orders')` from the action re-renders the [section](/guides/sections) and sends it back with the answer — one request, the rest of the page untouched |
|
|
@@ -71,6 +71,7 @@ renders can be, and that one is fine.
|
|
|
71
71
|
| `app/robots.ts`, `app/sitemap.ts` | the same files, the same shapes — [robots, sitemap and llms.txt](/guides/seo-files); `app/llms.ts` beside them |
|
|
72
72
|
| `@react-email/render` in a server action | the same call, in a module that starts with `"use ssr"` — [emails](/guides/emails). Next fails the same way where it renders server components; the directive is how this one moves it |
|
|
73
73
|
| `cache` from `react` | `cache` from `@rsc-kit/core/cache` — React's memoises only inside a component render; this one spans the request, so a guard, the layout and the action share one call. The build names server files still importing React's |
|
|
74
|
+
| `unstable_cache`, `revalidateTag` | `cache(fn, { key, revalidate, tags })` from `@rsc-kit/core/cache`, and `changed(tag)` from `@rsc-kit/core/changed`, which also refreshes open tabs. Kept in memory, or in any store with a `get` and a `set` — [across requests](/guides/caching/#across-requests) |
|
|
74
75
|
|
|
75
76
|
## Different on purpose
|
|
76
77
|
|
package/guides/edge-caching.md
CHANGED
|
@@ -57,6 +57,63 @@ static.
|
|
|
57
57
|
Deployments are handled for you: cached responses carry a build version, so a
|
|
58
58
|
new deploy does not leave old shells being served against a new payload.
|
|
59
59
|
|
|
60
|
+
## Keeping a page that is rendered per request
|
|
61
|
+
|
|
62
|
+
A page whose data changes after the build is rendered for each request, and
|
|
63
|
+
sent `private`. When the same answer may go to everyone for a while, say how
|
|
64
|
+
long:
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
// app/posts/[slug]/page.tsx
|
|
68
|
+
export const revalidate = 60 // seconds a shared cache may keep it
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The page is then rendered whole before it is sent, and judged. When the render
|
|
72
|
+
read nothing that belongs to one visitor, it goes out as:
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=86400
|
|
76
|
+
ETag: "…"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
A CDN keeps it for 60 seconds and refreshes it in the background after that;
|
|
80
|
+
a browser never keeps it. A cache revalidating a copy that has not changed is
|
|
81
|
+
answered `304`. A 404 - including the not-found.tsx a navigation shows - is
|
|
82
|
+
kept for at most a minute, so a scanner walking random urls cannot hold your
|
|
83
|
+
pages out. A layout can export it too: the smallest value on the way down to
|
|
84
|
+
the page wins, and `0` anywhere means the page is rendered per request as
|
|
85
|
+
before. It must be a number literal the build can read.
|
|
86
|
+
|
|
87
|
+
**A revalidate page calls its backend as nobody.** Its host calls carry no
|
|
88
|
+
cookie and no `Authorization`, and a cookie the backend sets is dropped -
|
|
89
|
+
`auth()->user()` is null there, as it is for every visitor a shared copy is
|
|
90
|
+
served to. A page that needs to know who is asking is not one to keep.
|
|
91
|
+
|
|
92
|
+
A form needs no token for this. The engine's actions, and Laravel 13's
|
|
93
|
+
forgery check, accept a same-origin post by its `Sec-Fetch-Site` header, so a
|
|
94
|
+
form in a kept page posts as the visitor. A backend that still checks a token
|
|
95
|
+
tied to the session should be asked for it by a component that awaits
|
|
96
|
+
`connection()`, inside `<Suspense>`. In a static page that part is a hole
|
|
97
|
+
filled per request with the visitor's own session; in a revalidate page it is
|
|
98
|
+
a read of the visitor, so the page is sent private.
|
|
99
|
+
|
|
100
|
+
Any of these sends the page as before, `private, no-store`, with a
|
|
101
|
+
`Cache-Status` header naming the reason and one line in the log per page and
|
|
102
|
+
reason:
|
|
103
|
+
|
|
104
|
+
| `Cache-Status: rsc-kit; fwd=bypass; detail=` | Why |
|
|
105
|
+
| --- | --- |
|
|
106
|
+
| `cookies`, `headers`, `request`, `connection` | the render read the visitor - in the page, a layout, a Suspense'd child, metadata or `middleware.ts`; the log names the component |
|
|
107
|
+
| `host-middleware` | the route has host middleware, which runs against the visitor's session |
|
|
108
|
+
| `set-cookie` | the answer set a cookie |
|
|
109
|
+
| `nonce` | the answer carries a CSP nonce, which a shared copy would give everyone |
|
|
110
|
+
| `status` | only a 200 or a 404 is kept, never a redirect or a 5xx |
|
|
111
|
+
| `failed-render` | React reported a failure inside the payload |
|
|
112
|
+
| `payload-url` | a navigation's payload was asked for without its `?_rsc=` key, or a document with one; the router always adds it |
|
|
113
|
+
|
|
114
|
+
The page is not prerendered: `revalidate` says its data changes, so a frozen
|
|
115
|
+
copy would be wrong from the first change.
|
|
116
|
+
|
|
60
117
|
## Finishing a shell at the edge
|
|
61
118
|
|
|
62
119
|
A shell has holes in it, and something has to fill them. That happens at your
|
package/guides/index.json
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
{
|
|
3
3
|
"slug": "admin",
|
|
4
4
|
"title": "Admin panels",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "An admin panel over any data - a database, an ORM, a REST API, a Laravel or Go backend - with server or client tables, tenants and plugins."
|
|
6
6
|
},
|
|
7
7
|
{
|
|
8
8
|
"slug": "admin-plugins",
|
|
9
9
|
"title": "Admin plugins",
|
|
10
|
-
"description": "Writing an @rsc-kit/
|
|
10
|
+
"description": "Writing an @rsc-kit/press plugin, publishing it as a package, and selling it."
|
|
11
11
|
},
|
|
12
12
|
{
|
|
13
13
|
"slug": "api-routes",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
{
|
|
38
38
|
"slug": "caching",
|
|
39
39
|
"title": "Asking once per request",
|
|
40
|
-
"description": "cache() — one lookup, however many places need it."
|
|
40
|
+
"description": "cache() — one lookup, however many places need it, and with a key and a lifetime, however many requests."
|
|
41
41
|
},
|
|
42
42
|
{
|
|
43
43
|
"slug": "coming-from-inertia",
|
|
@@ -174,11 +174,21 @@
|
|
|
174
174
|
"title": "OpenAPI",
|
|
175
175
|
"description": "A document derived from your route.ts files, and Scalar's page over it."
|
|
176
176
|
},
|
|
177
|
+
{
|
|
178
|
+
"slug": "pages",
|
|
179
|
+
"title": "Pages from blocks",
|
|
180
|
+
"description": "Landing pages, an about page, a pricing page - built in the admin panel from the app's own blocks, live on the next visit."
|
|
181
|
+
},
|
|
177
182
|
{
|
|
178
183
|
"slug": "ppr",
|
|
179
184
|
"title": "Partial prerendering",
|
|
180
185
|
"description": "Storing what every visitor sees the same, and rendering the rest per request."
|
|
181
186
|
},
|
|
187
|
+
{
|
|
188
|
+
"slug": "public-forms",
|
|
189
|
+
"title": "Public forms",
|
|
190
|
+
"description": "A contact form, a sign-up, a survey - drawn from a Standard Schema, checked in the browser and on the server, sent to any backend."
|
|
191
|
+
},
|
|
182
192
|
{
|
|
183
193
|
"slug": "pwa",
|
|
184
194
|
"title": "Progressive web apps",
|
package/guides/laravel.md
CHANGED
|
@@ -350,6 +350,14 @@ region with it rather than the browser being told to ask again — the same
|
|
|
350
350
|
thing `revalidate()` does from a JavaScript action, described in
|
|
351
351
|
[Sections](/guides/sections).
|
|
352
352
|
|
|
353
|
+
`Rsc::flash('Invoice sent.')` leaves a message for the page the visitor lands
|
|
354
|
+
on next - after a redirect, or in place - and `useFlash()` from
|
|
355
|
+
`@rsc-kit/core/useFlash` shows it once, the same message `flash()` leaves from
|
|
356
|
+
a JavaScript action ([A message for the next page](/guides/server-actions#a-message-for-the-next-page)).
|
|
357
|
+
A second argument is the kind: `'error'`, `'info'` or `'warning'`. It works
|
|
358
|
+
from a controller too, on its own response. The cookie is read by the browser,
|
|
359
|
+
so the package leaves it unencrypted: nothing secret goes in a flash.
|
|
360
|
+
|
|
353
361
|
## Saying data changed
|
|
354
362
|
|
|
355
363
|
`Rsc::revalidate()` is for the action that made the change. A webhook, a
|