|
| 1 | +--- |
| 2 | +name: cache-components |
| 3 | +description: Expert guidance for Next.js Cache Components and Partial Prerendering (PPR). PROACTIVE ACTIVATION when cacheComponents config is detected. |
| 4 | +--- |
| 5 | + |
| 6 | +Expert guidance for Next.js Cache Components and Partial Prerendering (PPR). |
| 7 | + |
| 8 | +**PROACTIVE ACTIVATION**: Use this skill automatically when working in Next.js projects that have `cacheComponents: true` in their `next.config.ts` or `next.config.js`. |
| 9 | + |
| 10 | +**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. |
| 11 | + |
| 12 | +**USE CASES**: |
| 13 | +- Implementing `'use cache'` directive |
| 14 | +- Configuring cache lifetimes with `cacheLife()` |
| 15 | +- Tagging cached data with `cacheTag()` |
| 16 | +- Invalidating caches with `updateTag()` / `revalidateTag()` |
| 17 | +- Optimizing static vs dynamic content boundaries |
| 18 | +- Debugging cache issues |
| 19 | +- Reviewing Cache Component implementations |
| 20 | + |
| 21 | +## Project Detection |
| 22 | + |
| 23 | +When starting work in a Next.js project, check if Cache Components are enabled: |
| 24 | + |
| 25 | +```bash |
| 26 | +# Check next.config.ts or next.config.js for cacheComponents |
| 27 | +grep -r "cacheComponents" next.config.* 2>/dev/null |
| 28 | +``` |
| 29 | + |
| 30 | +If `cacheComponents: true` is found, apply this skill's patterns proactively when: |
| 31 | +- Writing React Server Components |
| 32 | +- Implementing data fetching |
| 33 | +- Creating Server Actions with mutations |
| 34 | +- Optimizing page performance |
| 35 | +- Reviewing existing component code |
| 36 | + |
| 37 | +## Core Concept: The Caching Decision Tree |
| 38 | + |
| 39 | +When writing a **React Server Component**, ask: |
| 40 | + |
| 41 | +1. **Does it depend on request context?** (cookies, headers, searchParams) |
| 42 | +2. **Can this be cached?** (Is the output the same for all users?) |
| 43 | + - **YES** -> `'use cache'` + `cacheTag()` + `cacheLife()` |
| 44 | + - **NO** -> Wrap in `<Suspense>` (dynamic streaming) |
| 45 | + |
| 46 | +## Philosophy: Code Over Configuration |
| 47 | + |
| 48 | +Cache Components represents a shift from segment-based configuration to compositional code: |
| 49 | + |
| 50 | +- **Before (Deprecated)**: `export const revalidate = 3600` |
| 51 | +- **After**: `cacheLife('hours')` inside `'use cache'` |
| 52 | + |
| 53 | +- **Before (Deprecated)**: `export const dynamic = 'force-static'` |
| 54 | +- **After**: Use `'use cache'` and Suspense boundaries |
| 55 | + |
| 56 | +## Quick Start |
| 57 | + |
| 58 | +### 1. Enable Configuration |
| 59 | +```typescript |
| 60 | +// next.config.ts |
| 61 | +import type { NextConfig } from "next"; |
| 62 | + |
| 63 | +const nextConfig: NextConfig = { |
| 64 | + experimental: { |
| 65 | + ppr: true, |
| 66 | + dynamicIO: true, // often correlated features |
| 67 | + }, |
| 68 | + // Ensure basic cache components flag if required by your version |
| 69 | +}; |
| 70 | + |
| 71 | +export default nextConfig; |
| 72 | +``` |
| 73 | + |
| 74 | +### 2. Basic Usage |
| 75 | + |
| 76 | +```typescript |
| 77 | +import { cacheLife } from 'next/cache'; |
| 78 | + |
| 79 | +async function CachedPosts() { |
| 80 | + 'use cache' |
| 81 | + cacheLife('hours'); // Cache for hours |
| 82 | + |
| 83 | + const posts = await db.posts.findMany(); |
| 84 | + return <PostList posts={posts} />; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Core APIs |
| 89 | + |
| 90 | +### `'use cache'` |
| 91 | +Marks a function, component, or file as cacheable. The return value is cached and shared across requests. |
| 92 | + |
| 93 | +### `cacheLife(profile)` |
| 94 | +Control cache duration using semantic profiles: |
| 95 | +- `'seconds'`: Short-lived |
| 96 | +- `'minutes'`: Medium-lived |
| 97 | +- `'hours'`: Long-lived |
| 98 | +- `'days'`: Very long-lived |
| 99 | +- `'weeks'`: Static-like content |
| 100 | +- `'max'`: Permanent cache |
| 101 | + |
| 102 | +### `cacheTag(...tags)` |
| 103 | +Tag cached data for on-demand invalidation. |
| 104 | + |
| 105 | +```typescript |
| 106 | +import { cacheTag } from 'next/cache'; |
| 107 | + |
| 108 | +async function getUserProfile(id: string) { |
| 109 | + 'use cache' |
| 110 | + cacheTag('user-profile', `user-${id}`); |
| 111 | + // ... fetch logic |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### `revalidateTag(tag)` / `expireTag(tag)` |
| 116 | +Invalidate cached data in background or immediately. |
| 117 | + |
| 118 | +```typescript |
| 119 | +'use server' |
| 120 | +import { expireTag } from 'next/cache'; |
| 121 | + |
| 122 | +export async function updateUser(id: string, data: any) { |
| 123 | + await db.user.update({ where: { id }, data }); |
| 124 | + expireTag(`user-${id}`); // Invalidate specific cache |
| 125 | +} |
| 126 | +``` |
0 commit comments