use-cache-private-composition.mdx


title: 'Invalid use cache: private composition'

Why This Error Occurred

The use cache: private directive lets a cached function access request-specific data. Next.js stores its results only in the browser's memory. Private cached functions cannot run inside shared caches or without an active request. This error occurs when a private cached function is:

  • Nested inside a public use cache directive. A shared cached function can reuse its result across users, so it cannot contain a private cached function. Nest a private cached function only inside another private cached function.
  • Used inside unstable_cache(). unstable_cache() is a shared cache and has the same restriction.
  • Used without an active request. A private cached function depends on the current request, so it cannot run during build-time contexts such as generateStaticParams.
async function PrivateSegment() {
  'use cache: private'
  return <p>Private</p>
}

export default async function Page() {
  'use cache' // A public cache cannot contain a private one
  return <PrivateSegment />
}

Possible Ways to Fix It

Nest a private cached function only inside another private cached function. You can also call it directly from a component or function that runs during a request. Do not place it inside a public cached function, unstable_cache(), or a build-time context.

async function PrivateSegment() {
  'use cache: private'
  return <p>Private</p>
}

export default async function Page() {
  // No surrounding "use cache". The private segment runs per request.
  return <PrivateSegment />
}

If the data is the same for every user, use the use cache directive so Next.js can prerender and reuse the result. If you need per-user data, read it during the request instead of in generateStaticParams.

Rendered from the public repository vercel/next.js on GitHub; content belongs to its authors. Cached for up to an hour.

errors/use-cache-private-composition.mdx · vercel/next.js | Docs MD