Skip to main content

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.

A Server Function call crosses the network
1Client event / form submit
2Call Server Function reference
3Serialize arguments
4Network request
5Server validates + executes
6Serialize result
7Client receives result

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) {
// ...
}
Do not confuse component execution with callable functions
Server Component
Runs on the server through the RSC environment. No 'use server' marker is required.
Server Function
Async function marked for invocation through the server-function transport.

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>
Function identity and Action usage are different ideas
Server Function
Callable server command
Server Action
Form/Action workflow with pending and result semantics

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.

Mutation security belongs inside the Server Function
Client-controlled arguments
Authenticated actor
Safe mutation
Client UI

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.

Good payloads
Primitives ยท arrays ยท plain supported objects ยท Dates ยท FormData ยท typed binary data ยท Server Function references
Wrong payloads
DOM events ยท database connections ยท arbitrary class instances ยท ordinary closures ยท secrets

Extract the value you need instead of forwarding browser event objects.

Reads and writes have different ownersโ€‹

Prefer query/command separation
READServer Component
Read server data during render when the architecture supports it.
MUTATEServer Function
Validate, authorize, and perform a server-side command.

Server Functions are not a generic cached read API.

Optimistic UI never weakens the server boundaryโ€‹

1User submits intent
2Optimistic UI projects likely result
3Server Function validates + authorizes
4Persist / reject
5Canonical data refreshes
6UI converges or rolls back

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.
What should this mutation guarantee?
Payment / order / irreversible provisioningDesign idempotency explicitly
Several related writes must succeed togetherUse transaction semantics
Expected validation failureReturn safe domain state
Unexpected infrastructure failureThrow + observe/recover through error architecture

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:

  1. authenticate the actor;
  2. authorize the target operation;
  3. validate and normalize runtime input;
  4. keep secrets/internal objects server-side;
  5. use transactions when needed;
  6. design idempotency for sensitive commands;
  7. return the minimum serializable UI result;
  8. log safely with request/actor/action context;
  9. refresh canonical data through framework policy;
  10. 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.

Summaryโ€‹

Server Function = distributed mutation boundary
Client intent
Server authority
Validate ยท authorize ยท persist
Canonical UI

Referencesโ€‹