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.
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.
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
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() {