All articles
Web DevelopmentNovember 22, 2026 6 min read

The `use cache` Directive Looked Clean Until We Shipped It

Next.js's `use cache` directive promised a cleaner caching model than fetch options and tags. Six weeks into production, we found three failure modes that aren't in the docs — and one that cost us a weekend.

The use cache directive was supposed to be the good ending for Next.js caching. No more arguing about fetch options, no more passing { next: { tags: [...] } } down five function layers, just a top-of-file annotation and a clean mental model. We moved a mid-sized dashboard over to it in late 2025. It mostly worked. The parts that didn't work were interesting enough to write down.

What use cache actually is

If you haven't touched it yet: use cache is a function- or file-level directive, gated behind the dynamicIO experimental flag, that tells Next.js to memoize the output of a function based on its arguments and its closure. You pair it with cacheLife for TTLs and cacheTag for invalidation.

The pitch is that caching becomes a property of the function, not the data fetch. Any async work inside — a database call, a third-party API, a heavy computation — rides along for free.

import { unstable_cacheLife as cacheLife, unstable_cacheTag as cacheTag } from 'next/cache';

async function getProductSummary(productId: string) {
  'use cache';
  cacheLife('hours');
  cacheTag(`product:${productId}`);

  const [product, reviews, inventory] = await Promise.all([
    db.product.findUnique({ where: { id: productId } }),
    reviewsApi.get(productId),
    inventoryService.check(productId),
  ]);

  return { product, reviews, inventory };
}

Compared to the old unstable_cache wrapper, this reads like code a human would write. That's the appeal. It's also where the trouble starts.

Gotcha 1: Closure capture is real, and it will bite you

The cache key for a use cache function is derived from its arguments and its closed-over variables. The docs mention this. What they don't emphasize is how easy it is to close over something invisible.

Here's a version of code we shipped:

export async function ProductPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const locale = await getLocale(); // reads from headers()

  async function getPricing() {
    'use cache';
    cacheLife('minutes');
    return pricingService.get(id, locale);
  }

  return <Pricing data={await getPricing()} />;
}

Looks fine. It is not fine. getPricing is redefined on every request, and because the closure includes id and locale, the cache key does vary correctly — but the function identity doesn't, and under certain rendering conditions we saw stale entries served across locales. The reproduction was flaky enough that we only caught it after a support ticket about Canadian users seeing USD prices.

The fix is boring: hoist the cached function to module scope and pass everything as arguments.

async function getPricing(productId: string, locale: string) {
  'use cache';
  cacheLife('minutes');
  cacheTag(`pricing:${productId}`);
  return pricingService.get(productId, locale);
}

The rule we now enforce in review

If a function has 'use cache' at the top, it must be declared at module scope, and every input that affects the output must be a named parameter. No reading headers(), no reading cookies(), no reading request-scoped context inside the body. If you need those, read them in the caller and pass them in explicitly.

We added an ESLint rule for this. It catches about one violation per sprint.

Gotcha 2: Serialization is stricter than you think

use cache serializes both arguments and return values. In practice that means:

  • No Date objects as arguments (they come back as strings on cache hits in some paths)
  • No class instances in the return — Prisma models that use Decimal were a specific pain
  • No functions, obviously
  • Map and Set survive, but we stopped relying on it after one minor release changed behaviour

The sneakiest case: returning a Prisma result that includes a Decimal field. On a cold call, price.toNumber() worked. On a cache hit, price was a plain object and the method call threw. We now normalize at the cache boundary.

async function getOrder(orderId: string) {
  'use cache';
  cacheLife('hours');
  cacheTag(`order:${orderId}`);

  const order = await db.order.findUniqueOrThrow({ where: { id: orderId } });

  // Normalize before the cache boundary swallows it
  return {
    id: order.id,
    total: order.total.toNumber(),
    createdAt: order.createdAt.toISOString(),
    items: order.items.map(i => ({ ...i, price: i.price.toNumber() })),
  };
}

It's uglier. It's also debuggable six months from now, which is the only property that matters.

