Server

"use server"

Edit this page

"use server" will enable actions in the server environment only (i.e. console logging, etc.).

const logHello = async (message: string) => {
"use server";
console.log(message);
};

Basic usage

When using "use server", regardless of whether rendering is happening on the server or in the browser, the functions it apply to will only run on the server.

To do this, compilation is used to transform the "use server" function into an RPC call to the server.

If "use server" is inserted as the first line in a file, the entire file will become server-only.

"use server";
const logHello = async (message: string) => {
console.log(message);
};

However, if "use server" is inserted as the first line of a function, only that function will be server-only:

const logHello = async (message: string) => {
"use server";
console.log(message);
};
logHello("Hello");

In both of these examples, the logHello function, it would only show in the server console regardless of whether rendering was on the server or in the browser


Serialization

Server functions allow the serialization of many different data types in the response, using the Seroval serializer. The full list is available in Seroval's source code.


Meta information

To get a stable function-specific identifier, even for parallel processes or multiple cpu cores or workers, use the getServerFunctionMeta

Report an issue with this page