Revalidation: revalidatePath and revalidateTag
What you'll learn: How to invalidate cached data and trigger fresh fetches after mutating data in Server Actions.
The Problem Server Actions Solve
When you mutate data in a Server Action (like creating a todo or updating a user profile), Next.js often caches the result of your Server Components. Without revalidation, users might see stale data even after making changes. You need a way to tell Next.js: "This data changed—go fetch it again."
Two Revalidation Tools
Next.js provides two functions to invalidate cached data:
revalidatePath()
Invalidates all cached data for a specific route or path pattern:
'use server'
import { revalidatePath } from 'next/cache'
export async function createTodo(formData: FormData) {
const title = formData.get('title')
// Save to database
await db.todos.create({ title })
// Invalidate the /todos page so it refetches
revalidatePath('/todos')
}
Think of this as clearing the cache for an entire page.
revalidateTag()
Invalidates cached data tagged with a specific identifier—more granular control:
'use server'
import { revalidateTag } from 'next/cache'
export async function updateUser(userId: string, name: string) {
await db.users.update(userId, { name })
// Only invalidate data tagged with 'user-profile'
revalidateTag('user-profile')
}
This works with fetch calls or database queries that you've tagged with next: { tags: ['user-profile'] }.
When to Use Each
- revalidatePath: When mutations affect an entire page or section
- revalidateTag: When you want surgical precision—invalidate only specific queries
Key Takeaway: Use revalidatePath() or revalidateTag() inside Server Actions to bust the cache and ensure users see fresh data after mutations, without manual refetching logic.