Demo
Private Cache
- Enable runtime prefetching of personalized content by including it in prefetch requests.
- Use
use cache: privatewhen you need to cache user-specific data that depends on cookies, headers, or search params. - Unlike regular
use cache, private caches can access request-specific APIs likecookies()andheaders()directly inside the cached function. - Private caches are never persisted to cache handlers - they're only used to mark the dynamic content as runtime prefetchable.
- Without
use cache: private, personalized content cannot be prefetched and must wait until navigation occurs.
app/private-cache/product/[id]/with-private/page.tsx
1async function getRecommendations(productId: string) {2'use cache: private';3cacheTag(`recommendations-${productId}`);4cacheLife({ stale: 60 });56// Can call cookies() INSIDE the cached function!7const sessionId = (await cookies()).get('session-id')?.value || 'guest';89return getPersonalizedRecommendations(productId, sessionId);10}1112async function Recommendations({ productId }: { productId: string }) {13// This will be runtime prefetched automatically14const recommendations = await getRecommendations(productId);1516return (17<div>18{recommendations.map((rec) => (19<ProductCard key={rec.id} product={rec} />20))}21</div>22);23}2425export const unstable_prefetch = {26mode: 'runtime',27samples: [28{ params: { id: '1' }, cookies: [{ name: 'session-id', value: '1' }] },29],30};
Demo
- Products labeled "Private Cache" link to pages that use
use cache: privateto enable runtime prefetching. The content is still dynamic, but it's prefetched when the static content of the page is also prefetched. - Products labeled "No Private Cache" link to pages that don't use
use cache: private, meaning their recommendations will be loaded after navigation. - For demo purposes, the links display visual loading states:
- Pink border with "Prefetching Private Cache..." when the runtime prefetch is in progress
- Blue border with "Prefetched Private Cache" when the runtime prefetch has completed
- Gray border for links without private cache
- With Private Cache: Personalized recommendations appear instantly (runtime prefetched).
- Without Private Cache: Recommendations show a loading skeleton, then appear after they're finished loading. The loading starts when the link is clicked.
- Click "Change Session" to get different personalized recommendations - notice how Private Cache links still runtime prefetch your new session's data.
Notes
- This demo uses the experimental
use cachedirective and describes caching behavior once stable. use cache: privateenables runtime prefetching of dynamic content by allowingcookies()andheaders()inside cached functions.- Requires
unstable_prefetchexport withmode: 'runtime'andcacheLife({ stale: 60 })(≥30s).
layout.tsx (statically inferred)
page.tsx (statically inferred)
<ProductList> (statically inferred)



