unstable_cache

API Reference for the unstable_cache function.

Note: This API has been replaced by use cache in Next.js 16. We recommend opting into Cache Components and replacing unstable_cache with the use cache directive.

unstable_cache allows you to cache the results of expensive operations, like database queries, and reuse them across multiple requests.

import { getUser } from './data';
import { unstable_cache } from 'next/cache';
 
const getCachedUser = unstable_cache(
  async (id) => getUser(id),
  ['my-app-user']
);
 
export default async function Component({ userID }) {
  const user = await getCachedUser(userID);
  ...
}

Good to know:

  • Accessing request data such as headers or cookies inside a cache scope is not supported. Read the data outside the cached function and pass the values you need as arguments.
  • This API uses Next.js' built-in cache to persist the result across requests and deployments. See Caching and Revalidating.

Unsupported operations

The function passed to unstable_cache cannot depend on the incoming request. Read values from cookies() or headers() before calling the cached function, then pass those values as arguments so they become part of the cache key.

The cached function also cannot:

  • Call connection(), which requires an incoming request
  • Enable or disable draft mode
  • Call revalidateTag(), updateTag(), or revalidatePath() during cache execution
  • Call a function that uses the use cache: private directive

Perform request-dependent work before calling the cached function. Perform mutations and revalidation afterward from a Server Action or Route Handler.

Parameters

const data = unstable_cache(fetchData, keyParts, options)()
  • fetchData: This is an asynchronous function that fetches the data you want to cache. It must be a function that returns a Promise.
  • keyParts: This is an extra array of keys that further adds identification to the cache. By default, unstable_cache already uses the arguments and the stringified version of your function as the cache key. It is optional in most cases; the only time you need to use it is when you use external variables without passing them as parameters. However, it is important to add closures used within the function if you do not pass them as parameters.
  • options: This is an object that controls how the cache behaves. It can contain the following properties:
    • tags: An array of tags that can be used to control cache invalidation. Next.js will not use this to uniquely identify the function.
    • revalidate: The number of seconds after which the cache should be revalidated. Omit or pass false to cache indefinitely or until matching revalidateTag() or revalidatePath() methods are called.

Returns

unstable_cache returns a function that when invoked, returns a Promise that resolves to the cached data. If the data is not in the cache, the provided function will be invoked, and its result will be cached and returned.

Example

import { unstable_cache } from 'next/cache'
 
export default async function Page({
  params,
}: {
  params: Promise<{ userId: string }>
}) {
  const { userId } = await params
  const getCachedUser = unstable_cache(
    async () => {
      return { id: userId }
    },
    [userId], // add the user ID to the cache key
    {
      tags: ['users'],
      revalidate: 60,
    }
  )
 
  //...
}

Version History

VersionChanges
v14.0.0unstable_cache introduced.

On this page