Under The Hood
frontend nextjs Next.js 14+/15 (App Router)

Next.js Server Actions: RPC over the wire

Last updated
Prerequisites:
Next.js App Router basics
Understanding of React Server Components
  • nextjs
  • server-actions
  • rpc
  • forms
  • react
  • app-router

Read at your depth

The practical view

With 'use server' at the top of a file (or inline in a server component), you can define functions that run on the server but are called from the client: async function createPost(formData) { 'use server'; await db.insert(...); revalidatePath('/'); }. Use them in <form action={createPost}> or call them directly from event handlers in Client Components (passing the imported server function as a prop). The framework serializes arguments and return values over the wire. Forms get a progressive enhancement bonus: if JavaScript fails to load, the form still submits via a plain POST.

The same idea in other frameworks

react equivalent
// React 19: <form action={serverFunction}> is the same RSC
// mechanism — Server Actions are part of React, not Next.js

Legacy vs modern

Client-side fetch to an API route vs a Server Action

The fetch-to-API approach splits mutation and cache invalidation across two endpoints; a Server Action mutates and revalidates in one compiled RPC call.

before → after
API route + client fetch
await fetch('/api/posts', { method: 'POST', body: JSON.stringify({ title }) });
router.refresh();
Server Action
async function createPost(formData) {
  'use server';
  await db.insert(...);
  revalidatePath('/');
}

Interview gotchas

Under The Hood — a multi-depth technical interview hub.

Press ⌘ K to search.