Skip to the page

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.

The 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 or redirect, a fresh RSC payload for the current route. revalidateTag with a SWR profile does not re-render immediately.
  • Forms: <form action={createPost}> works before JavaScript loads when rendered from a Server Component (progressive enhancement); use useActionState for returned errors and useFormStatus for pending buttons.
  • The client dispatches actions one at a time. Do not use Promise.all to 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_KEY stable 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

  1. How do Server Actions work under the hood?
  2. What HTTP request does calling an action produce?
  3. How does the page update after an action without a second request?
  4. What happens if you Promise.all three actions?
  5. What breaks during a rolling deployment?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview