Server Actions
Write a mutation as a server function, call it from the client, and revalidate the cache it affects: type-checked, CSRF-protected, and served at one endpoint.
Defining an action
Wrap a server function with action(). Give it a stable id so the client can dispatch it, and optionally declare what to revalidate on success.
// src/actions/posts.js
import { action } from 'what-framework/server';
export const createPostAction = action(
async ({ title, body }) => {
const { createPost } = await import('../db.js');
const post = createPost({ title, body });
return { ok: true, slug: post.slug };
},
{ id: 'createPost', revalidate: ['/'] }
);
The client imports this module to dispatch the action, and nothing strips the server half of it out of the client bundle: there is no 'use server' directive. Bundle this module for the browser and the action body ships with it, along with every module it imports, dynamically or not. Never put an API key, a database password, or any other secret in an action module or in anything it imports.
Serving actions
Actions are dispatched over POST /__what_action. The deploy adapters mount this for you; importing the action module (e.g. from your routes file) registers it. Action IDs belong to the JavaScript realm: one realm serves one app's actions, and an ID registered twice warns in development (ERR_DUPLICATE_ACTION_ID), the later registration being the one called. Two apps served from one process need distinct IDs. With a manual server, mount the adapter for your runtime:
import { nodeActionMiddleware, fetchActionHandler } from 'what-framework/server';
// The session token to compare the request's against. This is what the
// deploy adapters do: read the what-csrf cookie they set on every response.
// Node hands you a headers object, fetch runtimes hand you a Headers.
function getCsrfToken(req) {
const hdrs = req.headers;
const raw = typeof hdrs?.get === 'function' ? hdrs.get('cookie') : hdrs?.cookie;
const m = String(raw || '').match(/(?:^|;\s*)what-csrf=([^;]+)/);
return m ? decodeURIComponent(m[1]) : null;
}
// Node (connect / express). Reads and parses the body for you, 1 MB cap.
app.use(nodeActionMiddleware({ getCsrfToken }));
// Fetch runtimes (Workers, Deno, Bun): (Request) => Response
const handler = fetchActionHandler({ getCsrfToken });
Both wrappers build on createActionHandler, which is runtime-neutral: it takes an already-parsed { method, headers, body, query } and returns { status, headers, body }. It reads no request stream, so it is not mountable on its own. Reach for it only when you are wiring a server the two adapters do not cover, and read and parse the body yourself.
Calling from the client
The endpoint reads the action id and the CSRF token as hidden fields. Attributes on the <form> element are never submitted, so a form that carries the id as data-action="createPost" and nothing else answers 400. <Form> emits both fields for you and renders a real <form method="post">, which submits with no JavaScript at all:
// src/components/new-post-form.js
import { h } from 'what-framework';
import { Form } from 'what-framework/server';
import { createPostAction } from '../actions/posts.js';
export function NewPostForm({ csrfToken }) {
return h(Form, { action: createPostAction, csrfToken, redirect: '/' },
h('input', { name: 'title', required: true }),
h('textarea', { name: 'body', required: true }),
h('button', {}, 'Publish'),
);
}
That renders hidden _action and _redirect inputs (and what-csrf-token when the form has a token), then your children. Pass the action function, not its id string, so the two can never drift apart. Without JavaScript, the endpoint answers 303 on success and the browser follows _redirect. On a mounted page the form runs its own submit instead: it calls the action as a transition, useFormStatus() inside it reads the submission, and the form is reset once the action has succeeded: its uncontrolled fields clear, and a controlled one (a value or checked prop) keeps what its prop holds, as in React 19. action may also be a client function, such as the dispatch useActionState returns, which receives the FormData.
A form needs no token to post from a page the browser opened: the endpoint accepts a request the browser marks same-origin (Sec-Fetch-Site, or an Origin header), which every current browser sends with a form post. That is how a form on a cached page (mode: 'static' or 'hybrid'), whose shared HTML can never carry a visitor's token, submits with scripting off. The token still covers browsers that send neither header: on an uncached page, hand the loader's ctx.csrfToken to <Form csrfToken> (on the client it finds the token itself, in the what-csrf-token meta tag or the what-csrf cookie).
Or call the action and use its return value. On the client the call is a transition (or joins the one it is called in): it posts to the same endpoint, then refetches what the action revalidates, inside that transition, and resolves once that has landed:
import { useActionState } from 'what-framework';
import { createPostAction } from '../actions/posts.js';
export function useCreatePost() {
// [result, create, isPending]: the result commits with the refetched data
return useActionState((previous, post) => createPostAction(post), null);
}
A hand-rolled fetch has to supply everything the wrapper does: the X-What-Action header, a JSON body shaped { args: [...] }, credentials: 'same-origin', and the X-CSRF-Token header. Omit the token and the endpoint answers 403.
Revalidating after a mutation
On the client, revalidate: [path] calls invalidatePath(path) inside the action's transition: the queries and route data read on that path refetch, and the screen goes from the old data to the new in one commit. invalidates: [key] does the same for query keys (see Data Fetching). On the server, the revalidate / revalidateTags options fire after the action resolves, purging the origin ISR cache (and any CDN) so the next request re-renders with fresh data. Two conditions apply to that. They run only on the dispatched path, so calling the action function directly on the server (from a loader, a route, or another action) runs the function and revalidates nothing. And they need a bound cache engine: the deploy adapters bind whichever what-isr engine you hand them, and a manual server binds one itself with setRevalidationHandler(engine). Unbound, both helpers are no-ops that log a dev warning. For the direct-call case, purge by hand:
import { revalidatePath, revalidateTag } from 'what-framework/server';
await revalidatePath('/'); // purge one path
await revalidateTag('posts'); // purge everything tagged 'posts'
Two revalidates, on purpose
revalidatePath/revalidateTag (the server's rendered cache) are distinct from invalidatePath (the client's query cache). The first pair purges the rendered cache through the bound engine; invalidatePath, from what-framework/data (what-server re-exports it), refetches what was read on the path and calls the callbacks you registered with onRevalidate(path, cb). See Caching & ISR.
CSRF & error masking
The handler checks where a request comes from by default. A request the browser marks cross-site (Sec-Fetch-Site, or an Origin that is not this host) is refused with 403, and one it marks same-origin is accepted. A request with neither header must carry a CSRF token (inject the session's via getCsrfToken; a meta tag is emitted into uncached documents), and a token sent with any request must match. It fails closed: a missing or bad token is rejected. trustedOrigins lets other origins of the app post. An Origin is compared with this server's origin, scheme included: the scheme is the connection's (a TLS socket, or the URL the platform gives the handler), or X-Forwarded-Proto when a proxy sends it, which a page on another site cannot set. Behind a TLS-terminating proxy, have the proxy send X-Forwarded-Proto, or list the https:// origin in trustedOrigins. Fetch clients send it as the X-CSRF-Token header, plain form posts as the _csrf or what-csrf-token field, and the header wins when both are present. Thrown errors are masked to a generic 500 so internal details never reach the client, and the real error is logged server-side.
An action returns a value; it does not own the HTTP response. There is no channel for it to set headers, so anything that must issue Set-Cookie (sign-in, sign-out, session rotation) belongs in a normal route handler, not an action.