Next.js & App Router (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How do `revalidatePath` and `revalidateTag` work for on-demand cache revalidation?
senior LevelMost AskedCode ExampleFormsSystem Design
In Simple Words: Instead of waiting for a timer to expire, you purge cached data immediately after a user mutation:
revalidatePath(path): Purges the Data Cache and Full Route Cache for a specific URL:typescriptrevalidatePath('/products'); // Purges /products routerevalidateTag(tag)(More Granular & Scalable): Attach a semantic tag when fetching data:When an admin updates a product, calltypescriptfetch('https://api.com/products', { next: { tags: ['products-list'] } });revalidateTag('products-list'). Every page across the site displaying products is refreshed instantly!