Lesson 3.1

Writing your first Server Action

Marking a function with the server directive turns it into an endpoint.

7mIntermediate12.2k students

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

Previous Lesson
Next Lesson
Writing your first Server Action — Next.js for Production — Vertex