Sub topic 13
Server Functions and the reverse direction
This chapter assumes you know how Server Components, the "use client" boundary and the RSC payload fit together. It also assumes you are comfortable with async/await and HTML forms.
So far, data has flowed in one direction: the server renders a tree and sends it to the client as a payload. Server Functions cover the reverse direction. They let client code ask the server to run a function and return a result.
What a Server Function is
A Server Function is an async function that runs only on the server. You mark it by putting the "use server" directive at the top of a module. Every exported function in that module becomes callable from the client.
The client never receives the function body. The bundler replaces the import with a reference, much like the client references you saw in the payload. The reference holds an identifier, and calling it sends a network request to the server.
// app/actions.js
'use server';
export async function addPost(formData) {
const title = formData.get('title');
if (typeof title !== 'string' || title.length === 0) {
throw new Error('Invalid title');
}
// save the post to a database here
}// app/NewPost.js (Server Component)
import { addPost } from './actions';
export default function NewPost() {
return (
<form action={addPost}>
<input name="title" />
<button type="submit">Add</button>
</form>
);
}Passing the function to a form's action prop is allowed because a Server Function reference is serializable. It crosses the boundary like any other prop. When the user submits the form, the browser sends the form data and the function's identifier to the server. The server looks up the function, runs it and responds.
Two different directives
The two directives are easy to confuse because they mark different things.
| Directive | Marks | Effect |
|---|---|---|
| `"use client"` | A module boundary | Code from this module onward is bundled for the browser |
| `"use server"` | Server-callable functions | The client receives a reference, not the code |
"use server" does not mark a Server Component. Server Components need no directive in an RSC framework, because they are the default.
The round trip
A call to a Server Function is a request and a response. The response often contains a fresh RSC payload, so the client can update the screen with new server-rendered output.
The exact wire format depends on your framework. The pattern is the same: a reference goes up, and a result and payload come back.
Validate every input
A Server Function is a public network endpoint. Anyone can send a request to it, not only your own form. The arguments are untrusted data, even when the UI only offers safe choices.
Follow these rules in every Server Function:
- Check the type and shape of each argument, as
addPostdoes withtypeof title. - Check authentication and authorization inside the function, not only in the component that renders the button.
- Return only the data the caller needs.
Hiding a button from some users does not protect the function behind it.
Common errors
- Putting
"use server"in a file that also has"use client". A module is one or the other. - Passing a non-serializable argument, such as a class instance or a function, from the client. Arguments follow the same serialization rules as props.
- Skipping authorization because the function is "internal".
Exercise
Why does a Server Function need to validate its inputs even though the form that calls it is written by you?
Show answer
A Server Function is reachable over the network, so any client can call it with arbitrary arguments, not just your form. The server must check types, shape and permissions itself.