Server Functions are asynchronous functions that run on the server and can be invoked from the client. In mutation contexts, they’re called Server Actions. They integrate with Next.js caching and are triggered via formAction, action, or startTransition. Only POST requests can invoke them.
Use the 'use server' directive to mark a function as server-side. You can place it inside a function or at the top of a file:
// app/lib/actions.ts
export async function createPost(formData: FormData) {
'use server'
const title = formData.get('title')
const content = formData.get('content')
// Update data and revalidate
}You can inline Server Functions directly in Server Components:
// app/page.tsx
export default function Page() {
async function createPost(formData: FormData) {
'use server'
// ...
}
return <></>
}Client Components can’t define Server Functions, but they can import and invoke them:
// app/ui/button.tsx
'use client'
import { createPost } from '@/app/actions'
export function Button() {
return <button formAction={createPost}>Create</button>
}// app/client-component.tsx
'use client'
export default function ClientComponent({ updateItemAction }) {
return <form action={updateItemAction}>...</form>
}action or formAction to submit data.onClick or useEffect to trigger updates.// app/ui/form.tsx
import { createPost } from '@/app/actions'
export function Form() {
return (
<form action={createPost}>
<input name="title" />
<input name="content" />
<button>Create</button>
</form>
)
}// app/like-button.tsx
'use client'
import { incrementLike } from './actions'
import { useState } from 'react'
export default function LikeButton({ initialLikes }) {
const [likes, setLikes] = useState(initialLikes)
return (
<>
<p>Total Likes: {likes}</p>
<button onClick={async () => {
const updatedLikes = await incrementLike()
setLikes(updatedLikes)
}}>
Like
</button>
</>
)
}Use useActionState to show a loading indicator while the Server Function is running:
// app/ui/button.tsx
'use client'
import { useActionState, startTransition } from 'react'
import { createPost } from '@/app/actions'
import { LoadingSpinner } from '@/app/ui/loading-spinner'
export function Button() {
const [state, action, pending] = useActionState(createPost, false)
return (
<button onClick={() => startTransition(action)}>
{pending ? <LoadingSpinner /> : 'Create Post'}
</button>
)
}Use revalidatePath or revalidateTag to refresh cached data after updates:
// app/lib/actions.ts
import { revalidatePath } from 'next/cache'
export async function createPost(formData) {
'use server'
// Update data
revalidatePath('/posts')
}Use redirect to navigate after a mutation:
// app/lib/actions.ts
'use server'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
export async function createPost(formData) {
// Update data
revalidatePath('/posts')
redirect('/posts')
}Use the cookies API inside Server Actions to get, set, or delete cookies:
// app/actions.ts
'use server'
import { cookies } from 'next/headers'
export async function exampleAction() {
const cookieStore = await cookies()
cookieStore.set('name', 'Delba')
cookieStore.delete('name')
}Use useEffect to trigger updates on mount or dependency change:
// app/view-count.tsx
'use client'
import { incrementViews } from './actions'
import { useState, useEffect, useTransition } from 'react'
export default function ViewCount({ initialViews }) {
const [views, setViews] = useState(initialViews)
const [isPending, startTransition] = useTransition()
useEffect(() => {
startTransition(async () => {
const updatedViews = await incrementViews()
setViews(updatedViews)
})
}, [])
return <p>Total Views: {views}</p>
}Server Functions in Next.js make it easy to update data, revalidate cache, and reflect changes in the UI—all with minimal client-side complexity. Whether triggered by forms, buttons, or lifecycle hooks, these actions help keep your app fast, interactive, and consistent.