Caching and Revalidation
β± Estimated reading time: 2 min
Next.js provides powerful caching and revalidation mechanisms to improve performance, reduce server load, and ensure fresh data. These are especially important in the App Router where server-side rendering and static generation are heavily used.
1. Default Caching Behavior
By default, fetch() in Next.js caches requests for static generation.
-
Cached at build time for static pages.
-
Suitable for content that doesnβt change often.
2. Static Rendering with next: { revalidate }
Use Incremental Static Regeneration (ISR) to revalidate data at intervals.
-
Page regenerates in the background after specified seconds.
-
Combines the speed of static pages with freshness.
3. Dynamic Rendering (cache: 'no-store')
Fetch fresh data on every request.
-
Always returns up-to-date data.
-
Ideal for dashboards, user-specific content, or real-time apps.
4. Revalidation After a Request
-
Forces rebuild on every request but still benefits from static caching when possible.
5. Server-Side Caching Headers
Set HTTP caching headers in API responses:
-
s-maxage: Cache on CDN/edge -
stale-while-revalidate: Serve stale content while revalidating
6. Caching Strategies Summary
| Strategy | Next.js Setting | Use Case |
|---|---|---|
| Static | Default fetch | Blog, landing pages |
| ISR | next: { revalidate: seconds } | Frequently updated content |
| Dynamic | cache: 'no-store' | Dashboards, user-specific pages |
| Edge caching | Cache-Control headers | API responses, CDNs |
7. Best Practices
-
Use ISR for pages that update often but not every second.
-
Use dynamic rendering for user-specific or sensitive data.
-
Combine server-side caching headers with Next.js revalidation.
-
Minimize unnecessary re-fetching for performance.
Conclusion
Caching and revalidation in Next.js help build fast, scalable, and fresh web applications. By understanding static, ISR, and dynamic caching, you can optimize both performance and user experience.
Register Now
Share this Post
β Back to Tutorials