How can you implement Incremental Static Regeneration (ISR) with dynamic parameters in the new App Router of Next.js, while ensuring atomic cache invalidation and minimizing stale content for high-traffic pages? What are the trade-offs between using revalidate tags, on-demand revalidation APIs, and edge middleware for this use case? #81243
-
SummaryHow can you best use Incremental Static Regeneration (ISR) with dynamic routes in Next.js’s App Router to keep high-traffic pages fresh, while ensuring cache updates are atomic and content isn’t stale? What are the pros and cons of using revalidate tags, on-demand revalidation APIs, and edge middleware for this purpose? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
In Next.js App Router, for dynamic pages like news/[slug], you fetch data using fetch in your Server Components.
Trade-offs: Time-based (revalidate): Easy, good general freshness, but content can be stale for a fixed duration. On-Demand (revalidateTag/revalidatePath): Optimal for freshness, atomic updates. Requires backend integration to trigger, adding complexity. Edge Middleware: Runs super fast but can't directly trigger ISR revalidation or access Next.js's data cache. It's for things like routing or header manipulation, not directly for making your page content fresh via ISR. For high-traffic, dynamic pages needing minimal staleness, combine On-Demand Revalidation (triggered by data changes) with a small time-based revalidate as a safety net. This ensures speed and freshness without constant full site rebuilds. |
Beta Was this translation helpful? Give feedback.
-
By default, fetch requests in Server Components are cached on the server (“Data Cache”). On the first request, Next.js fetches data from the remote source and stores it in the Data Cache. Subsequent requests for the same resource will return the cached data, as long as it’s still fresh. If the cache is stale or missing, Next.js fetches new data and updates the cache. You can control caching per request with the cache option: cache: 'force-cache' (default): Uses the Data Cache if possible. cache: 'no-store': Always fetches fresh data and does not cache the response. Revalidation: You can set a next.revalidate option to specify how often cached data should be considered stale and refetched. This enables Incremental Static Regeneration-like behavior for server components. Client-side Router Cache: Next.js also maintains a client-side Router Cache that stores the React Server Component payloads for route segments, layouts, and loading states, improving navigation speed and user experience. This cache is session-based and cleared on page refresh; it can also be automatically invalidated after a set period. |
Beta Was this translation helpful? Give feedback.
In Next.js App Router, for dynamic pages like news/[slug], you fetch data using fetch in your Server Components.
Time-based ISR (revalidate in fetch):
Add revalidate: 3600 (1 hour) to your fetch call. Next.js serves the old page instantly, then fetches new data in the background. Pros: Simple. Cons: Content can be stale for up to an hour.
On-Demand Revalidation (revalidateTag):
This is your best friend for freshness. Tag your fetch call: { next: { tags: ['article-id-123'] } }. When your article is updated in your CMS, tell your backend to hit a special Next.js API route (a Route Handler) that calls revalidateTag('article-id-123'). Pros: Near-instant updates, atomic (full page replaced…