← Back to list

Server Actions in Next.js App Router: When & How to Use Them (Next.js 16 Edition)

Next.js has been steadily moving toward a simpler, more powerful full-stack model, and with Next.js 16, that vision is clearer than ever.

Amir · 2026-01-04 22:32 · 5 claps · 2.4 min read
#nextjs #reactjs #server-action #nodejs
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Server Actions in Next.js App Router: When & How to Use Them (Next.js 16 Edition)

Next.js has been steadily moving toward a simpler, more powerful full-stack model, and with Next.js 16, that vision is clearer than ever.

At the center of this shift are Server Actions — a feature that eliminates much of the boilerplate we’ve traditionally written for APIs, form submissions, and data mutations. Combined with Next.js 16’s new explicit caching system, Server Actions enable a cleaner, faster, and more predictable way to build modern web applications.

In this article, we’ll cover:

  • What Server Actions are
  • When you should (and shouldn’t) use them
  • How they fit into the App Router
  • What’s new in Next.js 16 around caching
  • Practical examples and best practices

What Are Server Actions?

Server Actions are asynchronous functions that always run on the server but can be triggered directly from your React components — including client components.

They remove the need for:

  • Custom API routes
  • Client-side fetch calls for mutations
  • Manual serialization and error handling

You define them using a simple directive:

'use server'

Once defined, Server Actions can be passed directly to a <form> or invoked from a client component — and Next.js handles the network layer for you.

Why Server Actions Matter

Server Actions solve a long-standing problem in full-stack React:

“Why do we need to build an API just to submit a form?”

With Server Actions:

  • Business logic lives on the server
  • Sensitive code never reaches the browser
  • The UI stays declarative and minimal
  • They are especially useful for:
  • Creating, updating, and deleting data
  • Handling form submissions
  • Triggering cache invalidation
  • Coordinating UI updates after mutations

Server Actions in the App Router

The App Router (/app) is built around React Server Components (RSC). Server Actions are a natural extension of this architecture.

Typical Flow

  1. Server Component fetches data
  2. UI renders
  3. User submits a form or triggers an action
  4. Server Action runs
  5. Cache is revalidated
  6. UI updates automatically

No API routes. No client-side data mutation logic.

Basic Server Action Example

// app/actions/create-post.ts
export async function createPost(formData: FormData) {
  'use server';

  await db.post.create({
    data: {
      title: formData.get('title'),
      content: formData.get('content'),
    },
  });
}

Usage in a component:

<form action={createPost}>
  <input name="title" />
  <textarea name="content" />
  <button type="submit">Publish</button>
</form>

This is already production-ready — no API routes required.

What’s New in Next.js 16: Explicit Caching

One of the biggest changes in Next.js 16 is how caching works.

🔄 Before

Caching was often implicit and confusing.

✅ Now

Caching is explicit and opt-in.

By default, everything is dynamic unless you explicitly cache it.

Using use cache

Next.js 16 introduces the use cache directive to explicitly mark functions or components as cacheable.

'use cache';

export async function getPosts() {
  return fetch('https://example.com/posts').then(res => res.json());
}

This makes the behavior predictable and easier to reason about — a major improvement for large applications.

Cache Tags & Revalidation (Game Changer)

Next.js 16 also introduces tag-based cache invalidation, which pairs perfectly with Server Actions.

Cache with Tags

import { cacheTag } from 'next/cache';

export async function getPosts() {
  'use cache';
  cacheTag('posts');
  return fetch('/api/posts').then(res => res.json());
}

Revalidate from a Server Action

import { revalidateTag } from 'next/cache';

export async function createPost(data) {
  'use server';

  await db.post.create(data);
  revalidateTag('posts');
}

This ensures:

  • Only relevant cache entries are invalidated
  • The UI updates immediately
  • Performance remains optimal

When Should You Use Server Actions?

✅ Use Server Actions When:

  • You need to mutate data
  • You’re handling forms
  • You want tight integration with caching
  • You want to avoid unnecessary API layers

❌ Avoid Server Actions When:

  • You need a public API
  • You’re building a mobile client
  • You need WebSocket or long-lived connections

Server Actions are not a replacement for APIs, but they are a superior choice for internal app logic.


메타데이터
post_id
9ed24a49ab4d
slug
server-actions-in-next-js-app-router-when-how-to-use-them-next-js-16-edition-9ed24a49ab4d
url
https://medium.com/@amirjld/server-actions-in-next-js-app-router-when-how-to-use-them-next-js-16-edition-9ed24a49ab4d
canonical_url
https://medium.com/@amirjld/server-actions-in-next-js-app-router-when-how-to-use-them-next-js-16-edition-9ed24a49ab4d
author_url
https://medium.com/@amirjld
status
ok
fetched_at
2026-06-09 15:37:30