Data Fetching, Caching & Revalidation Strategies
Next.js extends the native fetch API to support fine-grained caching and revalidation controls on the server. By default, fetch requests inside Server Components can be configured for static caching ({ cache: 'force-cache' }), dynamic execution ({ cache: 'no-store' }), or time-based incremental revalidation ({ next: { revalidate: 60 } }).
For on-demand cache invalidation, Next.js provides revalidatePath('/blog/[slug]') and revalidateTag('posts'). When an admin updates a blog post, calling revalidateTag('posts') invalidates the server cache instantly without waiting for a TTL timeout, ensuring users see fresh data immediately.
Exercise
Implement a Server Component data fetch with { next: { tags: ['products'] } } and trigger a revalidateTag('products') call inside a mutation handler.
Check your understanding
What is the difference between revalidatePath and revalidateTag?Show answerHide answer
Answer
revalidatePath purges cached pages for a specific URL route, whereas revalidateTag purges cached data across multiple routes sharing the same cache tag.How do you disable caching for a specific fetch request in Next.js?Show answerHide answer
Answer
Pass { cache: 'no-store' } in the fetch options object.