Server Actions Overview
Server Actions are async server functions callable from forms and client components: mutations, progressive enhancement, revalidation.
- nextjs
- server-actions
Server Actions are async functions that execute on the server, marked with "use server". You can call them from forms (progressive enhancement) or from Client Components as async functions. They are the App Router-native mutation path.
Docs: Server Actions and Mutations.
Form action
// app/actions.ts
'use server';
import { revalidatePath } from 'next/cache';
export async function createNote(formData: FormData) {
const title = String(formData.get('title') ?? '');
await db.notes.create({ title });
revalidatePath('/notes');
}
// app/notes/page.tsx
import { createNote } from '../actions';
export default function NotesPage() {
return (
<form action={createNote}>
<input name="title" required />
<button type="submit">Add</button>
</form>
);
}
Works without client JS; with JS, Next enhances the submission.
Client invocation
'use client';
import { createNote } from '../actions';
export function AddButton() {
return (
<button
type="button"
onClick={async () => {
await createNote(new FormData());
}}
>
Quick add
</button>
);
}
Rules and safety
- Validate inputs on the server — never trust the client.
- Auth checks inside the action.
- Revalidate caches after success.
- Avoid huge payloads; actions are not a general RPC free-for-all without design.
- Closures can capture data — understand serialization boundaries.
Interview out-loud
“Server Actions are use server functions for mutations. Forms can post to them without client JS, and client code can await them. I validate and authorize on the server, then revalidatePath or revalidateTag so UI caches refresh.”
Related on this site
Further reading
Edge cases worth rehearsing
Interviewers and production incidents cluster around the same edges: first render versus update, empty and loading states, Strict Mode double setup, concurrent interruptions, and what happens when identity (key, route params, user id) changes mid-edit. Walk one concrete user journey end-to-end — open, edit, navigate away, come back — and say which state survives.
Prefer fixing data flow and ownership before reaching for memoization or micro-optimizations. Prefer event handlers over effects when a user action is the trigger. Prefer deriving values during render over mirroring props into state. Prefer stable list keys from business ids. Measure with the profiler when performance is the claim.
When you cite an API, mention one failure mode: abort on unmount, serializable props across server/client boundaries, focus restoration for dialogs, or cache invalidation after a mutation. Specific beats generic every time.
Edge cases worth rehearsing
Interviewers and production incidents cluster around the same edges: first render versus update, empty and loading states, Strict Mode double setup, concurrent interruptions, and what happens when identity (key, route params, user id) changes mid-edit. Walk one concrete user journey end-to-end — open, edit, navigate away, come back — and say which state survives.
Prefer fixing data flow and ownership before reaching for memoization or micro-optimizations. Prefer event handlers over effects when a user action is the trigger. Prefer deriving values during render over mirroring props into state. Prefer stable list keys from business ids. Measure with the profiler when performance is the claim.
When you cite an API, mention one failure mode: abort on unmount, serializable props across server/client boundaries, focus restoration for dialogs, or cache invalidation after a mutation. Specific beats generic every time.
Related guides
- Auth Patterns OverviewAuth in Next.js App Router: cookies/sessions, middleware gates, server checks, and avoiding client-only security theater.
- Caching in Next.js OverviewNext.js caching layers: request memoization, data cache, full route cache, and router cache — what invalidates each.
- Client Components in Next.jsClient Components in the App Router: use client, hydration, bundling boundaries, and patterns that keep JS small.
- Data Fetching Patterns App RouterFetch data in Server Components with async/await and fetch caching: colocation, parallel requests, and client fallbacks.
- Deploying Next on VercelDeploy Next.js on Vercel: git integration, env vars, previews, build cache, and production checklist.