Next.js 16 — cacheLife · cacheTag · Stable API

use cache Directive

Cache async functions with 'use cache', assign freshness profiles with cacheLife(), and enable targeted invalidation with cacheTag(). Previously unstable_ — now fully stable in Next.js 16.

Next.js 16cacheLifecacheTagStableDocs ↗
Live Demo — Cached Server Components

Real caching in action

These components run on the server and cache their results. The timestamp below is cached for ~1 second. The dashboard stats are cached for hours — the fetchedAt value shows when the data was last fetched.

⏳ ...
⏳ ...
Basic Usage

'use cache' + cacheLife + cacheTag

Add 'use cache' as the first line of any async function. The result is cached in memory. Call cacheLife() to set a freshness profile and cacheTag() to enable targeted invalidation.

lib/data.tsts
// 'use cache' — Next.js 16 stable (no unstable_ prefix needed)
// Previously: unstable_cacheLife, unstable_cacheTag
// Now:        cacheLife, cacheTag

import { cacheLife, cacheTag } from 'next/cache'

async function getPosts() {
  'use cache'                       // ← cache this function's result
  cacheLife('hours')                // stale: 1h, revalidate: 4h, expire: 8h
  cacheTag('posts')                 // tag for targeted invalidation

  const res = await fetch('https://api.example.com/posts')
  return res.json()
}
cacheLife Profiles

Built-in profiles & custom config

lib/slow-resource.tsts
// Built-in cacheLife profiles:
//   'seconds' → stale: 0,   revalidate: 1,    expire: 60
//   'minutes' → stale: 60,  revalidate: 300,  expire: 3600
//   'hours'   → stale: 3600, revalidate: 14400, expire: 86400
//   'days'    → stale: 86400, revalidate: 604800, expire: 2592000
//   'weeks'   → stale: 604800, revalidate: 2592000, expire: Infinity
//   'max'     → stale: ∞,   revalidate: ∞,    expire: ∞

// Custom profile:
async function getSlowResource() {
  'use cache'
  cacheLife({
    stale: 60,        // serve stale for 1 min while revalidating
    revalidate: 300,  // background revalidation every 5 min
    expire: 3600,     // hard expire after 1 hour
  })
  cacheTag('my-resource')
  return heavyDataFetch()
}
Cache Invalidation

Tag-based invalidation

app/actions.tsts
// Tag-based invalidation in Server Actions
'use server'

import { revalidateTag, updateTag } from 'next/cache'

// revalidateTag — stale-while-revalidate (background)
export async function publishPost(id: string) {
  await db.posts.publish(id)
  revalidateTag('posts', 'max')    // background — users see stale briefly
}

// updateTag — read-your-writes (immediate)
export async function updateMyProfile(data: Profile) {
  await db.users.update(data)
  updateTag('my-profile')          // immediate — user sees change now
}
Cache Variants

'use cache: private' and 'use cache: remote'

When a function reads cookies() or headers(), use 'use cache: private' to cache in the browser only. For serverless deployments where in-memory cache doesn't persist, use 'use cache: remote'.

lib/personalized.tsts
// 'use cache: private' — Next.js 16
// Cache functions that read cookies() or headers()
// Result is cached in the BROWSER only, not on the server

async function getPersonalizedContent() {
  'use cache: private'          // ← variant for session-specific data
  cacheLife('minutes')
  const session = await cookies()
  return fetchDashboard(session.get('userId'))
}

// Also available:
//   'use cache: remote' — persistent across serverless instances
//   'use cache'         — in-memory, resets on restart (default)
← Back to all demos