cache-tag-outside-use-cache.mdx


title: Cannot call cacheTag() outside use cache

Why This Error Occurred

The cacheTag() function attaches one or more tags to a cache entry so you can later revalidate it with revalidateTag(). Call it from a function or component that uses the use cache or use cache: private directive. When you call cacheTag() outside a cached function, there is no cache entry to tag.

import { cacheTag } from 'next/cache'

export default async function Page() {
  cacheTag('products') // No cache entry to tag
  return <p>...</p>
}

Possible Ways to Fix It

Move cacheTag() inside the cached function whose entry you want to tag.

import { cacheTag } from 'next/cache'

async function getProducts() {
  'use cache'
  cacheTag('products') // Tags 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 cacheTag() call. To revalidate the tag later, call revalidateTag() from a Server Action or Route Handler.

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

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