Mutations & Auth · 10 min read · Intermediate
Server Actions, Progressive Forms & Validation
Handle form submissions and database writes securely using 'use server' actions, Zod validation, and revalidatePath.
What you'll learn
- Define server-only mutations with the 'use server' directive
- Validate every FormData input on the server before touching the DB
- Call revalidatePath() after mutations so UI refreshes automatically
Overview
Handle form submissions and database writes securely using ‘use server’ actions, Zod validation, and revalidatePath.
Next.js App Router unifies server rendering, streaming, type-safe mutations, and edge-ready caching inside a single React + TypeScript codebase.
Production Code Example
// app/tasks/actions.ts
'use server';
import { revalidatePath } from 'next/cache';
export async function createTask(formData: FormData) {
const title = String(formData.get('title') ?? '').trim();
if (title.length < 3) throw new Error('Title too short');
// await db.insert(tasks).values({ title });
revalidatePath('/tasks');
}
Key Takeaways
- Define server-only mutations with the ‘use server’ directive
- Validate every FormData input on the server before touching the DB
- Call revalidatePath() after mutations so UI refreshes automatically
Quick Check
Check Your Understanding
Score: 0 / 1
1. Why must you validate and authorize every Server Action on the server even if the client form already validates inputs?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.