Menu

Cache Components and 'use cache'

Lesson 2: Cache Components and 'use cache'

What are Cache Components?

Cache Components is the caching model introduced in Next.js 16. Enable it with one flag:

// next.config.ts
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  cacheComponents: true,
};

export default nextConfig;

With it enabled:

  • Dynamic code runs at request time by default. Nothing is cached implicitly.
  • You mark what can be cached with the 'use cache' directive.
  • Data that is neither cached nor wrapped in <Suspense> causes an error, so slow, uncached work can never silently block a page.

Where can 'use cache' go?

Level Syntax Caches
Function First line inside an async function The function's return value
Component First line inside an async component The component's rendered output
File First line of the file Every exported function (all must be async)

Example

// lib/data/courses.ts
import { cacheLife, cacheTag } from 'next/cache';

export async function getCourseCatalogue() {
  'use cache';
  cacheLife('hours');
  cacheTag('courses');

  return db.course.findMany({ where: { published: true } });
}
// app/courses/page.tsx
export default async function CoursesPage() {
  const courses = await getCourseCatalogue(); // Served from cache after the first call
  return <CourseGrid courses={courses} />;
}

How it works

  • The cache key is built from the build ID, the function's identity, and its serialised arguments. getCourse('sql') and getCourse('python') are cached separately.
  • Arguments and return values must be serialisable (plain objects, arrays, dates, primitives).
  • Cached functions cannot call cookies(), headers(), or read searchParams. Read those outside and pass the values in as arguments.
  • children and other JSX passed into a cached component pass through without becoming part of its cache entry.

Passing request data into a cache:

import { cookies } from 'next/headers';

async function Recommendations() {
  const level = (await cookies()).get('level')?.value ?? 'beginner';
  return <CachedRecommendations level={level} />; // level becomes part of the key
}

async function CachedRecommendations({ level }: { level: string }) {
  'use cache';
  cacheLife('hours');
  const courses = await getCoursesForLevel(level);
  return <CourseGrid courses={courses} />;
}

Common mistakes

Mistake Effect Fix
Calling cookies() inside a 'use cache' function Error Read it outside and pass the value as an argument
Caching per-user data with a shared key One user sees another's data Include the user ID in the arguments, or do not cache it
Passing a class instance (e.g. an ORM model) as an argument Serialisation error Pass IDs or plain objects
Using 'use cache' in a non-async function Error Cached functions and components must be async

Key takeaways

  • With Cache Components, everything is fresh unless you add 'use cache'.
  • Cache at the level that matches the data: function, component, or whole file.
  • Request data goes in as arguments, never read inside the cache.

Try it: Enable cacheComponents, cache getCourseCatalogue with 'use cache', and log a message inside it to confirm it runs only once across several page loads (in production mode).


Nextjs Tutorial

J

Jebasta





Get in Touch For Details