Cache Components
Master Next.js caching with intelligent component-level cache control
✨ The solution you've been looking for
Expert guidance for Next.js Cache Components and Partial Prerendering (PPR).**PROACTIVE ACTIVATION**: Use this skill automatically when working in Next.js projects that have `cacheComponents: true` in their next.config.ts/next.config.js. When this config is detected, proactively apply Cache Components patterns and best practices to all React Server Component implementations.**DETECTION**: At the start of a session in a Next.js project, check for `cacheComponents: true` in next.config. If enabled, this skill's patterns should guide all component authoring, data fetching, and caching decisions.**USE CASES**: Implementing 'use cache' directive, configuring cache lifetimes with cacheLife(), tagging cached data with cacheTag(), invalidating caches with updateTag()/revalidateTag(), optimizing static vs dynamic content boundaries, debugging cache issues, and reviewing Cache Component implementations.
See It In Action
Interactive preview & real-world examples
AI Conversation Simulator
See how users interact with this skill
User Prompt
Help me optimize my Next.js blog using Cache Components. I want the post content cached but comments to be dynamic per user.
Skill Processing
Analyzing request...
Agent Response
Implementation of 'use cache' for posts with proper tagging, Suspense boundaries for comments, and cache invalidation strategies
Quick Start (3 Steps)
Get up and running in minutes
Install
claude-code skill install cache-components
claude-code skill install cache-componentsConfig
First Trigger
@cache-components helpCommands
| Command | Description | Required Args |
|---|---|---|
| @cache-components optimize-blog-performance-with-ppr | Transform a traditional blog into a high-performance site using static shells with cached content and dynamic user features | None |
| @cache-components e-commerce-product-pages | Build fast product pages that mix static layouts, cached product data, and dynamic user-specific content like cart status | None |
| @cache-components debug-cache-performance-issues | Identify and resolve common caching problems like cache misses, improper invalidation, or build-time errors | None |
Typical Use Cases
Optimize Blog Performance with PPR
Transform a traditional blog into a high-performance site using static shells with cached content and dynamic user features
E-commerce Product Pages
Build fast product pages that mix static layouts, cached product data, and dynamic user-specific content like cart status
Debug Cache Performance Issues
Identify and resolve common caching problems like cache misses, improper invalidation, or build-time errors
Overview
Next.js Cache Components
Auto-activation: This skill activates automatically in projects with
cacheComponents: truein next.config.
Project Detection
When starting work in a Next.js project, check if Cache Components are enabled:
1# Check next.config.ts or next.config.js for cacheComponents
2grep -r "cacheComponents" next.config.* 2>/dev/null
If cacheComponents: true is found, apply this skill’s patterns proactively when:
- Writing React Server Components
- Implementing data fetching
- Creating Server Actions with mutations
- Optimizing page performance
- Reviewing existing component code
Cache Components enable Partial Prerendering (PPR) - mixing static HTML shells with dynamic streaming content for optimal performance.
Philosophy: Code Over Configuration
Cache Components represents a shift from segment configuration to compositional code:
| Before (Deprecated) | After (Cache Components) |
|---|---|
export const revalidate = 3600 | cacheLife('hours') inside 'use cache' |
export const dynamic = 'force-static' | Use 'use cache' and Suspense boundaries |
| All-or-nothing static/dynamic | Granular: static shell + cached + dynamic |
Key Principle: Components co-locate their caching, not just their data. Next.js provides build-time feedback to guide you toward optimal patterns.
Core Concept
┌─────────────────────────────────────────────────────┐
│ Static Shell │
│ (Sent immediately to browser) │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Header │ │ Cached │ │ Suspense │ │
│ │ (static) │ │ Content │ │ Fallback │ │
│ └─────────────┘ └─────────────┘ └──────┬──────┘ │
│ │ │
│ ┌──────▼──────┐ │
│ │ Dynamic │ │
│ │ (streams) │ │
│ └─────────────┘ │
└─────────────────────────────────────────────────────┘
Mental Model: The Caching Decision Tree
When writing a React Server Component, ask these questions in order:
┌─────────────────────────────────────────────────────────┐
│ Does this component fetch data or perform I/O? │
└─────────────────────┬───────────────────────────────────┘
│
┌──────────▼──────────┐
│ YES │ NO → Pure component, no action needed
└──────────┬──────────┘
│
┌─────────────────▼─────────────────┐
│ Does it depend on request context? │
│ (cookies, headers, searchParams) │
└─────────────────┬─────────────────┘
│
┌────────────┴────────────┐
│ │
┌────▼────┐ ┌─────▼─────┐
│ YES │ │ NO │
└────┬────┘ └─────┬─────┘
│ │
│ ┌─────▼─────────────────┐
│ │ Can this be cached? │
│ │ (same for all users?) │
│ └─────┬─────────────────┘
│ │
│ ┌──────────┴──────────┐
│ │ │
│ ┌────▼────┐ ┌─────▼─────┐
│ │ YES │ │ NO │
│ └────┬────┘ └─────┬─────┘
│ │ │
│ ▼ │
│ 'use cache' │
│ + cacheTag() │
│ + cacheLife() │
│ │
└──────────────┬─────────────────────┘
│
▼
Wrap in <Suspense>
(dynamic streaming)
Key insight: The 'use cache' directive is for data that’s the same across users. User-specific data stays dynamic with Suspense.
Quick Start
Enable Cache Components
1// next.config.ts
2import type { NextConfig } from 'next'
3
4const nextConfig: NextConfig = {
5 cacheComponents: true,
6}
7
8export default nextConfig
Basic Usage
1// Cached component - output included in static shell
2async function CachedPosts() {
3 'use cache'
4 const posts = await db.posts.findMany()
5 return <PostList posts={posts} />
6}
7
8// Page with static + cached + dynamic content
9export default async function BlogPage() {
10 return (
11 <>
12 <Header /> {/* Static */}
13 <CachedPosts /> {/* Cached */}
14 <Suspense fallback={<Skeleton />}>
15 <DynamicComments /> {/* Dynamic - streams */}
16 </Suspense>
17 </>
18 )
19}
Core APIs
1. 'use cache' Directive
Marks code as cacheable. Can be applied at three levels:
1// File-level: All exports are cached
2'use cache'
3export async function getData() {
4 /* ... */
5}
6export async function Component() {
7 /* ... */
8}
9
10// Component-level
11async function UserCard({ id }: { id: string }) {
12 'use cache'
13 const user = await fetchUser(id)
14 return <Card>{user.name}</Card>
15}
16
17// Function-level
18async function fetchWithCache(url: string) {
19 'use cache'
20 return fetch(url).then((r) => r.json())
21}
Important: All cached functions must be async.
2. cacheLife() - Control Cache Duration
1import { cacheLife } from 'next/cache'
2
3async function Posts() {
4 'use cache'
5 cacheLife('hours') // Use a predefined profile
6
7 // Or custom configuration:
8 cacheLife({
9 stale: 60, // 1 min - client cache validity
10 revalidate: 3600, // 1 hr - start background refresh
11 expire: 86400, // 1 day - absolute expiration
12 })
13
14 return await db.posts.findMany()
15}
Predefined profiles: 'default', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'max'
3. cacheTag() - Tag for Invalidation
1import { cacheTag } from 'next/cache'
2
3async function BlogPosts() {
4 'use cache'
5 cacheTag('posts')
6 cacheLife('days')
7
8 return await db.posts.findMany()
9}
10
11async function UserProfile({ userId }: { userId: string }) {
12 'use cache'
13 cacheTag('users', `user-${userId}`) // Multiple tags
14
15 return await db.users.findUnique({ where: { id: userId } })
16}
4. updateTag() - Immediate Invalidation
For read-your-own-writes semantics:
1'use server'
2import { updateTag } from 'next/cache'
3
4export async function createPost(formData: FormData) {
5 await db.posts.create({ data: formData })
6
7 updateTag('posts') // Client immediately sees fresh data
8}
5. revalidateTag() - Background Revalidation
For stale-while-revalidate pattern:
1'use server'
2import { revalidateTag } from 'next/cache'
3
4export async function updatePost(id: string, data: FormData) {
5 await db.posts.update({ where: { id }, data })
6
7 revalidateTag('posts', 'max') // Serve stale, refresh in background
8}
When to Use Each Pattern
| Content Type | API | Behavior |
|---|---|---|
| Static | No directive | Rendered at build time |
| Cached | 'use cache' | Included in static shell, revalidates |
| Dynamic | Inside <Suspense> | Streams at request time |
Parameter Permutations & Subshells
Critical Concept: With Cache Components, Next.js renders ALL permutations of provided parameters to create reusable subshells.
1// app/products/[category]/[slug]/page.tsx
2export async function generateStaticParams() {
3 return [
4 { category: 'jackets', slug: 'classic-bomber' },
5 { category: 'jackets', slug: 'essential-windbreaker' },
6 { category: 'accessories', slug: 'thermal-fleece-gloves' },
7 ]
8}
Next.js renders these routes:
/products/jackets/classic-bomber ← Full params (complete page)
/products/jackets/essential-windbreaker ← Full params (complete page)
/products/accessories/thermal-fleece-gloves ← Full params (complete page)
/products/jackets/[slug] ← Partial params (category subshell)
/products/accessories/[slug] ← Partial params (category subshell)
/products/[category]/[slug] ← No params (fallback shell)
Why this matters: The category subshell (/products/jackets/[slug]) can be reused for ANY jacket product, even ones not in generateStaticParams. Users navigating to an unlisted jacket get the cached category shell immediately, with product details streaming in.
generateStaticParams Requirements
With Cache Components enabled:
- Must provide at least one parameter - Empty arrays now cause build errors (prevents silent production failures)
- Params prove static safety - Providing params lets Next.js verify no dynamic APIs are called
- Partial params create subshells - Each unique permutation generates a reusable shell
1// ❌ ERROR with Cache Components
2export function generateStaticParams() {
3 return [] // Build error: must provide at least one param
4}
5
6// ✅ CORRECT: Provide real params
7export async function generateStaticParams() {
8 const products = await getPopularProducts()
9 return products.map(({ category, slug }) => ({ category, slug }))
10}
Cache Key = Arguments
Arguments become part of the cache key:
1// Different userId = different cache entry
2async function UserData({ userId }: { userId: string }) {
3 'use cache'
4 cacheTag(`user-${userId}`)
5
6 return await fetchUser(userId)
7}
Build-Time Feedback
Cache Components provides early feedback during development. These build errors guide you toward optimal patterns:
Error: Dynamic data outside Suspense
Error: Accessing cookies/headers/searchParams outside a Suspense boundary
Solution: Wrap dynamic components in <Suspense>:
1<Suspense fallback={<Skeleton />}>
2 <ComponentThatUsesCookies />
3</Suspense>
Error: Uncached data outside Suspense
Error: Accessing uncached data outside Suspense
Solution: Either cache the data or wrap in Suspense:
1// Option 1: Cache it
2async function ProductData({ id }: { id: string }) {
3 'use cache'
4 return await db.products.findUnique({ where: { id } })
5}
6
7// Option 2: Make it dynamic with Suspense
8;<Suspense fallback={<Loading />}>
9 <DynamicProductData id={id} />
10</Suspense>
Error: Request data inside cache
Error: Cannot access cookies/headers inside 'use cache'
Solution: Extract runtime data outside cache boundary (see “Handling Runtime Data” above).
Additional Resources
- For complete API reference, see REFERENCE.md
- For common patterns and recipes, see PATTERNS.md
- For debugging and troubleshooting, see TROUBLESHOOTING.md
Code Generation Guidelines
When generating Cache Component code:
- Always use
async- All cached functions must be async - Place
'use cache'first - Must be first statement in function body - Call
cacheLife()early - Should follow'use cache'directive - Tag meaningfully - Use semantic tags that match your invalidation needs
- Extract runtime data - Move
cookies()/headers()outside cached scope - Wrap dynamic content - Use
<Suspense>for non-cached async components
Proactive Application (When Cache Components Enabled)
When cacheComponents: true is detected in the project, automatically apply these patterns:
When Writing Data Fetching Components
Ask yourself: “Can this data be cached?” If yes, add 'use cache':
1// Before: Uncached fetch
2async function ProductList() {
3 const products = await db.products.findMany()
4 return <Grid products={products} />
5}
6
7// After: With caching
8async function ProductList() {
9 'use cache'
10 cacheTag('products')
11 cacheLife('hours')
12
13 const products = await db.products.findMany()
14 return <Grid products={products} />
15}
When Writing Server Actions
Always invalidate relevant caches after mutations:
1'use server'
2import { updateTag } from 'next/cache'
3
4export async function createProduct(data: FormData) {
5 await db.products.create({ data })
6 updateTag('products') // Don't forget!
7}
When Composing Pages
Structure with static shell + cached content + dynamic streaming:
1export default async function Page() {
2 return (
3 <>
4 <StaticHeader /> {/* No cache needed */}
5 <CachedContent /> {/* 'use cache' */}
6 <Suspense fallback={<Skeleton />}>
7 <DynamicUserContent /> {/* Streams at runtime */}
8 </Suspense>
9 </>
10 )
11}
When Reviewing Code
Flag these issues in Cache Components projects:
- Data fetching without
'use cache'where caching would benefit - Missing
cacheTag()calls (makes invalidation impossible) - Missing
cacheLife()(relies on defaults which may not be appropriate) - Server Actions without
updateTag()/revalidateTag()after mutations -
cookies()/headers()called inside'use cache'scope - Dynamic components without
<Suspense>boundaries - DEPRECATED:
export const revalidate- replace withcacheLife()in'use cache' - DEPRECATED:
export const dynamic- replace with Suspense + cache boundaries - Empty
generateStaticParams()return - must provide at least one param
What Users Are Saying
Real feedback from the community
Environment Matrix
Dependencies
Framework Support
Context Window
Security & Privacy
Information
- Author
- vercel
- Updated
- 2026-01-30
- Category
- full-stack
Related Skills
Cache Components
Expert guidance for Next.js Cache Components and Partial Prerendering (PPR).**PROACTIVE …
View Details →Nextjs App Router Patterns
Master Next.js 14+ App Router with Server Components, streaming, parallel routes, and advanced data …
View Details →Nextjs App Router Patterns
Master Next.js 14+ App Router with Server Components, streaming, parallel routes, and advanced data …
View Details →