Chapter
04 · Server Actions ("use server")
Mutation entry points: adapt UI input, call entity actions, trigger revalidation.
Mental Model
- Server Actions are the mutation boundary callable from the UI.
- They orchestrate: validate → authorize → call entity action → revalidate.
- No Prisma here; data authority stays in entity actions.
File Classification
Layer: Mutation boundary
Directive: "use server"
Runtime: Server
Prisma: ❌ Never
Calls: Entity actions for writes
Revalidation: ✅ Explicit
Canonical Example
"use server";
import { createProject } from "./actions/createProject";
import { getSession } from "@/auth";
import { revalidatePath } from "next/cache";
export async function createProjectAction(formData: FormData) {
const session = await getSession();
if (!session) throw new Error("Unauthorized"); // security
await createProject(formData, session.user.id);
revalidatePath("/projects"); // perf
}
Server vs Entity Actions (Quick)
- Server Action: callable by UI, no Prisma, orchestrates + revalidates.
- Entity Action: owns Prisma, validation, authorization; not called by UI directly.
- Keep mutations thin at server-action layer; keep rules/DB in entity actions.