Back to Insights
Next.jsISRCachingSoftware

Advanced Next.js Caching: Maximizing Speed with ISR and Fine-Grained Revalidation

Chief Architect, SkillForgeJuly 18, 202611 min read

Next.js App Router relies on nested cache systems to achieve fast response times. Developers must manage these caches to ensure dynamic page components update instantly when database records change. We detail the mechanics of Next.js fetch caching.

1. The Four Caches of Next.js

Next.js integrates four separate cache layouts to optimize delivery speeds:

  • Request Memoization: Caches identical fetch requests within a single render pass.
  • Data Cache: Persists fetched records across user sessions and requests.
  • Full Route Cache: Pre-renders HTML and RSC payloads for static routes at build time.
  • Router Cache: Client-side in-memory cache that stores visited pages during navigation.

2. Fine-Grained Tag-Based Revalidation

Instead of revalidating entire pages, utilize fine-grained tags. This allows you to target specific queries when updating database entries, keeping other page elements cached. Here is a custom fetch request config:

// Fetching product details with cache tags
async function getProductDetails(productId: string) {
  const res = await fetch(`https://api.skillforge.app/products/${productId}`, {
    next: { 
      tags: [`product:${productId}`, 'products_list'],
      revalidate: 3600 // 1 hour backup expiry
    }
  });
  return res.json();
}

3. Triggering Revalidation from Webhooks

When a change occurs in your CMS or database, trigger an API route to clear the specific cache tag instantly:

import { revalidateTag } from 'next/cache';

export async function POST(request: Request) { const { productId } = await request.json(); revalidateTag(`product:${productId}`); return NextResponse.json({ revalidated: true, now: Date.now() }); } ```

4. Incremental Static Regeneration (ISR) Benefits

ISR allows you to update static pages without rebuilding the entire application. By combining tag revalidation with ISR, you serve static content instantly while ensuring updates propagate dynamically.

Have questions about this article?

Our solutions architects can help design implementations.