use cache: private

Learn how to use the `"use cache: private"` directive to enable runtime prefetching of personalized content in your Next.js application.

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:

import type { NextConfig } from 'next'
 
const nextConfig: NextConfig = {
  cacheComponents: true,
}
 
export default nextConfig

Then add 'use cache: private' to your function along with a cacheLife configuration.

Basic example

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:

APIAllowed in use cacheAllowed in 'use cache: private'
cookies()NoYes
headers()NoYes
searchParamsNoYes
connection()NoNo

Note: The connection() API is prohibited in both use cache and 'use cache: private' as it provides connection-specific information that cannot be safely cached.

Version History

VersionChanges
v16.0.0"use cache: private" is enabled with the Cache Components feature.

On this page