- All Courses
- Next.js for Production
- Server Actions and Mutations
- Writing your first Server Action
Writing your first Server Action
Marking a function with the server directive turns it into an endpoint.
Overview
A function that runs on the server, called from the client
Marking a function with the server directive turns it into an endpoint. You pass the function itself to a form action, and the framework generates the network call, the serialisation, and the routing.
What actually crosses the wire is the arguments, so they must be serialisable, and the return value, which the caller receives back. The function body — including any secret it closes over — never leaves the server.
After a successful write, the cached data for the affected routes is still the old data. An action that mutates should end by revalidating the path or tag it invalidated, or the user will submit a form and watch nothing change.
In this lesson you will:
- Define a server function you can call from a form
- Understand what crosses the network boundary
- Refresh the page data after a write
Resources
Notes are not saved yet — they clear when you leave this page.
Streaming with Suspense boundaries
26m
Form validation and progressive enhancement
14m