# On-Demand Revalidation (revalidateTag)

> Purge cache entries across multiple routes simultaneously using tags.

Subject: Next.js | Lesson 28 of 65 | 6 min read
Interactive lesson: https://codewithanimation.in/nextjs/revalidate-tag
Previous: [On-Demand Revalidation (revalidatePath)](https://codewithanimation.in/nextjs/revalidate-path.md) | Next: [Dynamic Rendering (Opting Out)](https://codewithanimation.in/nextjs/dynamic-rendering.md)

## Tag-Based Caching

`revalidateTag` is a powerful Next.js utility that allows you to purge data from the cache based on custom strings (tags) instead of URL paths.

You attach these tags to your `fetch` requests, grouping related data together regardless of what page it lives on.

## The Multi-Route Problem

Imagine an e-commerce site where a product's price is displayed on the Homepage, the Category page, and the Product Details page.

If you change the price, using `revalidatePath` would require you to manually list every single URL where that product appears. With `revalidateTag`, you just tag those fetches with `'prices'`, and a single `revalidateTag('prices')` call purges all of them instantly across the entire application.

## Tagging and Purging

Step 1: Tag your fetch request: `fetch('https://api.store.com/shoes', { next: { tags: ['shoes', 'catalog'] } })`.

Step 2: After updating a shoe in the database, call `revalidateTag('shoes')` inside your Server Action. Every cache entry across the app containing that tag is immediately flushed.

## Common mistake: Silent Typo Failures

Tags are just plain strings. If you tag your fetch with `'products'` but call `revalidateTag('product')` in your server action, nothing happens.

Next.js will not throw an error. It simply fails to find the tag and leaves the stale data in the cache. Always use constants or enums for your cache tags to prevent typos!

## Tip: Tags Transcend Routes

Remember that cache tags are bound to the `fetch` request, not the page URL.

This makes tags incredibly scalable for complex applications where data is reused across many different layouts and components.

## Try it: The Tag Network

Interact with the Server Action below. Notice how calling `revalidateTag('products')` instantly identifies and purges the cached data across completely different URL routes simultaneously.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/revalidate-tag

## Check yourself

### Question 1

Why would you choose `revalidateTag` over `revalidatePath`?

- A. Because revalidateTag is faster
- B. Because the data you want to purge appears on multiple different URL routes
- C. Because revalidateTag works on the client side
- D. Because revalidatePath cannot clear dynamic routes

Correct answer: B

`revalidateTag` is ideal when the same data is scattered across multiple routes. It allows you to purge all instances with a single command without tracking URLs.

- Why not A: They use the same underlying cache invalidation mechanism.
- Why not C: Both functions are Server-only utilities.
- Why not D: revalidatePath can clear dynamic routes using the 'layout' option.

### Question 2

What happens if you call `revalidateTag('user-profile')` but the fetch was tagged as `'user_profile'`?

- A. Next.js throws a runtime error
- B. The cache is silently NOT cleared, leaving stale data
- C. Next.js fuzzy-matches the string and clears the cache anyway
- D. The entire cache is cleared as a fallback

Correct answer: B

Tag mismatches fail silently. This is a common source of bugs where developers assume their cache is clearing, but a typo prevents it.

- Why not A: Tags fail silently.
- Why not C: Tags require exact string matching.
- Why not D: That would be a severe performance issue.

## Remember this

- `revalidateTag` clears data based on custom tags, not URLs.
- Add tags via `fetch(url, { next: { tags: ['my-tag'] } })`.
- A single `revalidateTag` call can purge data across multiple pages simultaneously.
- Tags must match exactly; typos will silently fail to clear the cache.
- Always use a constants file for your tag names to prevent typos.