Gotcha 3: cacheLife defaults are not what you want

The named profiles — 'seconds', 'minutes', 'hours', 'days', 'weeks', 'max' — are convenient. They're also opinionated in ways that don't match most product requirements.

For example, 'hours' at the time of writing uses a stale window that will happily serve content that's hours out of date while revalidating in the background. For a pricing page, that's wrong. For a marketing page, it's fine. The point is: you need to know what each profile does, and the mapping can change between Next.js minor versions.

We define our own profiles in a single file and never use the string shortcuts:

// lib/cache-profiles.ts
import { unstable_cacheLife as cacheLife } from 'next/cache';

export const CACHE = {
  pricing: () => cacheLife({ stale: 30, revalidate: 60, expire: 300 }),
  catalog: () => cacheLife({ stale: 300, revalidate: 600, expire: 3600 }),
  marketing: () => cacheLife({ stale: 3600, revalidate: 7200, expire: 86400 }),
} as const;

Then in the cached function:

async function getPricing(productId: string, locale: string) {
  'use cache';
  CACHE.pricing();
  cacheTag(`pricing:${productId}`);
  return pricingService.get(productId, locale);
}

Three benefits: the TTLs are reviewable in one place, upgrading Next.js doesn't silently change behaviour, and grep works.

Gotcha 4: Tags don't propagate the way you'd hope

If function A calls function B and both have 'use cache', calling cacheTag('x') inside B does not tag A's cache entry. Each cached function is its own island.

This matters because the natural factoring — small cached helpers composed into larger cached page loaders — leaves you with invalidation that only clears the leaves. Revalidate product:123 and the inner getProductSummary recomputes, but the outer getProductPageData keeps serving its stale composition.

Our pattern now: the outermost cached function restates every tag it transitively depends on.

async function getProductPageData(productId: string, locale: string) {
  'use cache';
  CACHE.catalog();
  cacheTag(`product:${productId}`);
  cacheTag(`pricing:${productId}`);
  cacheTag(`reviews:${productId}`);

  const [summary, pricing, reviews] = await Promise.all([
    getProductSummary(productId),
    getPricing(productId, locale),
    getReviews(productId),
  ]);

  return { summary, pricing, reviews };
}

Yes, it's duplicative. Yes, it's the only way we've found to make revalidateTag behave predictably across layers.

What this did to our Core Web Vitals

The short version, framed as a range because every app is different: on the dashboard routes we migrated, TTFB on warm cache hits dropped into the sub-100ms range from a previous 300–500ms, and INP improved modestly because fewer Server Components needed to re-execute on navigation. LCP barely moved — it was never the bottleneck.

The less flattering version: during the first week post-launch, we had two incidents where stale cache entries persisted longer than expected because we'd misread the cacheLife profiles (gotcha 3). Net improvement, with a bumpy middle.

When we'd reach for use cache — and when we wouldn't

Reach for it when:

  • The function is pure in its arguments and genuinely expensive
  • You can describe its invalidation story in one sentence
  • The return value is plain JSON-ish data

Skip it when:

  • The function needs request-scoped context (auth, geo, A/B assignment)
  • The underlying data changes on a schedule you don't control
  • You're caching something that's already fast — you're adding complexity for no win

For the request-scoped cases, we still use React.cache for per-request memoization and leave use cache alone.

Where we'd start

If you're evaluating use cache for a real app in 2026, start with one route, not a sweep. Pick a page where you can articulate the invalidation rules before writing code. Hoist every cached function to module scope from day one. Define your own cacheLife profiles. Normalize return values at the cache boundary. And write down the tag propagation rules somewhere your team will actually read — a CACHING.md in the repo is fine.

The directive is good. The defaults are the sharp edge. If you want help auditing an App Router codebase before you commit, that's the kind of work our web development team does regularly — and the patterns above are what we bring to it.

#Next.js#React#Caching#Performance#App Router

Want a team like ours?

72Technologies builds production software for the kind of teams who actually read this blog.

Start a project