#When to use revalidateTag vs revalidatePath?
1 messages · Page 1 of 1 (latest)
Hi @blazing tundra. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
revalidateTag and revalidatePath are both methods provided by Next.js for on-demand revalidation of data. The choice between the two depends on the specific use case and how your data is cached.
revalidateTag is used to revalidate data associated with a specific cache tag. This is useful for scenarios where you want to update your cached data without waiting for a revalidation period to expire. You can tag cache entries when using fetch or unstable_cache, and then call revalidateTag to purge the cache entries associated with that tag. This method is particularly useful when you want to revalidate groups of data at once.
Example usage of revalidateTag:
// Cache data with a tag
fetch(`<https://...`,> { next: { tags: ['a', 'b', 'c'] } })
// Revalidate entries with a specific tag
revalidateTag('a')
Source (https://nextjs.org/docs/app/building-your-application/caching)
revalidatePath allows you to manually revalidate data and re-render the route segments below a specific path in a single operation. This is useful when you want to revalidate data associated with a specific filesystem path.
Example usage of revalidatePath:
revalidatePath('/')
Source (https://nextjs.org/docs/app/building-your-application/caching)
Both revalidateTag and revalidatePath can be used in Route Handlers (https://nextjs.org/docs/app/building-your-application/routing/route-handlers) to revalidate data in response to a third party event (e.g. webhook), or in Server Actions (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions) to revalidate data after a user interaction (e.g. form submission).