Next.js Developer interview question · Most asked

How do `revalidatePath` and `revalidateTag` work?

Summary

revalidatePath('/posts') invalidates cached data and rendered output for a path; revalidateTag('posts') invalidates all cached fetches/entries tagged posts. Both are called from Server Actions or Route Handlers after mutations or webhooks.

Longer Answer

Tag-based invalidation is more precise: tag the fetch (next: { tags: ['post:42'] }) and invalidate exactly what changed. On-demand revalidation via a webhook (e.g., from a CMS) keeps static pages fresh without short time-based intervals. Recent versions also add updateTag for read-your-own-writes semantics within Server Actions.

Key Points

  • Path-based vs tag-based invalidation
  • Call after mutations or CMS webhooks
  • Tag fetches precisely
  • Combine with time-based revalidate

More most-asked Next.js Developer questions

← All Next.js Developer questions