Framework InternalsBaseline · Newly

Server Actions & Serialized Closure Boundaries

Mental Model

A Server Action compiles into an HTTP POST endpoint identified by an opaque build-time action ID. Captured closure variables are serialized into hidden form inputs or JSON payloads constrained by Structured Clone rules, while UI updates pair with Optimistic Rollback.

Interactive Simulator

Server Action RPC & Closure Boundary

Trace how a 'use server' closure compiles into an encrypted RPC boundary.

Client Stub (act_9f2a)
HTTP POST
Server Handler
Step 1 / 3: 1. Build-Time Stub & Closure Bind

Compiler strips server code from client bundle and binds encrypted orgId to actionId.

const stub = createServerReference('act_9f2a').bind(null, encrypt(orgId));
Internal State / Memory
actionId
act_9f2a
boundClosure
encrypted(orgId)
serverCodeInClient
false

Under the Hood

The bundler splits a "use server" module into a server-only handler map and a thin client RPC stub.

  1. Action ID & Closure Capture: At build time, the compiler replaces the server function with createServerReference(actionId). Any lexical variable captured from an enclosing Server Component is encrypted and bound via .bind(null, encryptedClosure).
  2. Wire Transport: Calling the stub serializes arguments (and bound closure tokens) into a POST request with a Next-Action (or framework RPC) header.
  3. Server Dispatch & Revalidation: The server runtime looks up actionId, decrypts bound closure arguments, executes the handler, and streams updated UI fragments back in the same HTTP response.
TSX
// Server Component captures `orgId` into a hidden encrypted closure
export default function InviteForm({ orgId }: { orgId: string }) {
  async function inviteMember(formData: FormData) {
    'use server';
    // Must re-verify session & orgId authorization here!
    const user = await requireSession();
    await db.invites.create({ orgId, email: String(formData.get('email')), by: user.id });
  }
  return <form action={inviteMember}><input name="email" /><button>Invite</button></form>;
}

Problem Genealogy

  • 2016 (Manual REST/GraphQL Glue): Teams maintained separate API route files, request DTO schemas, and client fetch wrappers—often 150+ lines of boilerplate per form.
  • 2021 (tRPC / End-to-End Type Inference): Shared TypeScript router types across client and server, eliminating manual fetch typing while keeping explicit endpoints.
  • 2023 ("use server" Compiler RPC): Bundlers automated endpoint generation and closure binding directly inside component trees.

Production Pitfalls

Production Pitfall
  • Missing Authorization Checks Inside Actions: Because every "use server" function is a public HTTP POST endpoint, attackers can replay actionId with forged payloads if the handler skips session/RBAC verification.
  • Non-Serializable Arguments: Passing class instances, Map callbacks, or DOM events to a Server Action throws runtime serialization errors across the wire boundary.
When Not to Use
  • When not to use Server Actions: Never use Server Actions for cacheable GET data reads or public webhook ingestion; POST-only RPC endpoints bypass CDN edge caching and serialize sequentially in some frameworks.

Connections & Logical Next Steps

Framework InternalsServer Actions & Serialized Closure Boundaries
What This Unlocks
Leaf concept