Advertisement
Intermediate Next.js, On-Demand Revalidation Strategies

How do you use revalidateTag to refresh multiple related pieces of cached content at once?

Short answer

You tag your fetch requests with a specific tag name using the next options, and later call revalidateTag with that same tag name to invalidate every cached fetch result sharing that tag simultaneously, which is useful when one piece of data appears in several different places…

Save Next.js mock interview
Advertisement
AModel answer
You tag your fetch requests with a specific tag name using the next options, and later call revalidateTag with that same tag name to invalidate every cached fetch result sharing that tag simultaneously, which is useful when one piece of data appears in several different places across your site.
// Tagging a fetch request
const res = await fetch(url, { next: { tags: ['products'] } });

// Later, revalidate everything tagged 'products'
import { revalidateTag } from 'next/cache';
revalidateTag('products');
Real-world example
An online store tags every fetch related to its product catalog with a single products tag, letting it refresh the homepage, category pages, and search results all at once whenever product data changes.
Likely follow-ups
  • What is the advantage of tags over revalidating each individual path manually?
  • Can a single fetch request have multiple tags at the same time?
Related topics