Menu

Refreshing Data After Mutations

Lesson 4: Refreshing Data After Mutations

Why refresh after a mutation?

After an action changes data, any cached or already-rendered page that shows that data is out of date. You tell Next.js what to refresh.

API Import from What it does Use when
revalidatePath(path) next/cache Marks a route's data and render as stale A specific page shows the changed data
updateTag(tag) next/cache Expires tagged cache entries and reads fresh data in the same request (Server Actions only) The user must see their own change immediately
revalidateTag(tag, profile) next/cache Marks tagged entries stale; serves old data while refreshing in the background Content where a short delay is fine
refresh() next/cache Re-renders uncached data on the page without touching the cache (Server Actions only) Counters, notifications, live status
redirect(url) next/navigation Sends the user to another page After creating something, go to its page

Tags are attached with cacheTag() inside 'use cache' functions (Module 7).

Example

'use server';

import { updateTag, revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';

export async function updateCourse(courseId: string, formData: FormData) {
  const session = await requireInstructor();
  const data = CourseSchema.parse(Object.fromEntries(formData));

  const course = await db.course.update({ where: { id: courseId }, data });

  updateTag(`course-${courseId}`);   // Instructor sees the edit immediately
  revalidatePath('/courses');        // Course list refreshes too
  redirect(`/courses/${course.slug}`);
}

How it works

  1. The action updates the database.
  2. updateTag expires every cache entry tagged course-<id>, so the next read is fresh.
  3. revalidatePath marks the list page stale.
  4. redirect sends the user to the course page, which renders with the new data.

**updateTag** versus **revalidateTag**:

updateTag revalidateTag
Where Server Actions only Server Actions and Route Handlers
Freshness Read-your-own-writes: fresh in this request Stale-while-revalidate: old data served once, then refreshed
Typical use Forms, settings, edits by the current user Webhooks from a CMS, bulk updates
Signature in Next.js 16 updateTag('posts') revalidateTag('posts', 'max')

Common mistakes

Mistake Effect Fix
Forgetting to revalidate User saves, but the page shows old data Call the right API after every write
Calling revalidateTag with one argument Deprecated in Next.js 16 Pass a profile, e.g. 'max', or use updateTag
redirect() inside try/catch Redirect swallowed as an error Call it after the try/catch block
Revalidating / with 'layout' after every small change Whole site re-renders Target the specific path or tag

Key takeaways

  • Every mutation must be followed by the right refresh.
  • Use updateTag for instant feedback to the user who made the change, revalidateTag for background refreshes, and refresh for uncached data.

Try it: After a review is added, revalidate the course page and the instructor dashboard, and confirm both show the new review.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details