Skip to content

Latest commit

 

History

History
44 lines (32 loc) · 1.39 KB

File metadata and controls

44 lines (32 loc) · 1.39 KB
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.

Useful Links