rembrembdocs

unstable_cache

Last updated April 23, 2026

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 uncached data sources such as headers or cookies inside a cache scope is not supported. If you need this data inside a cached function use headers outside of the cached function and pass the required uncached data in as an argument.
  • This API uses Next.js' built-in cache to persist the result across requests and deployments. See Caching and Revalidating.

Parameters

const data = unstable_cache(fetchData, keyParts, options)()

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

app/page.tsx

JavaScriptTypeScript

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.

Was this helpful?