We've shipped three apps on Next.js server actions. Here's what survived contact with real users.
1. One wrapper for every action
Every action goes through a small defineAction() helper that validates input with zod, checks auth, applies a rate limit and maps errors to a typed result. Handlers never throw raw errors at the client.
export const renameProject = defineAction({
name: 'renameProject',
input: z.object({ id: z.number().int(), name: z.string().min(2).max(60) }),
rateLimit: { limit: 20, windowSec: 60 },
handler: async ({ input, viewer }) => {
const project = await db.project.findFirstOrThrow({ where: { id: input.id, ownerId: viewer.id } });
return db.project.update({ where: { id: project.id }, data: { name: input.name } });
},
});
2. Results, not exceptions
The client gets { ok: true, data } or { ok: false, error }. Forms show field errors inline; everything else becomes a toast.
3. Side effects after commit
Emails, notifications and analytics run after the transaction commits. If they fail, the user's action still succeeded.
4. Revalidate narrowly
Tag-based revalidation for the exact data that changed, never revalidatePath('/').
What we got wrong at first
Putting business logic inside components. Move it to a server module the action calls; your tests will thank you.
ByteNomad Dev Studio · Next.js, Postgres, bots · milestone escrow
- Reacted by