Server Functions, 'use server', and mutation boundaries
Server Functions let client code invoke async functions that execute on the server. Even when the syntax resembles a normal call, this is a network and trust boundary.
The browser never directly executes the server implementation.
'use server'โ
async function createNote(formData) {
'use server';
await db.notes.create({ title: formData.get('title') });
}
A module can also mark exported async functions:
'use server';
export async function createNote(formData) {
// ...
}
'use server' marker is required.Server Function vs Server Actionโ
A Server Function is a server-callable async function. A Server Action is a Server Function being used in an Action/mutation flow such as a form submission or Transition-backed operation.
<form action={updateProfile}>...</form>
Forms are a first-class boundaryโ
async function requestUsername(formData) {
'use server';
const username = formData.get('username');
await saveUsername(username);
}
export default function Page() {
return (
<form action={requestUsername}>
<input name="username" />
<button type="submit">Save</button>
</form>
);
}
Forms can integrate FormData, pending state, progressive enhancement, Actions, and result state. Native form semantics remain valuable even in advanced React applications.
useActionState on the client sideโ
'use client';
import { useActionState } from 'react';
import { updateProfile } from './actions';
export default function ProfileForm() {
const [state, action, isPending] = useActionState(
updateProfile,
{ error: null }
);
return (
<form action={action}>
<input name="displayName" disabled={isPending} />
<button disabled={isPending}>Save</button>
{state.error && <p>{state.error}</p>}
</form>
);
}
The server returns minimal serializable state; the client owns pending/result presentation.
Outside forms, coordinate mutations with Transitionsโ
'use client';
import { useTransition } from 'react';
import { likePost } from './actions';
export function LikeButton() {
const [isPending, startTransition] = useTransition();
function handleClick() {
startTransition(async () => {
await likePost();
});
}
return <button onClick={handleClick} disabled={isPending}>Like</button>;
}
Treat arguments as untrusted network inputโ
TypeScript does not validate what arrives at the server boundary.
A hidden button is never authorization.
'use server';
export async function deleteProject(projectId) {
const user = await requireUser();
const project = await db.projects.find(projectId);
if (!project || project.ownerId !== user.id) {
throw new Error('Not authorized');
}
await db.projects.delete(projectId);
}
Serializable payloadsโ
Arguments and results must fit React's supported serialization model. Send application data, not runtime machinery.
Extract the value you need instead of forwarding browser event objects.
Reads and writes have different ownersโ
Server Functions are not a generic cached read API.
Optimistic UI never weakens the server boundaryโ
Optimistic state is a presentation guess, not authority.
Framework policy after mutationโ
Refreshing server-rendered data may involve route refresh, framework cache invalidation, tag/path revalidation, navigation, or client-cache updates. Those mechanisms belong to the framework/data layer, not React Server Functions themselves.
Distributed-system concerns still applyโ
A Server Function call can be retried, duplicated, delayed, or fail partway through. Sensitive operations may require:
- idempotency keys;
- database transactions;
- timeout/retry policy;
- duplicate-submission handling;
- audit logs;
- trace/request IDs.
CSRF and browser securityโ
Server Functions use framework-managed transport, but normal web security remains relevant: allowed origins, cookies, CSRF protection, authentication, deployment topology, and secret handling.
Understand the framework's concrete security model instead of assuming the abstraction removes browser threats.
Production checklistโ
For each exposed Server Function:
- authenticate the actor;
- authorize the target operation;
- validate and normalize runtime input;
- keep secrets/internal objects server-side;
- use transactions when needed;
- design idempotency for sensitive commands;
- return the minimum serializable UI result;
- log safely with request/actor/action context;
- refresh canonical data through framework policy;
- test unauthorized direct invocation, not only the visible UI path.
Interview questionsโ
Junior: What does 'use server' mark?
Mid-level: Why must a Server Function validate and authorize arguments even when only an admin button calls it?
Senior: Design a Server Function for order placement that needs validation, authorization, idempotency, transactions, optimistic UI, revalidation, and observability.