Server Actions
Server Actions in Next.js 16 combine action execution + cache invalidation + page re-render into a single HTTP request. New APIs: updateTag() for immediate refresh, revalidateTag() for background revalidation, and refresh() for router refresh.
Note-taking with real Server Actions
Adding a note calls a 'use server' action. The page re-renders with fresh data in the same single HTTP roundtrip — no client-side state needed.
Welcome to the Server Actions demo!
updateTag vs revalidateTag vs refresh
updateTag(tag)Use when: User edits their profile, deletes a post
Expire + refresh IMMEDIATELY in same request
revalidateTag(tag, 'max')Use when: Publishing blog post, updating catalog
Mark as stale — background refresh on next read
refresh()Use when: Mark notification as read, update counters
Re-fetch RSC payload for current route
// app/demos/server-actions/actions.ts
'use server'
import { updateTag, revalidateTag } from 'next/cache'
import { db } from '@/lib/db'
/**
* updateTag() — read-your-writes semantics
* Expires cache AND immediately refreshes data within the SAME request.
* User sees their change right away.
*/
export async function updateUserProfile(userId: string, profile: Profile) {
await db.users.update(userId, profile)
updateTag(`user-${userId}`) // expire + refresh NOW
}
/**
* revalidateTag() — stale-while-revalidate
* Marks tag as stale. Users see old data while fresh data loads in background.
* Use for content where a slight delay is acceptable (blog posts, catalogs).
*/
export async function publishPost(postId: string) {
await db.posts.publish(postId)
revalidateTag('posts', 'max') // background refresh
}
/**
* refresh() — refresh client router
* Refreshes the RSC payload for the current route.
*/
export async function markAsRead(notificationId: string) {
await db.notifications.markAsRead(notificationId)
refresh() // re-render current route
}Single HTTP roundtrip
When a Server Action calls updateTag, revalidatePath, or refresh, Next.js bundles the action result and the re-rendered RSC payload into one Flight stream response.
// Next.js 16: action + revalidation + re-render = ONE HTTP request
// The response Flight stream carries both:
// 1. The action's return value (consumed by useActionState)
// 2. A newly rendered RSC Payload for the current route
export async function createPost(formData: FormData) {
const session = await auth()
if (!session?.user) throw new Error('Unauthorized')
await db.post.create({
data: { title: String(formData.get('title')), authorId: session.user.id }
})
updateTag('posts') // No follow-up fetch needed — UI updates in same response!
}Treat every action as untrusted
Next.js provides CSRF checks, 1MB body limits, and encrypted action IDs. But application-level checks — authenticate, authorize, validate — are your responsibility.
// ✅ Every action is an untrusted entry point
// Framework provides: CSRF check, 1MB body limit, encrypted action IDs
export async function deletePost(id: string) {
// 1. Always authenticate
const session = await auth()
if (!session?.user) throw new Error('Unauthorized')
// 2. Authorize — check ownership, not just login
const post = await db.post.findUnique({ where: { id } })
if (post?.authorId !== session.user.id) throw new Error('Forbidden')
// 3. Validate inputs (id could be anything)
if (!z.string().uuid().safeParse(id).success) throw new Error('Invalid ID')
await db.post.delete({ where: { id } })
revalidatePath('/posts')
}