Mutations, APIs and the request layer · senior
Server Actions: how they work
A Server Action is a "use server" function that the client calls with a POST to the current page; Next.js runs it, and in the same response returns its value plus the re-rendered page when you revalidate, refresh, set cookies or redirect.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
"use server"at the top of an async function (inline in a Server Component) or a file (all exports). Client Components import them from a"use server"file; they cannot define them.- At build time the client gets a reference (an encrypted action id plus a dispatcher). Calling it sends a POST with serialized arguments to the page that uses it.
- Single round trip: the response carries the return value and, if the action called
updateTag,revalidatePath,refresh, changed cookies orredirect, a fresh RSC payload for the current route.revalidateTagwith a SWR profile does not re-render immediately. - Forms:
<form action={createPost}>works before JavaScript loads when rendered from a Server Component (progressive enhancement); useuseActionStatefor returned errors anduseFormStatusfor pending buttons. - The client dispatches actions one at a time. Do not use
Promise.allto run actions in parallel; do parallel work inside one action or in a Route Handler. - Actions are for mutations. For reads, fetch in Server Components.
redirect()throws: call revalidation before it and keep it outside try/catch.- Deployments: action ids change between builds (rotated at least every 14 days), so old tabs can get "Failed to find Server Action"; keep
NEXT_SERVER_ACTIONS_ENCRYPTION_KEYstable across instances, prefer rolling deploys, show a retry. Body limit is 1 MB by default (serverActions.bodySizeLimit).
The red pen
Common traps: mistake → fix
Mistake: "Server Actions run in parallel like fetch calls"
Fix: the client dispatches them sequentially.
Mistake: "Use a Server Action to read data in a client component"
Fix: use Server Components or a Route Handler for reads.
Mistake: "Action ids never change"
Fix: new builds produce new ids; handle skew.
Easy to hard
Questions interviewers ask, easy to hard
- How do Server Actions work under the hood?
- What HTTP request does calling an action produce?
- How does the page update after an action without a second request?
- What happens if you
Promise.allthree actions? - What breaks during a rolling deployment?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.