use cache: private
The 'use cache: private' directive works just like use cache, but allows you to use runtime APIs like cookies, headers, or search params.
Good to know: Unlike
use cache, private caches are not prerendered statically as they contain personalized data that is not shared between users.
Usage
To use 'use cache: private', enable the cacheComponents flag in your next.config.ts file:
next.config.ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
}
export default nextConfigThen add 'use cache: private' to your function along with a cacheLife configuration.
Basic example
app/product/[id]/page.tsx
import { Suspense } from 'react'
import { cookies } from 'next/headers'
import { cacheLife, cacheTag } from 'next/cache'
export default async function ProductPage({
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params
return (
<div>
<ProductDetails id={id} />
<Suspense fallback={<div>Loading recommendations...</div>}>
<Recommendations productId={id} />
</Suspense>
</div>
)
}
async function Recommendations({ productId }: { productId: string }) {
const recommendations = await getRecommendations(productId)
return (
<div>
{recommendations.map((rec) => (
<ProductCard key={rec.id} product={rec} />
))}
</div>
)
}
async function getRecommendations(productId: string) {
'use cache: private'
cacheTag(`recommendations-${productId}`)
cacheLife({ stale: 60 }) // Minimum 30 seconds required for runtime prefetch
// Access cookies within private cache functions
const sessionId = (await cookies()).get('session-id')?.value || 'guest'
return getPersonalizedRecommendations(productId, sessionId)
}Request APIs allowed in private caches
The following request-specific APIs can be used inside 'use cache: private' functions:
| API | Allowed in use cache | Allowed in 'use cache: private' |
|---|---|---|
cookies() | No | Yes |
headers() | No | Yes |
searchParams | No | Yes |
connection() | No | No |
Note: The
connection()API is prohibited in bothuse cacheand'use cache: private'as it provides connection-specific information that cannot be safely cached.
Version History
| Version | Changes |
|---|---|
v16.0.0 | "use cache: private" is enabled with the Cache Components feature. |
Related
View related API references.
use cache
Learn how to use the use cache directive to cache data in your Next.js application.
cacheComponents
Learn how to enable the cacheComponents flag in Next.js.
cacheLife
Learn how to use the cacheLife function to set the cache expiration time for a cached function or component.
cacheTag
Learn how to use the cacheTag function to manage cache invalidation in your Next.js application.
Prefetching
Learn how to configure prefetching in Next.js
Was this helpful?