Загрузка…
Загрузка…
Next.js · middle · сложность 6
Opt-in Data Cache (Next 15 default off), on-demand invalidation в Server Action, segment exports для force-dynamic и ISR.
// Opt a fetch INTO the Data Cache (Next 15 default is off)
await fetch(url, { cache: 'force-cache' }); // cache indefinitely
await fetch(url, { next: { revalidate: 3600 } }); // time-based: hourly
await fetch(url, { next: { tags: ['products'] } }); // taggable for on-demand
await fetch(url, { cache: 'no-store' }); // never cache (dynamic)// On-demand invalidation — the surgical tool, e.g. inside a Server Action
'use server';
import { revalidateTag, revalidatePath } from 'next/cache';
export async function publish() {
await db.publish();
revalidateTag('products'); // purge every fetch tagged 'products'
revalidatePath('/dashboard'); // purge a specific route's Full Route Cache
}// Force a whole segment's rendering/caching behaviour
export const dynamic = 'force-dynamic'; // no Full Route Cache, render every request
export const revalidate = 60; // segment-level ISR window
export const fetchCache = 'force-cache'; // override per-fetch defaults for the segmentПаттерн: tag fetch → mutate → revalidateTag; segment revalidate + fetch tags — точный контроль четырёх cache layers.