Technical
Next.js Server Actions: Replacing Half My API Routes
Every form I built used to follow the same shape: client component, fetch to /api/thing, API route handler, response JSON, optimistic update. Five layers for a form submission. Server Actions collapse this to one function.
The Before
// app/contact/page.tsx (client)
'use client'
export default function Contact() {
async function submit(formData) {
await fetch('/api/contact', { method: 'POST', body: formData })
}
return <form action={submit}>...</form>
}
// plus app/api/contact/route.tsTwo files, one fetch, manual JSON serialization, manual error shape.
The After
// app/contact/page.tsx (server component)
async function submit(formData: FormData) {
'use server'
await saveContact(formData.get('email'))
revalidatePath('/contact')
}
export default function Contact() {
return <form action={submit}>...</form>
}One file. No API route. Direct database call. Next.js handles the serialization, the fetch, the revalidation. Progressive enhancement works: the form submits even with JS disabled.
When Not To Use Them
Server Actions are for mutations driven by your own UI. If the endpoint needs to be public, called from mobile, or consumed by another service, keep the API route. Actions are an internal app convenience, not a public interface.
What Changed For Me
Roughly half my /api/ folder is gone. Contact forms, newsletter signups, admin mutations: all Server Actions now. The public API that powers the mobile app stays in /api/. The split is cleaner than trying to unify them.
See the Next.js Server Actions docs. If you are on App Router and still writing API routes for every form, try this for your next feature.
RELATED READING
The Consulting Shift I Am Making In Year Two
After a year of writing and building, my consulting practice is changing shape. Shorter engagements. Sharper outcomes.
ReadThe Frontend Shift: Shipping Less JavaScript In Year Two
A year ago I reached for Next.js for everything. This year I often reach for nothing.
ReadThe Serverless Lesson I Would Write On A Sticky Note
After a year of shipping serverless projects, one rule explains most of the wins and all of the losses.
Read