How to use Server Actions with NextJS

Discover why and how NextJS Server Actions improve performance on both Client Components and Server Components, and learn how to use them in your projects.
•
Updated on August 14, 2026
•
5 min read
Believemy logo

Nowadays, performance and efficiency have become major concerns. Users are increasingly demanding about their experience when spending time on a website.

To further optimize page load performance, we now have a magical feature: Server Actions.

Good to know

Server Actions were introduced in Next.js 13 but became truly usable starting with Next.js 14.

They allow data management directly on the server, while simplifying client-side page loading.

Warning

If you're a beginner, it is essential to follow a comprehensive React course before using Next.js. A good Next.js developer is, above all, a good React developer (who is, in turn, a good JavaScript developer)! 😋

 

Understanding Server Actions

Server Actions, as mentioned, are one of Next.js's flagship features. They enable executing instructions on the server directly from components (even Client Components).

Good to know

A Client Component, simply put, is code that runs in the user's browser. By contrast, a Server Component is executed on the server and sent pre-rendered to the user.

Unlike methods like getServerSideProps or getStaticProps (deprecated as of Next.js 13), Server Actions integrate directly into components without requiring dedicated pages or complex hooks.

 

Why Use Server Actions?

Using Server Actions offers several benefits:

  1. Improved Performance: By executing code directly on the server, client-side load is reduced, significantly improving load times.
  2. Simplified Code: Server Actions eliminate the need to create separate API files or use functions like getServerSideProps. Everything can now be managed within the components themselves.
  3. Reduced Client-Side JavaScript: Since most of the work is done server-side, the size of the JavaScript bundle sent to the client is smaller, speeding up the application.

 

Setting Up Server Actions in Next.js

Initializing a Next.js Project

If you're new to Next.js, here’s how to initialize a project:

CONSOLE
npx create-next-app@latest

During initialization, you can choose between the app or pages router. Choose the app router. It’s the latest router offered by Next.js, enabling better application structure and full use of Server Components and Server Actions.

Configuring Server Actions

Server Actions are used directly within components. Simply mark a function with the async attribute to use it as a Server Action.

Example: In a Server Component

Server Components can use:

  • Inline functions
  • Modules with "use server"

Here’s an example using an inline function:

JSX
export default function Page() {
    // ...

    // Server Action
    async function create() {
        "use server";
        const res = await fetch('https://believemy.com');
        // ...
    }

    return (

    );
}

In this example, we dynamically fetch data for an article. This approach greatly simplifies data flow while reducing the amount of client-side code executed.

Example: Using Modules

For modular use, create a new file and include the use server directive. All functions in this file are treated as Server Actions.

JSX
'use server';

export async function create() {
    // ...
}

export async function replace() {
    // ...
}

To use the Server Action, include the following in a Server Component:

JSX
import { create } from "./actions";

export default function ServerComponent() {
    return;
}

The action attribute informs Next.js to use a Server Action. It sends all form data to the server when the user submits the form.

 

Example: In a Client Component

Client Components can only use modules for Server Actions. Using the previous actions.jsx example:

JSX
'use client';

import { create } from "./actions";

export default function ClientComponent() {
    return;
}

 

API Routes vs. Server Actions

With the advent of Server Actions, many developers wonder if API Routes are still necessary.

Good to know

API Routes, introduced in earlier Next.js versions, create dedicated endpoints for APIs, used throughout a project to communicate with the server. 👀

With Server Actions, these dedicated routes are mostly unnecessary, as you can handle requests directly in components.

Here’s a summary:

When to Use API Routes:

  • When you need to expose API endpoints for external services.
  • To centralize server logic in a single file.

When to Use Server Actions:

  • When data retrieval is directly tied to component rendering.
  • To optimize performance and simplify code by avoiding additional client API requests.
Good to know

Another advantage of Server Actions is caching. Requests to fetch the latest articles on your site, for instance, can be cached to avoid repeated executions, significantly improving load times, especially for infrequently updated data.

 

FAQ on Server Actions

Our learners often ask questions about Server Actions. Here’s a quick summary of the most insightful ones:

Question

What are best practices for using Server Actions in Next.js?

Ensure proper server-side error handling and only use Server Actions when truly necessary. Otherwise, they can add unnecessary complexity to your code.

 

Question

Do Server Actions affect SEO?

No, they even improve SEO by speeding up page rendering.

 

Question

Can I use external APIs with Server Actions?

Yes, Server Actions are great for interacting with external APIs. It also ensures API keys stay secure!

 

Question

Are API Routes still useful in Next.js 14 and beyond?

Yes, for cases where you need to expose an API to external services. However, Server Actions suffice for most use cases.

 

Question

How can I learn Next.js?

With our course! 😇 You can find it here.

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Setting up a loader when changing a Nextjs page

Have you ever been on a site where you click on a link and nothing happens? Or perhaps you've wondered whether your request was actually received? This is where having a loader comes in. In this article, we'll explain why it's important to have a loader on your website, how to set it up in your Nextjs project and create a function that will display the loader until the page is loaded. If you want to improve the user experience on your website and reassure your visitors, this article is for you.

December 11, 2024

How to use localStorage in a NextJs or React application

The aim of this article is to teach you how to use localStorage in a Next.JS or React application. Indeed, there are a few differences with the normal use, in a vanilla application.

December 11, 2024

What's new in React 19

React 19 has just been announced by the Meta team. Powerful new features have been introduced almost two years after the release of React 18.3. Let's find out what's new with React 19.

December 5, 2024

Send an email with NextJS and SendGrid for free

Have you always wanted to send emails with JavaScript? Or more specifically, how to send emails with React or NextJS? Then let's find out together how to use SendGrid! It's a quick and easy solution to set up that will help us ensure that our emails are sent and, most importantly, received.

December 9, 2024

Code faster with React using these techniques

Learn how to code faster with React thanks to simple but effective techniques used by experienced developers.

December 9, 2024

React vs NextJS: what should you choose for your project in 2025?

Which technology should you use, React or NextJS? Discover their advantages and disadvantages to choose the one best suited to your projects.

December 9, 2024

What's new in NextJS 15

NextJS 15 was presented to the world on October 24, 2024 in San Francisco. Here's everything we know about NextJS 15's powerful new features!

December 5, 2024

How to use React's useEffect and useRef hooks?

Discover the best practices to adopt with useEffect and useRef, what they're for and how to use them in the right way.

December 11, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.