A "use server" function the browser can call is a name the renderer forwards
to you. Your backend lists them in rsc-host.json, at the project root beside
vite.config.ts:
{
"actions": { "orders": { "cancel": "Orders.cancel" }, "profile": { "update": "Profile.update" } },
"functions": ["Orders.cancel", "Orders.recent", "Profile.update"]
}A client component then imports the action and calls it:
"use client";
import * as orders from '@/server-actions/orders';
export function CancelButton({ id }: { id: number }) {
return <button onClick={() => orders.cancel(id)}>Cancel</button>;
}Writing the manifest
actionsmaps a module - one per class, its path underserver-actions/- to the names it exports, each to whatever your side dispatches on.- A class’s single action, an
__invoke, is the module’sdefault. functionslists every namerpc()may call.
The build writes a module per class under server-actions/ in the source
directory: server-actions/orders.ts exporting cancel. A client component
imports * as orders from it and calls orders.cancel. The build also types
rpc()’s first argument with the functions.
Keeping it current
rscKit({ hostManifest: { command: 'bin/rails rsc:manifest' } })Have Vite run the command that writes the manifest,
rscKit({ hostManifest: { command } }), so it cannot go stale. A stale one
names a method since renamed, and nothing fails until the browser calls it.
Laravel’s rsc:host-manifest is this step; a Go registry’s WriteManifest
writes the same file. (The command above is an example for your own backend.)
Typing the functions
"types": { "Orders.recent": { "params": [{ "type": "integer" }], "result": { "type": "array", "items": { "$ref": "#/defs/Order" } } } },
"defs": { "Order": { "type": "object", "properties": { "id": { "type": "integer" } }, "required": ["id"] } }A function can also carry its types, so the app’s rpc() and the stubs are
typed. types maps a name to:
- its positional
params; - how many trailing ones are
optional; - a variadic
rest; - its
result.
Each is a JSON Schema. defs holds the named object types they refer to by
"$ref": "#/defs/Name".
A function as a form
"types": { "Contact.send": { "params": [{ "type": "object",
"properties": { "name": { "type": "string", "maxLength": 100 }, "email": { "type": "string", "format": "email" } },
"required": ["name", "email"], "x-order": ["name", "email"] }] } }A function taking one object with properties can be
a form with no schema, read from
these same types. required, format (email, date), minLength,
maxLength, minimum, maximum and enum become its fields and browser
checks.
The properties are drawn in the order written. If your language’s JSON sorts a
map, say the order with "x-order": ["name", "email"] beside them.
Checking a form before it is submitted
"validates": ["Profile.update"]<Form> asks whether a field would pass before the form is sent, and an
action whose validation runs before its body can answer without running. List
those functions in rsc-host.json. A function not listed is never sent a
check, because a backend that has never heard of one would run the call.
The check is an ordinary call with validate beside its args, never in a
batch:
{ "function": "Profile.update", "args": [{ "email": "taken@example.com" }], "validate": ["email"] }validate lists the fields to check. * matches one segment of a dotted
name, and an empty list means every field.
Run what would refuse the call - authentication, authorization, validation, limited to those fields - and stop there:
- Fields that pass answer
{ "result": null }. - Fields that do not answer the
422 with
validationErrorsa submit would. - Nothing may be written.
- Any other refusal is no verdict on the fields, and the form shows nothing for it.
Laravel does this for a function whose first parameter is a form request, with Precognition’s own rule filtering.
A message for the next page
Set-Cookie: rsc-flash=%5B%7B%22message%22%3A%22Saved.%22%2C%22kind%22%3A%22success%22%7D%5D; Path=/; Max-Age=60; SameSite=LaxuseFlash() on the page shows what an action leaves for the next one. Your
side leaves one by setting a cookie on the reply, the way a login does:
- named
rsc-flash; - its value a URI-encoded JSON list of
{ "message", "kind" }, withkindone ofsuccess,error,info,warning; - with
Path=/, a shortMax-AgeandSameSite=Lax.
The browser reads it, so it is never HttpOnly and never encrypted. A message
the visitor’s cookie still carries goes first: append to the list, do not
replace it.