Define a server action

Another feature introduced in React 19 is server actions.

Server actions are a way to define functions that can be invoked on the client but are executed on the server.

It works like a server endpoint, but with the convenience of a normal function.

Let's start by creating a new actions folder inside src:

mkdir src/actions

Inside it, we create a like-post.ts file. This file contains a function to like a post give its id.

We mark the file with "use server" to indicate that it is a server action:

src/actions/like-post.ts
"use server";

export const likePost = (id: number) => {
  /// ...
};