@rsc-kit/mcp 0.30.38 → 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 +343 -1
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +109 -0
- package/guides/admin.md +401 -0
- 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 +21 -1
- 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/server-actions.md +47 -0
- package/guides/your-own-backend.md +17 -0
- package/package.json +2 -2
package/dist/recipes.js
CHANGED
|
@@ -216,7 +216,29 @@ the same selector showing a #results-skeleton.
|
|
|
216
216
|
Do NOT patch around a skeleton on an in-page search with your own useState,
|
|
217
217
|
router.replace or a Suspense key: the form already does this. It is the form
|
|
218
218
|
only - a <Link> or visit() to the same path with another query is a page of its
|
|
219
|
-
own. A search to ANOTHER page still shows that page's shell first
|
|
219
|
+
own. A search to ANOTHER page still shows that page's shell first.
|
|
220
|
+
|
|
221
|
+
A REDIRECT FROM AN ACTION keeps the layouts as they were, like a link: right
|
|
222
|
+
for save-then-list. If the action changed what a LAYOUT reads (team switch,
|
|
223
|
+
sign-in, a count in the sidebar), call revalidate('all') BEFORE redirect() -
|
|
224
|
+
the destination then comes back whole, layouts included. A named section counts
|
|
225
|
+
too. Backend: Rsc::revalidate('all') before the redirect. Without it the page is
|
|
226
|
+
new and the sidebar is stale until a reload.
|
|
227
|
+
|
|
228
|
+
A MESSAGE FOR THE NEXT PAGE ("Saved." after a save that redirects): the return
|
|
229
|
+
value goes to the form, which the redirect replaced. Call
|
|
230
|
+
await flash('Product saved.') from @rsc-kit/core/flash in the action before
|
|
231
|
+
redirect(), and render ONE client component in the root layout:
|
|
232
|
+
useFlash(({ message, kind }) => toast(message)) from @rsc-kit/core/useFlash.
|
|
233
|
+
kind: 'success' (default) | 'error' | 'info' | 'warning'. flash() sets a cookie:
|
|
234
|
+
an action, middleware or route.ts, NEVER a page. The browser takes it after the
|
|
235
|
+
navigation (or when the cookie changes) and clears it, so a stored page stays
|
|
236
|
+
cacheable. Do NOT read it in a server component, and render it as text.
|
|
237
|
+
From the backend, the same message: Laravel Rsc::flash('Invoice sent.') or
|
|
238
|
+
Rsc::flash('Declined.', 'error'); Go rsckit.Flash(ctx, rsckit.FlashSuccess,
|
|
239
|
+
"Invoice sent."). In an action called by rpc() it rides the host call onto the
|
|
240
|
+
page, as a login cookie does; NOT from a batched read. The same useFlash()
|
|
241
|
+
shows it. Nothing secret: the browser reads it, unencrypted.`,
|
|
220
242
|
},
|
|
221
243
|
{
|
|
222
244
|
topic: 'prefetch',
|
|
@@ -653,6 +675,19 @@ for a component under its own <Suspense> and for a parallel route slot. Only
|
|
|
653
675
|
an authorization check should NOT be there: the layouts above already
|
|
654
676
|
rendered. See the redirects guide.
|
|
655
677
|
|
|
678
|
+
TO LET A CDN KEEP A PAGE RENDERED PER REQUEST: export const revalidate = 60
|
|
679
|
+
(seconds, a number literal; a layout may export it too and the smallest wins,
|
|
680
|
+
0 anywhere turns it off). The page is rendered whole, then sent Cache-Control:
|
|
681
|
+
public, max-age=0, s-maxage=60, stale-while-revalidate=86400 with an ETag
|
|
682
|
+
only if the render read nothing of the visitor; a 404 is kept a minute at
|
|
683
|
+
most. A revalidate page calls its backend as nobody: no cookie, no
|
|
684
|
+
Authorization, and a cookie the backend sets is dropped - do NOT export it on
|
|
685
|
+
a page that shows who is signed in. Otherwise the page is sent private,
|
|
686
|
+
no-store with Cache-Status: rsc-kit; fwd=bypass; detail=<reason> (cookies,
|
|
687
|
+
headers, request, connection, host-middleware, set-cookie, nonce, status,
|
|
688
|
+
failed-render, payload-url) and a log line naming what read the visitor. It is
|
|
689
|
+
not prerendered.
|
|
690
|
+
|
|
656
691
|
FOR AN EDGE THAT CACHES PAGES: rsc-kit implements the edge contract (v2,
|
|
657
692
|
docs: reference/edge-contract). Every server build writes .output/edge.json -
|
|
658
693
|
{ v: 2, build, shells, static, bypass } - whose shells are url patterns ("/",
|
|
@@ -900,6 +935,12 @@ apiUrl refuses a page, because fetching one gets html. It checks the PATH, not
|
|
|
900
935
|
the response type - for types across the boundary use a server action or a
|
|
901
936
|
query, where the return type is the function's because it is the same function.
|
|
902
937
|
|
|
938
|
+
A POST, PUT, PATCH or DELETE a browser sends from another site is answered 403
|
|
939
|
+
before the handler runs (Sec-Fetch-Site, else Origin against the host - Go's
|
|
940
|
+
http.CrossOriginProtection rule; actions and forms too). No CSRF token: do NOT
|
|
941
|
+
add one. A webhook or server sends neither header and is never refused. Another
|
|
942
|
+
origin that should post: rscKit({ trustedOrigins: ['https://admin.example.com'] }).
|
|
943
|
+
|
|
903
944
|
They run their directory's \`middleware.ts\`, so an endpoint under a guarded
|
|
904
945
|
path is guarded.
|
|
905
946
|
|
|
@@ -1271,6 +1312,10 @@ IMPORTS
|
|
|
1271
1312
|
(optimistic: option + React's useOptimistic)
|
|
1272
1313
|
cache from 'react' -> cache from @rsc-kit/core/cache: React's dedupes only inside a render; this one
|
|
1273
1314
|
spans the request (guards, actions, api routes). The build names files still on React's
|
|
1315
|
+
unstable_cache, revalidateTag -> cache(fn, { key, revalidate, tags }) from @rsc-kit/core/cache keeps answers ACROSS
|
|
1316
|
+
requests (memory, or installCacheStore({ get, set }) for Redis/KV); changed(tag)
|
|
1317
|
+
from @rsc-kit/core/changed makes the next ask miss - no purge. Runs as nobody:
|
|
1318
|
+
cookies()/headers() throw inside it; arguments and answers are JSON
|
|
1274
1319
|
@react-email/render, renderToString in an action -> the same call, in a module that starts with "use ssr"
|
|
1275
1320
|
(the build warns naming the app file and the package; the stub throws when called)
|
|
1276
1321
|
(how_to emails). Next gets away with it only for externalised packages; here it is explicit
|
|
@@ -2639,6 +2684,303 @@ That limit is narrower than Next's: the action's logic is a unit test here.
|
|
|
2639
2684
|
Do NOT start a dev server, spawn a process or pick a port in a test. Do NOT
|
|
2640
2685
|
add a second runner. The one in tests/ goes through the real build already.`,
|
|
2641
2686
|
},
|
|
2687
|
+
{
|
|
2688
|
+
topic: 'admin',
|
|
2689
|
+
summary: 'An admin panel with @rsc-kit/press: resources over any data source, server or client tables, Standard Schema forms, auth, tenants, plugins, themes',
|
|
2690
|
+
body: `@rsc-kit/press builds an admin panel from resources; a theme draws it
|
|
2691
|
+
(@rsc-kit/press/shadcn: sidebar, data table, dialogs). The app keeps what is
|
|
2692
|
+
its own: where the data lives, who may see it, whose it is. Full guides:
|
|
2693
|
+
admin, admin-plugins. There is no @rsc-kit/admin any more - do not import it.
|
|
2694
|
+
@rsc-kit/press defineResource, memorySource, perTenant, definePlugin, types
|
|
2695
|
+
@rsc-kit/press/admin definePanel, resourceParams, recordParams, listSearch, pluginsSearch, AdminTheme
|
|
2696
|
+
@rsc-kit/press/forms defineForm - a public form from a schema (how_to public-forms)
|
|
2697
|
+
@rsc-kit/press/pages definePages - pages an editor builds from blocks (how_to pages)
|
|
2698
|
+
@rsc-kit/press/shadcn shadcn (the theme; it draws panels and public forms)
|
|
2699
|
+
|
|
2700
|
+
SETUP: the app needs Tailwind v4 + shadcn (create-rsc-kit --tailwind --shadcn).
|
|
2701
|
+
In the app's stylesheet, after tailwind and shadcn:
|
|
2702
|
+
@import "@rsc-kit/press/shadcn.css";
|
|
2703
|
+
Without it the panel renders unstyled - the line is Tailwind's @source for the
|
|
2704
|
+
theme's components.
|
|
2705
|
+
|
|
2706
|
+
A RESOURCE:
|
|
2707
|
+
\`\`\`ts
|
|
2708
|
+
import * as z from 'zod'
|
|
2709
|
+
import { defineResource } from '@rsc-kit/press'
|
|
2710
|
+
|
|
2711
|
+
export const products = defineResource<Product>()({
|
|
2712
|
+
name: 'products', key: 'id', title: 'name',
|
|
2713
|
+
icon: Package, // lucide-react, optional
|
|
2714
|
+
source: productsSource, // a DataSource, below
|
|
2715
|
+
mode: 'server', // or 'client'; server is the default
|
|
2716
|
+
table: (col) => [
|
|
2717
|
+
col.text('name').searchable().sortable(),
|
|
2718
|
+
col.money('price').sortable(),
|
|
2719
|
+
col.badge('status'),
|
|
2720
|
+
col.boolean('featured').hidden(), // off in the Columns menu at first
|
|
2721
|
+
col.date('createdAt'),
|
|
2722
|
+
// col.text('image').cell((value, row) => <Avatar .../>) - a server component
|
|
2723
|
+
],
|
|
2724
|
+
filters: ['status'],
|
|
2725
|
+
form: z.object({ name: z.string().min(2), price: z.number().positive(), status: z.enum(['draft', 'active']), featured: z.boolean() }),
|
|
2726
|
+
fields: { description: { type: 'textarea', description: 'Help text' } },
|
|
2727
|
+
perPage: 10,
|
|
2728
|
+
})
|
|
2729
|
+
\`\`\`
|
|
2730
|
+
|
|
2731
|
+
THE FORM IS ANY STANDARD SCHEMA - zod, valibot, arktype, Effect Schema. Use
|
|
2732
|
+
the one the app already has; do NOT add zod for the panel. The schema checks
|
|
2733
|
+
every save on the server, errors under each field. Fields come from the
|
|
2734
|
+
schema's Standard JSON Schema (zod 4.2+ has it). A schema without it MUST list
|
|
2735
|
+
every field in fields: { title: { type: 'text' } } - the panel throws saying
|
|
2736
|
+
so. A number field arrives as a number: write z.number(), NOT z.coerce.number().
|
|
2737
|
+
A switch that is off is false; an empty optional field is absent.
|
|
2738
|
+
BROWSER CHECK: the same schema's constraints become attributes (required,
|
|
2739
|
+
min/max, minlength/maxlength, an anchored pattern, type=email, step=1 for an
|
|
2740
|
+
integer); a wrong field is caught before submit and the browser's message
|
|
2741
|
+
shows in the field's FieldError. The schema never ships to the browser, so it
|
|
2742
|
+
works the same when the rules live in Laravel/Go. A courtesy only: custom
|
|
2743
|
+
messages and uniqueness come from the server. Do NOT add a client-side
|
|
2744
|
+
schema run.
|
|
2745
|
+
THEME: the header menu is Light/Dark/System (System default, follows the OS);
|
|
2746
|
+
it sets class="dark" on <html> before paint, so the root layout's <html> gets
|
|
2747
|
+
suppressHydrationWarning. modeToggle: false hides it.
|
|
2748
|
+
|
|
2749
|
+
A DATA SOURCE is five functions, and the panel calls nothing else - a
|
|
2750
|
+
database, ORM, REST API or rpc() into Laravel/Go alike:
|
|
2751
|
+
\`\`\`ts
|
|
2752
|
+
import type { DataSource } from '@rsc-kit/press'
|
|
2753
|
+
|
|
2754
|
+
const source: DataSource<Customer, Values> = {
|
|
2755
|
+
async list({ q, sort, dir, page, perPage, filters, searchable }, { tenant }) { return { rows, total } },
|
|
2756
|
+
find: (id, { tenant }) => ..., // null -> 404, for the page AND a save/delete of that id
|
|
2757
|
+
create: (values, { tenant }) => ...,
|
|
2758
|
+
update: (id, values, { tenant }) => ...,
|
|
2759
|
+
delete: (id, { tenant }) => ...,
|
|
2760
|
+
}
|
|
2761
|
+
\`\`\`
|
|
2762
|
+
The last argument of every call is the context { tenant } (null without
|
|
2763
|
+
tenancy). On a Laravel/Go backend the functions call rpc() and the backend
|
|
2764
|
+
validates again - the panel's check is the renderer's, the backend's counts.
|
|
2765
|
+
A BACKEND 422 (unique:products,sku, a stock check) shows under the same fields
|
|
2766
|
+
as the schema's, values kept: do NOT catch it in create/update - let the rpc()
|
|
2767
|
+
call throw, and the panel's save answers it as field errors.
|
|
2768
|
+
memorySource(rows, key, make) is for tests and demos; perTenant((tenantId) =>
|
|
2769
|
+
source) keeps one source per tenant.
|
|
2770
|
+
|
|
2771
|
+
OPTIONS: an enum's values are shown humanized ('in_stock' reads "In stock")
|
|
2772
|
+
and saved as they are; fields: { status: { options: ['draft', { value:
|
|
2773
|
+
'active', label: 'Live' }] } } labels them yourself. An optional select starts
|
|
2774
|
+
on "None", which saves nothing.
|
|
2775
|
+
|
|
2776
|
+
RELATIONS (belongsTo): fields: { categoryId: { relation: 'categories' } } is a
|
|
2777
|
+
select of the categories resource's rows - its key saved, its title shown -
|
|
2778
|
+
and col.relation('categoryId', 'categories') shows the title, not the id.
|
|
2779
|
+
The choices are that resource's list() with perPage: null AS THE VISITOR
|
|
2780
|
+
(tenant-scoped), so relate to a resource with a select's worth of rows. The
|
|
2781
|
+
save checks the posted id with the related source's find() as the visitor: an
|
|
2782
|
+
id from another tenant is refused under the field ("Choose one of the listed
|
|
2783
|
+
categories."). A z.number() relation arrives as a number. A relation to a
|
|
2784
|
+
resource the panel does not have throws when the panel is defined.
|
|
2785
|
+
|
|
2786
|
+
BULK ACTIONS: a resource that is not readOnly ticks rows and offers the
|
|
2787
|
+
panel's own Delete below the table. bulkActions: [{ name: 'publish', label:
|
|
2788
|
+
'Publish', destructive?: true, run: (rows, context) => ... }] adds more. run
|
|
2789
|
+
gets ROWS, each found with the source's find() as the visitor - an id that is
|
|
2790
|
+
not theirs never reaches it - after authorize. destructive asks first. The
|
|
2791
|
+
toast says "3 products published." with done: 'published' (else "Publish: 3
|
|
2792
|
+
products."), or run's own string. 'delete' is
|
|
2793
|
+
reserved, and a readOnly resource with bulkActions throws.
|
|
2794
|
+
|
|
2795
|
+
TABLE MODE: 'server' - each search/filter/sort/page is a request, list() gets
|
|
2796
|
+
one page (perPage a number), state is in the url (links, back button, works
|
|
2797
|
+
without JS); for data that grows. 'client' - list() is called once with
|
|
2798
|
+
perPage: null and returns EVERY row; search, filters, sort and paging run in
|
|
2799
|
+
the browser; for small data (hundreds of rows). A source must handle
|
|
2800
|
+
perPage: null if any resource using it is client mode.
|
|
2801
|
+
|
|
2802
|
+
THE PANEL:
|
|
2803
|
+
\`\`\`ts
|
|
2804
|
+
import { definePanel } from '@rsc-kit/press/admin'
|
|
2805
|
+
import { shadcn } from '@rsc-kit/press/shadcn'
|
|
2806
|
+
|
|
2807
|
+
export const panel = definePanel({
|
|
2808
|
+
path: '/admin',
|
|
2809
|
+
theme: shadcn, // required; { ...shadcn, Dashboard: Mine } replaces one page
|
|
2810
|
+
resources: [products, customers],
|
|
2811
|
+
authorize: async () => (await currentUser())?.isAdmin === true,
|
|
2812
|
+
user: currentUser, // { name, email?, avatar? } for the sidebar footer
|
|
2813
|
+
signOut: () => endSession(), // runs in a server action, lands on /
|
|
2814
|
+
tenancy: { label: 'Teams', tenants: () => teamsOfUser(), current: () => teamFromCookie(), switch: (id) => setTeamCookie(id) },
|
|
2815
|
+
plugins: [auditLog()],
|
|
2816
|
+
})
|
|
2817
|
+
\`\`\`
|
|
2818
|
+
AUTH: the panel has no login of its own; authorize asks the app's. It runs on
|
|
2819
|
+
every page AND every save, delete and tenant switch (actions are public
|
|
2820
|
+
endpoints). A refusal is a 404. Do NOT guard only the pages.
|
|
2821
|
+
THEME: an AdminTheme is an object of components - Shell, Brand,
|
|
2822
|
+
TenantSwitcher, Nav, Breadcrumbs, UserMenu, Dashboard, Index, Table, Form,
|
|
2823
|
+
Plugins, Cell, Loading. Each gets data, bound server actions and drawn
|
|
2824
|
+
elements as props, never a component, so any may be a client component. The
|
|
2825
|
+
panel owns the guard, the data and the Suspense boundaries; a theme only draws.
|
|
2826
|
+
TENANTS: current() is read from the session; the panel refuses a current
|
|
2827
|
+
tenant that is not in tenants() (a forged cookie). switch() runs in a server
|
|
2828
|
+
action, so cookies().set works there.
|
|
2829
|
+
|
|
2830
|
+
MOUNTING - seven route files, because the engine finds routes, url schemas and
|
|
2831
|
+
sections by their files. Each re-exports from the panel:
|
|
2832
|
+
src/app/admin/middleware.ts export default panel.middleware
|
|
2833
|
+
src/app/admin/layout.tsx export default panel.Layout
|
|
2834
|
+
src/app/admin/page.tsx export default panel.Dashboard
|
|
2835
|
+
src/app/admin/[resource]/page.tsx export const params = resourceParams
|
|
2836
|
+
export const searchParams = listSearch
|
|
2837
|
+
export default panel.Index
|
|
2838
|
+
src/app/admin/[resource]/admin-table.section.tsx export default panel.Table
|
|
2839
|
+
src/app/admin/[resource]/new/page.tsx export const params = resourceParams
|
|
2840
|
+
export default panel.Create
|
|
2841
|
+
src/app/admin/[resource]/[id]/page.tsx export const params = recordParams
|
|
2842
|
+
export default panel.Edit
|
|
2843
|
+
resourceParams, recordParams and listSearch come from '@rsc-kit/press/admin'. The
|
|
2844
|
+
section MUST be named admin-table.section.tsx: a delete refreshes it by name.
|
|
2845
|
+
|
|
2846
|
+
PLUGINS: definePlugin({ name, resources?, widgets?, onChange? }). A widget is
|
|
2847
|
+
{ title, description?, component } - a server component given { tenant }.
|
|
2848
|
+
onChange(change) runs after a save/delete is written, with { panel, resource,
|
|
2849
|
+
id, change, values?, tenant }; a plugin that throws is logged and never fails
|
|
2850
|
+
the save. Export a function of options returning the plugin. Publish with
|
|
2851
|
+
@rsc-kit/press and react as PEER dependencies and the npm keyword
|
|
2852
|
+
rsc-kit-admin-plugin.`,
|
|
2853
|
+
},
|
|
2854
|
+
{
|
|
2855
|
+
topic: 'public-forms',
|
|
2856
|
+
summary: 'A public form (contact, sign-up, survey) from a Standard Schema with @rsc-kit/press/forms: browser and server checks, a bot trap, any backend',
|
|
2857
|
+
body: `defineForm turns a Standard Schema into a form anyone may fill in - the
|
|
2858
|
+
admin panel's form without the panel, drawn by the same theme. Full guide:
|
|
2859
|
+
public-forms.
|
|
2860
|
+
|
|
2861
|
+
\`\`\`ts title="src/forms/contact.ts"
|
|
2862
|
+
import * as z from 'zod'
|
|
2863
|
+
import { defineForm } from '@rsc-kit/press/forms'
|
|
2864
|
+
import { shadcn } from '@rsc-kit/press/shadcn'
|
|
2865
|
+
|
|
2866
|
+
export const contact = defineForm({
|
|
2867
|
+
name: 'contact', // unique; lowercase letters, digits, dashes
|
|
2868
|
+
form: z.object({ name: z.string().min(2), email: z.email(), message: z.string().min(10) }),
|
|
2869
|
+
fields: { message: { type: 'textarea' } }, // what the schema cannot say
|
|
2870
|
+
theme: shadcn,
|
|
2871
|
+
submit: 'Send message',
|
|
2872
|
+
done: 'Thanks - we will be in touch.', // shown in place of the form
|
|
2873
|
+
// redirect: '/thanks', // OR this; never both
|
|
2874
|
+
handle: async (values) => { await db.insert(messages).values(values) },
|
|
2875
|
+
})
|
|
2876
|
+
\`\`\`
|
|
2877
|
+
|
|
2878
|
+
\`\`\`tsx title="src/app/contact/page.tsx"
|
|
2879
|
+
import { contact } from '@/forms/contact'
|
|
2880
|
+
export default function ContactPage() {
|
|
2881
|
+
return <main><h1>Contact us</h1><contact.Form /></main>
|
|
2882
|
+
}
|
|
2883
|
+
\`\`\`
|
|
2884
|
+
|
|
2885
|
+
The form reads nothing from the request: the page stays static and is stored
|
|
2886
|
+
by the build. Do NOT add connection() or a Suspense boundary for it.
|
|
2887
|
+
|
|
2888
|
+
CHECKS: the schema's constraints become the browser's (required, minlength,
|
|
2889
|
+
type="email", pattern), with the message under the field. The schema checks
|
|
2890
|
+
every submission on the server again, and handle() only gets values that
|
|
2891
|
+
passed. A refusal handle() decides goes under its field:
|
|
2892
|
+
import { fieldErrors } from '@rsc-kit/core/action'
|
|
2893
|
+
if (await subscribed(email)) fieldErrors({ email: 'Already on the list.' })
|
|
2894
|
+
|
|
2895
|
+
A BACKEND: handle: (values) => rpc('Contact.send', values). A 422 from Laravel
|
|
2896
|
+
or Go lands under the same fields, uncaught.
|
|
2897
|
+
|
|
2898
|
+
THE BOT TRAP: every form carries a hidden field named homepage. Filled in, the
|
|
2899
|
+
submission is answered as a success and dropped - handle() never runs. So a
|
|
2900
|
+
form cannot have a field named homepage. It is not a CAPTCHA; rate-limit a form
|
|
2901
|
+
worth attacking.
|
|
2902
|
+
|
|
2903
|
+
A THEME draws it with one component, PublicForm (type FormsTheme from
|
|
2904
|
+
@rsc-kit/press/forms); replace it with theme: { ...shadcn, PublicForm: Mine }.
|
|
2905
|
+
SETUP is the panel's: bun add @rsc-kit/press, and
|
|
2906
|
+
@import "@rsc-kit/press/shadcn.css" in the stylesheet.`,
|
|
2907
|
+
},
|
|
2908
|
+
{
|
|
2909
|
+
topic: 'pages',
|
|
2910
|
+
summary: 'Landing pages built in the admin panel from the app\'s blocks with @rsc-kit/press/pages: defineBlock, definePages, one catch-all route, real 404s',
|
|
2911
|
+
body: `An editor builds pages in the admin panel from blocks the app defines; one
|
|
2912
|
+
catch-all route serves them, read from the source on every request - a save is
|
|
2913
|
+
live on the next visit, nothing is rebuilt. Full guide: pages.
|
|
2914
|
+
|
|
2915
|
+
A BLOCK is a schema for what the editor fills in, and a server component:
|
|
2916
|
+
\`\`\`tsx title="src/site/blocks.tsx"
|
|
2917
|
+
import * as z from 'zod'
|
|
2918
|
+
import { defineBlock } from '@rsc-kit/press'
|
|
2919
|
+
|
|
2920
|
+
export const hero = defineBlock({
|
|
2921
|
+
name: 'hero', // lowercase, digits, dashes
|
|
2922
|
+
form: z.object({ heading: z.string().min(3), lead: z.string().optional() }),
|
|
2923
|
+
fields: { lead: { type: 'textarea' } },
|
|
2924
|
+
render: ({ data }) => <section><h1>{data.heading}</h1>{data.lead && <p>{data.lead}</p>}</section>,
|
|
2925
|
+
})
|
|
2926
|
+
// A block may place a public form: render: () => <contact.Form />
|
|
2927
|
+
\`\`\`
|
|
2928
|
+
|
|
2929
|
+
THE PAGES and the three files that serve them:
|
|
2930
|
+
\`\`\`ts title="src/site/pages.ts"
|
|
2931
|
+
import { definePages } from '@rsc-kit/press/pages'
|
|
2932
|
+
export const site = definePages({ blocks: [hero, features, text], source: pagesSource })
|
|
2933
|
+
\`\`\`
|
|
2934
|
+
\`\`\`tsx title="src/app/[...path]/page.tsx"
|
|
2935
|
+
import { site } from '@/site/pages'
|
|
2936
|
+
export const generateMetadata = site.generateMetadata
|
|
2937
|
+
export default function SitePage({ params }: { params: Promise<{ path: string }> }) {
|
|
2938
|
+
return <main><site.Page params={params} /></main>
|
|
2939
|
+
}
|
|
2940
|
+
\`\`\`
|
|
2941
|
+
\`\`\`ts title="src/app/[...path]/middleware.ts"
|
|
2942
|
+
import { site } from '@/site/pages'
|
|
2943
|
+
export default site.middleware // REQUIRED for real 404s
|
|
2944
|
+
\`\`\`
|
|
2945
|
+
And resources: [site.resource, ...] in definePanel.
|
|
2946
|
+
|
|
2947
|
+
Without the middleware, a draft or a typo answers 200: the route's shell has
|
|
2948
|
+
gone out before the page finds nothing. site.middleware does the lookup before
|
|
2949
|
+
anything is sent.
|
|
2950
|
+
|
|
2951
|
+
A catch-all is the weakest route: every other page wins over it. The slug is
|
|
2952
|
+
the address (about, pricing/teams); the page with slug home is served at / when
|
|
2953
|
+
there is no app/page.tsx (definePages({ home }) renames it; /home itself 404s).
|
|
2954
|
+
|
|
2955
|
+
THE SOURCE: rows { id, title, slug, description?, published, blocks }, blocks a
|
|
2956
|
+
list of { type, data } (a JSON column). list() is asked for filters: { slug } to
|
|
2957
|
+
find a page and MUST honour it. memorySource does; a Laravel/Go backend filters
|
|
2958
|
+
the column. memorySource is demo-only: edits are lost on restart and not
|
|
2959
|
+
shared between replicas - real pages go in the app's database (a pages table,
|
|
2960
|
+
JSON blocks column, behind rpc()). Each block's data is checked by its own schema on save, errors under
|
|
2961
|
+
blocks.<index>.<field>; a second page at a taken slug is refused under slug.
|
|
2962
|
+
|
|
2963
|
+
A looked-up page is kept between requests with cache() (memory unless the app
|
|
2964
|
+
calls installCacheStore) for definePages({ revalidate }) seconds, 60 by default,
|
|
2965
|
+
false to ask every request. A save in the panel calls changed('press:pages'), so
|
|
2966
|
+
it is live on the next visit; a write straight to the database shows on expiry,
|
|
2967
|
+
or at once after the app calls changed('press:pages') itself.
|
|
2968
|
+
|
|
2969
|
+
BLOCKS IN ANY RESOURCE: fields: { body: { blocks: [text, quote] } } on a
|
|
2970
|
+
z.array(z.any()) property gives it the block editor; draw it with
|
|
2971
|
+
<Blocks blocks={[text, quote]} value={post.body} /> from @rsc-kit/press.
|
|
2972
|
+
A block kind the code no longer defines is left out of the page, not an error.
|
|
2973
|
+
|
|
2974
|
+
UPLOADS: fields: { photo: { type: 'image', storage, maxSize } } on a z.url()
|
|
2975
|
+
(or z.string()) property; type: 'file' with accept: '.pdf' for documents.
|
|
2976
|
+
Works in a resource, a block and a public form. storage is
|
|
2977
|
+
{ put(file, { field, tenant }) => Promise<string> } - press ships none; the
|
|
2978
|
+
string is what the record saves (a url). Write to S3/R2/disk from put; a
|
|
2979
|
+
Laravel/Go backend gets the url, not the file (rpc() carries JSON), so use the
|
|
2980
|
+
bucket its disk reads. Uploads are stored only after the rest of the form
|
|
2981
|
+
passes. Image fields refuse SVG unless accept says so. Make the property
|
|
2982
|
+
.nullable() for a Remove button that saves null. Old files are not deleted.`,
|
|
2983
|
+
},
|
|
2642
2984
|
];
|
|
2643
2985
|
/** Every topic, with one line each — what a caller reads before choosing. */
|
|
2644
2986
|
export function listTopics() {
|