Skip to solution
mediumFrontend

How do you revalidate data after a mutation in Next.js?

887 views
01

Understand the problem

Refreshing caches once a Server Action changes data.

nextjsrevalidationserver-actionsmutations
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

After a Server Action mutates data, call revalidatePath() or revalidateTag() to invalidate the affected caches, or router.refresh() on the client, so the UI reflects the change. Redirecting with redirect() also re-renders the target route fresh.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Mutate then revalidate + redirect
'use server';
import { revalidateTag, revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';

export async function createTodo(data) {
  const todo = await db.todo.create({ data });
  revalidateTag('todos');            // refresh anything tagged 'todos'
  revalidatePath('/todos');          // and this specific route
  redirect('/todos/' + todo.id);     // navigate to the fresh page
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 32 of 95 decoded in the Next.js track. One more won't hurt.

Back to track