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.
Next.js integrates four separate cache layouts to optimize delivery speeds:
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();
}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() }); } ```
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.
An extensive roadmap of twenty innovative ECE project concepts spanning IoT, TinyML, biomedical engineering, and automotive systems.
An exhaustive guide to building advanced Internet of Things prototypes using the dual-core ESP32 chip with built-in Wi-Fi and Bluetooth.