'use cache' unstableCache cacheComponents and their relationship with full route cache and on-demand ISR #82565
Replies: 1 comment 2 replies
-
Hi @d-plz Short answer: that’s expected today. What to use right nowIf your goal is: “generate /[slug] statically, cache it at both data and full-route level, and invalidate on-demand via tags/path,” prefer the stable path: 1. Keep the route static: // app/[slug]/page.tsx
export const dynamic = 'force-static'; // or omit and ensure everything is static
export const revalidate = false; // use on-demand instead of time-based ISR 2. Use fetch with cache tags: const data = await fetch(url, { next: { tags: ['post', slug] } }).then(r => r.json()); 3. In a server action or API route, trigger invalidation: import { revalidateTag, revalidatePath } from 'next/cache';
export async function invalidatePost(slug: string) {
revalidateTag('post'); // data cache
revalidatePath(`/${slug}`); // full route cache
} This combo keeps the page truly static/ISR’d at the edge and lets you refresh both the data cache and the pre-rendered HTML on demand. When to try use cacheuse cache is useful for memoizing expensive computations and composing caches inside components/functions, but it currently implies dynamic = 'auto'. Until it matures, don’t rely on it for ISR-style, CDN-level full-route caching. Bottom line: For on-demand ISR with tag/path revalidation of both data and HTML, stick to fetch caching + revalidateTag/revalidatePath and a static route. Avoid use cache for this specific use case for now. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I wanted to use the unstable 'use cache' directives and all the experimental caching for components/functions as I understood it would work the same way that the fetch-extended caching works with the full route cache.
But it seems the unstable directives force
export const dynamic = 'auto'
instead of allowingexport const dynamic = 'force-static
the latter being necessary for on-demand generated ISR pages to be "truly" cached both at the function/data level and at the CDN/route level.Is this expected and normal? or over time we can expect the 'use cache' to synchronize with the full route cache?
The goal here being simple: having the same behavior fetch-caching has with on-demand ISR. Dynamically generating /[slug]/home and caching it both the data and the full route level while invalidating it via tags/path as needed.
Beta Was this translation helpful? Give feedback.
All reactions