cache-life-outside-use-cache.mdx


title: Cannot call cacheLife() outside use cache

Why This Error Occurred

The cacheLife() function configures the stale, revalidate, and expire times for a cache entry. Call it from a function or component that uses the use cache or use cache: private directive. When you call cacheLife() outside a cached function, there is no cache entry to configure.

import { cacheLife } from 'next/cache'

export default async function Page() {
  cacheLife('hours') // No cache entry to configure
  return <p>...</p>
}

Possible Ways to Fix It

Move cacheLife() inside the cached function whose lifetime you want to set.

import { cacheLife } from 'next/cache'

async function getProducts() {
  'use cache'
  cacheLife('hours') // Configures this cache entry
  return db.products.findMany()
}

export default async function Page() {
  const products = await getProducts()
  return <p>...</p>
}

If you don't intend to cache the function or component, remove the cacheLife() call.

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

errors/cache-life-outside-use-cache.mdx · vercel/next.js | Docs MD