@rsc-kit/mcp 0.30.37 → 0.30.39
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 +164 -1
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +109 -0
- package/guides/admin.md +297 -0
- package/guides/index.json +10 -0
- package/guides/redirects.md +6 -0
- package/guides/server-actions.md +47 -0
- package/package.json +2 -2
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Admin plugins
|
|
2
|
+
|
|
3
|
+
> Writing an @rsc-kit/admin plugin, publishing it as a package, and selling it.
|
|
4
|
+
|
|
5
|
+
A plugin is a plain object that adds to an [admin panel](/guides/admin/)
|
|
6
|
+
without changing its code. Everything in one is something an app could write
|
|
7
|
+
itself; a plugin is the same thing in a package, so it can be installed,
|
|
8
|
+
versioned and sold.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
interface PanelPlugin {
|
|
12
|
+
name: string // unique in a panel: the package name
|
|
13
|
+
resources?: Resource[] // shown as if the app defined them
|
|
14
|
+
widgets?: Widget[] // dashboard cards
|
|
15
|
+
onChange?: (change: RecordChange) => void | Promise<void>
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## A whole plugin
|
|
20
|
+
|
|
21
|
+
An audit log: it records every save and delete, and shows the latest on the
|
|
22
|
+
dashboard.
|
|
23
|
+
|
|
24
|
+
```tsx title="src/index.tsx"
|
|
25
|
+
import { definePlugin } from '@rsc-kit/admin'
|
|
26
|
+
import type { RecordChange, Tenant } from '@rsc-kit/admin'
|
|
27
|
+
|
|
28
|
+
export function auditLog({ keep = 50 }: { keep?: number } = {}) {
|
|
29
|
+
const entries: (RecordChange & { at: Date })[] = []
|
|
30
|
+
|
|
31
|
+
function Recent({ tenant }: { tenant: Tenant | null }) {
|
|
32
|
+
const mine = entries.filter((entry) => entry.tenant?.id === tenant?.id).slice(0, 6)
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<ul className="space-y-2 text-sm">
|
|
36
|
+
{mine.map((entry, i) => (
|
|
37
|
+
<li key={i}>{entry.change} {entry.resource} #{entry.id}</li>
|
|
38
|
+
))}
|
|
39
|
+
</ul>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return definePlugin({
|
|
44
|
+
name: 'audit-log',
|
|
45
|
+
widgets: [{ title: 'Recent activity', component: Recent }],
|
|
46
|
+
onChange(change) {
|
|
47
|
+
entries.unshift({ ...change, at: new Date() })
|
|
48
|
+
entries.length = Math.min(entries.length, keep)
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Export a function that takes the plugin's options and returns the plugin, so
|
|
55
|
+
an app writes `plugins: [auditLog({ keep: 500 })]`.
|
|
56
|
+
|
|
57
|
+
- **A widget is a server component**, given the current `tenant`. It can be
|
|
58
|
+
`async` and read data; show that tenant's numbers, never everyone's.
|
|
59
|
+
- **`onChange` runs after the record is written.** A plugin that throws is
|
|
60
|
+
logged and does not undo the change or fail the save - the person saving is
|
|
61
|
+
not the plugin's audience. Do slow work (an email, a webhook) without making
|
|
62
|
+
the save wait on it.
|
|
63
|
+
- **`change.tenant`** is whose record it was, in a panel with tenants. Keep
|
|
64
|
+
what you store split by it.
|
|
65
|
+
- **Resources** a plugin adds are ordinary `defineResource` resources. A name
|
|
66
|
+
shared with one of the app's is refused when the panel is defined, as is the
|
|
67
|
+
same plugin installed twice.
|
|
68
|
+
|
|
69
|
+
## Publishing it
|
|
70
|
+
|
|
71
|
+
```json title="package.json"
|
|
72
|
+
{
|
|
73
|
+
"name": "rsc-kit-admin-audit-log",
|
|
74
|
+
"type": "module",
|
|
75
|
+
"keywords": ["rsc-kit", "rsc-kit-admin-plugin"],
|
|
76
|
+
"exports": { ".": "./dist/index.js", "./styles.css": "./dist/styles.css" },
|
|
77
|
+
"peerDependencies": { "@rsc-kit/admin": "^0.1.0", "react": "^19" }
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
- `@rsc-kit/admin` and React are **peers**, never dependencies: two copies of
|
|
82
|
+
either in one app break.
|
|
83
|
+
- Ship compiled JavaScript, not TypeScript source.
|
|
84
|
+
- Style with Tailwind classes and the shadcn theme's colours (`text-muted-foreground`,
|
|
85
|
+
`bg-card`), so the plugin matches the app it is installed in. If it has
|
|
86
|
+
components, ship a `styles.css` with `@source "./";` and tell the app to
|
|
87
|
+
import it, as the panel itself does.
|
|
88
|
+
- The `rsc-kit-admin-plugin` keyword is how plugins are found: search npm for
|
|
89
|
+
`keywords:rsc-kit-admin-plugin`.
|
|
90
|
+
|
|
91
|
+
## Selling it
|
|
92
|
+
|
|
93
|
+
A paid plugin is an npm package behind a licence. The ways that work today,
|
|
94
|
+
the same as Filament's paid plugins:
|
|
95
|
+
|
|
96
|
+
- **A private registry with a licence key** - Anystack, Lemon Squeezy, or
|
|
97
|
+
your own. The buyer adds a scoped registry to `.npmrc` with their key as the
|
|
98
|
+
token, and installs as usual:
|
|
99
|
+
|
|
100
|
+
```ini title=".npmrc"
|
|
101
|
+
@acme:registry=https://npm.acme.dev/
|
|
102
|
+
//npm.acme.dev/:_authToken=${ACME_LICENSE_KEY}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
- **A private GitHub repository** the buyer is invited to on purchase, installed
|
|
106
|
+
with `bun add github:acme/admin-plugin`.
|
|
107
|
+
|
|
108
|
+
Keep a free listing on npm - the README and the keyword - pointing at where it
|
|
109
|
+
is sold, so it is found the same way a free plugin is.
|
package/guides/admin.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# Admin panels
|
|
2
|
+
|
|
3
|
+
> A shadcn 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
|
+
|
|
5
|
+
`@rsc-kit/admin` builds an admin panel from a list of resources. Each one is a
|
|
6
|
+
table, a form and a data source. The panel draws the pages with shadcn - the
|
|
7
|
+
sidebar, the data table, the dialogs - and the app keeps everything that is
|
|
8
|
+
its own: where the data lives, who may see it, and whose it is.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
The panel is styled by the app's shadcn theme, so the app has Tailwind v4 and
|
|
13
|
+
shadcn set up (`create-rsc-kit --tailwind --shadcn` does both):
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
bun add @rsc-kit/admin
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```css title="src/app/styles.css"
|
|
20
|
+
@import "tailwindcss";
|
|
21
|
+
@import "shadcn/tailwind.css";
|
|
22
|
+
@import "@rsc-kit/admin/styles.css";
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The last line tells Tailwind to scan the panel's components. Without it the
|
|
26
|
+
panel renders unstyled.
|
|
27
|
+
|
|
28
|
+
The header's theme menu is Light, Dark or System, and System - the default -
|
|
29
|
+
follows the OS. It sets the `dark` class on `<html>` before the first paint,
|
|
30
|
+
so give the root layout's `<html>` `suppressHydrationWarning`, as shadcn's own
|
|
31
|
+
theme setup does. `definePanel({ modeToggle: false })` hides it for an app
|
|
32
|
+
with its own.
|
|
33
|
+
|
|
34
|
+
## A resource
|
|
35
|
+
|
|
36
|
+
```ts title="src/admin/resources/products.ts"
|
|
37
|
+
import * as z from 'zod'
|
|
38
|
+
import { defineResource } from '@rsc-kit/admin'
|
|
39
|
+
|
|
40
|
+
export const products = defineResource<Product>()({
|
|
41
|
+
name: 'products',
|
|
42
|
+
key: 'id',
|
|
43
|
+
title: 'name',
|
|
44
|
+
source: productsSource,
|
|
45
|
+
table: (col) => [
|
|
46
|
+
col.text('name').searchable().sortable(),
|
|
47
|
+
col.money('price').sortable(),
|
|
48
|
+
col.badge('status'),
|
|
49
|
+
col.boolean('featured').hidden(),
|
|
50
|
+
],
|
|
51
|
+
filters: ['status'],
|
|
52
|
+
form: z.object({
|
|
53
|
+
name: z.string().min(2),
|
|
54
|
+
price: z.number().positive(),
|
|
55
|
+
status: z.enum(['draft', 'active']),
|
|
56
|
+
featured: z.boolean(),
|
|
57
|
+
}),
|
|
58
|
+
fields: { description: { type: 'textarea' } },
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Columns are `text`, `money`, `badge`, `boolean`, `date` and `relation`. `.hidden()` starts
|
|
63
|
+
a column switched off in the Columns menu, and `.cell((value, row) => ...)`
|
|
64
|
+
draws a cell yourself - a server component, so it can use the app's own shadcn
|
|
65
|
+
components.
|
|
66
|
+
|
|
67
|
+
## The form is any Standard Schema
|
|
68
|
+
|
|
69
|
+
`form` is a Standard Schema of an object: zod, valibot, arktype, Effect Schema
|
|
70
|
+
- whatever the app already uses. The panel never imports a validation library.
|
|
71
|
+
The same schema checks every save on the server, and each refusal shows under
|
|
72
|
+
the field it is about.
|
|
73
|
+
|
|
74
|
+
The fields are read from the schema. A schema that describes itself as JSON
|
|
75
|
+
Schema - **Standard JSON Schema**, which zod 4.2+ implements - gives the form
|
|
76
|
+
its fields, their types, which are required and an enum's options. A schema
|
|
77
|
+
that does not still validates; list its fields in `fields`, and the panel says
|
|
78
|
+
so if you forget:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
form: v.object({ title: v.pipe(v.string(), v.minLength(1)) }),
|
|
82
|
+
fields: { title: { type: 'text' } },
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Checked in the browser first
|
|
86
|
+
|
|
87
|
+
The constraints the browser can check itself come from the schema too, as
|
|
88
|
+
attributes on the controls: `required`, `min` and `max`, `minlength` and
|
|
89
|
+
`maxlength`, an anchored `pattern`, `type="email"`, and `step="1"` for an
|
|
90
|
+
integer. A submit with a wrong field never leaves the page, and the browser's
|
|
91
|
+
message - in the visitor's language - shows under the field in the same
|
|
92
|
+
`FieldError` the server's refusals use. With JavaScript off, the browser shows
|
|
93
|
+
its own bubble instead.
|
|
94
|
+
|
|
95
|
+
The schema itself never reaches the browser, so this is the same for a schema
|
|
96
|
+
in the app and for rules that live in Laravel or Go. It is a courtesy only: a
|
|
97
|
+
custom message (`'Like ABC-123'`) and anything the browser cannot know - a
|
|
98
|
+
unique email - come from the server, which checks every save.
|
|
99
|
+
|
|
100
|
+
A number field arrives as a number, so the schema says `z.number()` - not
|
|
101
|
+
`z.coerce.number()` - and is the same schema an API would use. A switch that is
|
|
102
|
+
off is `false`, and an empty optional field is left out.
|
|
103
|
+
|
|
104
|
+
## Any data source
|
|
105
|
+
|
|
106
|
+
A data source is five functions. The panel calls nothing else, so it is a
|
|
107
|
+
database, an ORM, a REST API or a backend behind `rpc()` alike:
|
|
108
|
+
|
|
109
|
+
```ts title="src/admin/resources/customers.ts"
|
|
110
|
+
import type { DataSource } from '@rsc-kit/admin'
|
|
111
|
+
|
|
112
|
+
const customersSource: DataSource<Customer, Values> = {
|
|
113
|
+
async list({ q, sort, dir, page, perPage, filters }, { tenant }) {
|
|
114
|
+
const res = await fetch(`${API}/customers?${new URLSearchParams({ q: q ?? '', page: String(page) })}`)
|
|
115
|
+
const { data, total } = await res.json()
|
|
116
|
+
|
|
117
|
+
return { rows: data, total }
|
|
118
|
+
},
|
|
119
|
+
find: (id) => fetch(`${API}/customers/${id}`).then((r) => (r.ok ? r.json() : null)),
|
|
120
|
+
create: (values) => post(`${API}/customers`, values),
|
|
121
|
+
update: (id, values) => put(`${API}/customers/${id}`, values),
|
|
122
|
+
delete: (id) => del(`${API}/customers/${id}`),
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
A save says "Product saved." on the list it lands on, and a delete says
|
|
127
|
+
"Product deleted." in place, as a toast. Both use
|
|
128
|
+
[`flash()`](/guides/server-actions#a-message-for-the-next-page).
|
|
129
|
+
|
|
130
|
+
`find` answering `null` is a 404, for the page and for a save or delete of that
|
|
131
|
+
id. `memorySource(rows, key, make)` is one for tests and demos.
|
|
132
|
+
|
|
133
|
+
### A Laravel or Go backend
|
|
134
|
+
|
|
135
|
+
On a backend the five functions are `rpc()` calls. The source runs in the
|
|
136
|
+
renderer, in a page and in the panel's save, and both forward the visitor's
|
|
137
|
+
cookie, so `auth()->user()` is them:
|
|
138
|
+
|
|
139
|
+
```ts title="src/admin/resources/products.ts"
|
|
140
|
+
const productsSource: DataSource<Product, Values> = {
|
|
141
|
+
list: (query) => rpc('Products.list', query),
|
|
142
|
+
find: (id) => rpc('Products.find', Number(id)),
|
|
143
|
+
create: (values) => rpc('Products.create', values),
|
|
144
|
+
update: (id, values) => rpc('Products.update', Number(id), values),
|
|
145
|
+
delete: (id) => rpc('Products.delete', Number(id)),
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```php title="app/Rsc/Products.php"
|
|
150
|
+
public function create(StoreProduct $request): Product
|
|
151
|
+
{
|
|
152
|
+
return Product::create($request->validated());
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The backend validates again, and its answer is the one that counts. A rule the
|
|
157
|
+
schema cannot know - `unique:products,sku`, a stock check - comes back as a 422,
|
|
158
|
+
and its messages show under the same fields as the schema's, with the form's
|
|
159
|
+
values kept. Nothing to catch: the panel treats the backend's refusal and its
|
|
160
|
+
own the same way.
|
|
161
|
+
|
|
162
|
+
### Choices that read as words
|
|
163
|
+
|
|
164
|
+
An enum's options are shown humanized - `'in_stock'` reads "In stock" in the
|
|
165
|
+
form and the filter, and `'in_stock'` is still what is saved. Say them
|
|
166
|
+
yourself where that is not the word:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
fields: { status: { options: ['draft', { value: 'active', label: 'Live' }] } },
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
An optional select starts on "None", which saves nothing.
|
|
173
|
+
|
|
174
|
+
## Relations
|
|
175
|
+
|
|
176
|
+
A belongsTo is a field that names another resource of the panel:
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
table: (col) => [col.text('name'), col.relation('categoryId', 'categories')],
|
|
180
|
+
form: z.object({ name: z.string(), categoryId: z.number().int() }),
|
|
181
|
+
fields: { categoryId: { relation: 'categories' } },
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The form offers the categories as a select, labelled "Category": each one's
|
|
185
|
+
`key` saved, its `title` shown. The column shows "Lighting", not `3`, and a
|
|
186
|
+
client-mode table searches and sorts it by that word. A `z.number()` relation
|
|
187
|
+
arrives as a number, as a number field does.
|
|
188
|
+
|
|
189
|
+
The choices are the related resource's `list` with `perPage: null`, asked as
|
|
190
|
+
the visitor - scoped to their tenant like any list - so a relation is for a
|
|
191
|
+
resource with a select's worth of rows: categories, teams, plans. A save checks
|
|
192
|
+
the id it was sent with that resource's `find`, as the visitor, so an id posted
|
|
193
|
+
by hand from another tenant is refused under the field. A relation to a
|
|
194
|
+
resource the panel does not have fails when the panel is defined.
|
|
195
|
+
|
|
196
|
+
## Bulk actions
|
|
197
|
+
|
|
198
|
+
Every table that is not `readOnly` can tick rows; ticking shows what can be
|
|
199
|
+
done to them below the table, with the panel's own **Delete**. Add your own:
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
bulkActions: [
|
|
203
|
+
{ name: 'publish', label: 'Publish', run: (rows) => rpc('Products.publish', rows.map((row) => row.id)) },
|
|
204
|
+
{ name: 'archive', label: 'Archive', destructive: true, run: archiveAll },
|
|
205
|
+
],
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
`run` is given the rows themselves, each found by the source's `find` as the
|
|
209
|
+
visitor: an id the browser sent that is not theirs, or is already gone, never
|
|
210
|
+
reaches it. It runs after `authorize`, like every save. A `destructive` action
|
|
211
|
+
asks first, as Delete does. When it is done the table refreshes and a toast
|
|
212
|
+
says "Publish: 3 products." - or whatever `run` returns as a string.
|
|
213
|
+
|
|
214
|
+
## Server or client tables
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
mode: 'server' // the default
|
|
218
|
+
mode: 'client'
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
**Server** - each search, filter, sort and page is a request, and `list` is
|
|
222
|
+
asked for one page (`perPage` a number). The url holds the state, so a table is
|
|
223
|
+
a link, the back button works and it works with JavaScript off. For data that
|
|
224
|
+
grows.
|
|
225
|
+
|
|
226
|
+
**Client** - `list` is asked once with `perPage: null` and returns every row;
|
|
227
|
+
search, filters, sorting and paging happen in the browser with no request. For
|
|
228
|
+
data that stays small - hundreds of rows, not thousands.
|
|
229
|
+
|
|
230
|
+
## Who may use it
|
|
231
|
+
|
|
232
|
+
The panel has no login of its own. It asks the app's:
|
|
233
|
+
|
|
234
|
+
```ts title="src/admin/panel.ts"
|
|
235
|
+
import { definePanel } from '@rsc-kit/admin'
|
|
236
|
+
|
|
237
|
+
export const panel = definePanel({
|
|
238
|
+
path: '/admin',
|
|
239
|
+
resources: [products, customers],
|
|
240
|
+
authorize: async () => (await currentUser())?.isAdmin === true,
|
|
241
|
+
user: currentUser,
|
|
242
|
+
signOut: () => endSession(),
|
|
243
|
+
})
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
`authorize` runs on every page, and on every save, delete and team switch -
|
|
247
|
+
an action is reachable without the page that rendered it, so a guard on the
|
|
248
|
+
pages alone would not be one. A refusal is a 404. `user` fills the sidebar's
|
|
249
|
+
footer, and `signOut` runs in a server action and lands the visitor on `/`.
|
|
250
|
+
|
|
251
|
+
## Tenants
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
tenancy: {
|
|
255
|
+
label: 'Teams',
|
|
256
|
+
tenants: () => teamsOf(currentUser()),
|
|
257
|
+
current: () => teamFromSession(),
|
|
258
|
+
switch: (id) => rememberTeam(id),
|
|
259
|
+
},
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
The sidebar gets a team switcher, and every data source call is told the
|
|
263
|
+
tenant as its last argument - `list(query, { tenant })`. A current tenant that
|
|
264
|
+
is not one of the visitor's own - a cookie naming someone else's team - is
|
|
265
|
+
refused, so `current` reads the session and the panel checks membership.
|
|
266
|
+
`switch` runs in a server action, so it may set a cookie.
|
|
267
|
+
|
|
268
|
+
`perTenant((tenantId) => source)` keeps a source per tenant, for data that is
|
|
269
|
+
already split that way.
|
|
270
|
+
|
|
271
|
+
## Mounting it
|
|
272
|
+
|
|
273
|
+
The engine finds routes, url schemas and sections by their files, so the
|
|
274
|
+
panel is mounted with seven files of one or two lines each:
|
|
275
|
+
|
|
276
|
+
```
|
|
277
|
+
src/app/admin/middleware.ts export default panel.middleware
|
|
278
|
+
src/app/admin/layout.tsx export default panel.Layout
|
|
279
|
+
src/app/admin/page.tsx export default panel.Dashboard
|
|
280
|
+
src/app/admin/[resource]/page.tsx export const params = resourceParams
|
|
281
|
+
export const searchParams = listSearch
|
|
282
|
+
export default panel.Index
|
|
283
|
+
src/app/admin/[resource]/admin-table.section.tsx export default panel.Table
|
|
284
|
+
src/app/admin/[resource]/new/page.tsx export const params = resourceParams
|
|
285
|
+
export default panel.Create
|
|
286
|
+
src/app/admin/[resource]/[id]/page.tsx export const params = recordParams
|
|
287
|
+
export default panel.Edit
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
The section must be named `admin-table.section.tsx`: a delete refreshes the
|
|
291
|
+
table by that name.
|
|
292
|
+
|
|
293
|
+
## Plugins
|
|
294
|
+
|
|
295
|
+
A plugin adds resources, dashboard widgets and a reaction to every save and
|
|
296
|
+
delete, and is installed with `plugins: [auditLog()]`. Writing one to publish
|
|
297
|
+
or sell is [its own guide](/guides/admin-plugins/).
|
package/guides/index.json
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"slug": "admin",
|
|
4
|
+
"title": "Admin panels",
|
|
5
|
+
"description": "A shadcn 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
|
+
},
|
|
7
|
+
{
|
|
8
|
+
"slug": "admin-plugins",
|
|
9
|
+
"title": "Admin plugins",
|
|
10
|
+
"description": "Writing an @rsc-kit/admin plugin, publishing it as a package, and selling it."
|
|
11
|
+
},
|
|
2
12
|
{
|
|
3
13
|
"slug": "api-routes",
|
|
4
14
|
"title": "API routes",
|
package/guides/redirects.md
CHANGED
|
@@ -20,6 +20,12 @@ export default async function ProductPage({ params }: { params: Promise<{ slug:
|
|
|
20
20
|
default status is `307`, because it preserves the method: a redirect out of a
|
|
21
21
|
`POST` does not silently become a `GET` of the target.
|
|
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.
|
|
28
|
+
|
|
23
29
|
A redirect during a navigation stays a navigation. The document does not reload,
|
|
24
30
|
the layouts you are inside stay mounted, and only the part below them changes.
|
|
25
31
|
|
package/guides/server-actions.md
CHANGED
|
@@ -537,6 +537,53 @@ one can be wired in without this package having an opinion about which. See
|
|
|
537
537
|
[Writing to the response](/guides/authorization#writing-to-the-response) for
|
|
538
538
|
where else it works, and where it does not.
|
|
539
539
|
|
|
540
|
+
## A message for the next page
|
|
541
|
+
|
|
542
|
+
An action's return value goes back to the form that submitted it. When the
|
|
543
|
+
action redirects, that form is gone, so "Saved." on the list it lands on is a
|
|
544
|
+
one-time message instead:
|
|
545
|
+
|
|
546
|
+
```ts
|
|
547
|
+
'use server';
|
|
548
|
+
|
|
549
|
+
import { flash } from '@rsc-kit/core/flash';
|
|
550
|
+
import { redirect } from '@rsc-kit/core/redirect';
|
|
551
|
+
|
|
552
|
+
export async function saveProduct(formData: FormData) {
|
|
553
|
+
await products.save(formData);
|
|
554
|
+
await flash('Product saved.');
|
|
555
|
+
|
|
556
|
+
redirect('/products');
|
|
557
|
+
}
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
```tsx title="src/app/toasts.tsx"
|
|
561
|
+
'use client';
|
|
562
|
+
|
|
563
|
+
import { useFlash } from '@rsc-kit/core/useFlash';
|
|
564
|
+
import { toast } from 'sonner';
|
|
565
|
+
|
|
566
|
+
export function Toasts() {
|
|
567
|
+
useFlash(({ message, kind }) => (kind === 'error' ? toast.error : toast.success)(message));
|
|
568
|
+
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
Render `<Toasts />` once, in the root layout. `kind` is `'success'` (the
|
|
574
|
+
default), `'error'`, `'info'` or `'warning'`; two calls are two messages.
|
|
575
|
+
|
|
576
|
+
`flash()` sets a cookie, so call it where a cookie can still be set: an action,
|
|
577
|
+
middleware or a `route.ts`, not a page. The browser takes it, not the server:
|
|
578
|
+
`useFlash` looks when it mounts, after every navigation and, where the browser
|
|
579
|
+
supports CookieStore, whenever a response sets it, so an action that stays on
|
|
580
|
+
the page shows its message too. Each message is handed over once, then cleared.
|
|
581
|
+
A page never reads it, so a stored or edge-cached page stays cacheable. Unread,
|
|
582
|
+
it lapses after a minute.
|
|
583
|
+
|
|
584
|
+
The cookie is readable by script, so it holds a message and never data. Render
|
|
585
|
+
the message as text.
|
|
586
|
+
|
|
540
587
|
## Work the visitor should not wait for
|
|
541
588
|
|
|
542
589
|
An audit row, a welcome email, a cache warm: the action's answer should not
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsc-kit/mcp",
|
|
3
|
-
"version": "0.30.
|
|
3
|
+
"version": "0.30.39",
|
|
4
4
|
"description": "An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
37
|
-
"create-rsc-kit": "^0.30.
|
|
37
|
+
"create-rsc-kit": "^0.30.39",
|
|
38
38
|
"zod": "^3.25"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|