Next.js App Router Best Practices

Chapter 2 · Practice 3 of 6

Refresh cached data with updateTag or revalidateTag after a write

From Caching with Cache Components

Why it matters

Cached data is only useful if it refreshes when the source changes. Tag it with cacheTag, then invalidate the tag after a mutation.

UseWhenWhere it works
updateTag(tag)The user must see their own change immediately, such as right after creating a post. Expires the cache at once.Server Actions only
revalidateTag(tag, 'max')A short delay is fine, such as a CMS webhook updating a catalog. Serves stale content while fresh content loads in the background.Server Actions and Route Handlers

Prefer tags over revalidatePath, which invalidates a whole route and is easy to overuse.

Example

TypeScript
'use server';
import { updateTag } from 'next/cache';
import { redirect } from 'next/navigation';

export async function createPost(formData: FormData) {
  const post = await savePost(formData);
  updateTag('posts');
  redirect('/posts/' + post.id);
}