Skip to main content
Alvin QuachFull Stack Developer
HomeProjectsExperienceBlog
HomeProjectsExperienceBlog
alvinquach

Full Stack Developer building systems that respect complexity.

Open to opportunities

AQ

Projects

  • All Projects
  • Hoparc Physical Therapy
  • OpportunIQ
  • Hoop Almanac
  • SculptQL

Knowledge

  • Blog
  • Experience
  • Interview Prep

Connect

  • Contact
  • LinkedIn
  • GitHub
  • X

Resources

  • Resume
© 2026All rights reserved.
Back to Blogs
Tutorial
Depth: ●●●○○

Server Actions in Next.js: When to Use Them Over API Routes

Server Actions simplify server-side operations without creating API routes. Learn when to use 'use server', how draftMode() works, and practical patterns from my portfolio's draft preview system.

Published September 24, 20251 min readImportance: ★★★★☆
Share:

Summary: Server Actions in Next.js

Server Actions are async functions marked with 'use server' that run only on the server but can be called directly from Client Components.

They replace many simple use cases for API routes (like toggling settings, handling form submissions, or managing cookies) without needing separate route files.

Core Capabilities

Run exclusively on the server

Callable from Client Components

Access to cookies, headers, database, and other server-only APIs

No manual request/response handling or API route boilerplate

Example: Disabling Draft Mode

A Server Action to turn off Sanity draft mode and clear the draft cookie:

```ts

// app/actions/draft.ts

'use server'

import { draftMode } from 'next/headers'

export async function disableDraftMode() {

(await draftMode()).disable()

await new Promise((resolve) => setTimeout(resolve, 1000))

}

```

Used from a Client Component:

```tsx

'use client'

import { useRouter } from 'next/navigation'

import { disableDraftMode } from '@/app/actions/draft'

export function DisableDraftMode() {