Skip to content

Commit ea7b2b0

Browse files
authored
enhance cache components flow (#12)
1 parent 079d54c commit ea7b2b0

File tree

3 files changed

+258
-52
lines changed

3 files changed

+258
-52
lines changed

src/mcp-prompts/enable-cache-components-prompt.md

Lines changed: 226 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,54 @@ This prompt uses the Next.js 16 Knowledge Base resources for on-demand access to
2828

2929
---
3030

31-
# ENABLE WORKFLOW: Cache Components Setup & Verification Guide
32-
33-
The section below contains the step-by-step enablement workflow. Load the knowledge base resources above for detailed technical behavior, API semantics, and best practices.
31+
# ENABLE WORKFLOW: Complete Cache Components Setup & Verification Guide
32+
33+
The section below contains the comprehensive step-by-step enablement workflow. This guide includes ALL steps needed to enable Cache Components: configuration updates, flag changes, boundary setup, error detection, and automated fixing. Load the knowledge base resources above for detailed technical behavior, API semantics, and best practices.
34+
35+
## Overview: What This Process Covers
36+
37+
This prompt automates the complete Cache Components enablement workflow:
38+
39+
**Configuration & Flags (Phase 1-2):**
40+
- ✅ Detect package manager (npm/pnpm/yarn/bun)
41+
- ✅ Verify Next.js version (16.0.0+)
42+
- ✅ Update next.config to enable `experimental.cacheComponents`
43+
- ✅ Migrate from `experimental.dynamicIO` or `experimental.ppr` if needed
44+
- ✅ Document existing Route Segment Config for migration
45+
46+
**Dev Server & MCP Setup (Phase 3):**
47+
- ✅ Start dev server once with MCP enabled (`__NEXT_EXPERIMENTAL_MCP_SERVER=true`)
48+
- ✅ Verify MCP server is active and responding
49+
- ✅ Capture base URL and MCP endpoint for error detection
50+
51+
**Error Detection (Phase 4):**
52+
- ✅ Load every route in Chrome browser using chrome_devtools tool
53+
- ✅ Collect errors from Chrome session using Next.js MCP `get_errors` tool
54+
- ✅ Categorize all Cache Components errors by type
55+
- ✅ Build comprehensive error list before fixing
56+
57+
**Automated Fixing (Phase 5):**
58+
- ✅ Fix blocking route errors (add Suspense boundaries or "use cache")
59+
- ✅ Fix dynamic value errors (add `await connection()`)
60+
- ✅ Fix route params errors (add `generateStaticParams`)
61+
- ✅ Fix unavailable API errors (move outside cache or use "use cache: private")
62+
- ✅ Migrate Route Segment Config to "use cache" + cacheLife
63+
- ✅ Add cache tags with cacheTag() for on-demand revalidation
64+
- ✅ Configure cacheLife profiles for revalidation control
65+
- ✅ Verify each fix with Fast Refresh (no restart needed)
66+
67+
**Final Verification (Phase 6):**
68+
- ✅ Verify all routes return 200 OK
69+
- ✅ Confirm zero errors with final `get_errors` check
70+
- ✅ Stop dev server after verification
71+
- ✅ Run production build and test
72+
73+
**Key Features:**
74+
- One-time dev server start (no restarts needed)
75+
- Automated error detection using Next.js MCP tools
76+
- Browser-based testing with chrome_devtools
77+
- Fast Refresh applies fixes instantly
78+
- Comprehensive fix strategies for all error types
3479

3580
## Decision Guide: Suspense vs "use cache"
3681

@@ -105,68 +150,120 @@ Before enabling Cache Components:
105150

106151
## PHASE 2: Enable Cache Components Configuration
107152
────────────────────────────────────────
108-
Update the Next.js configuration to enable Cache Components:
153+
Update the Next.js configuration to enable Cache Components. This phase handles ALL configuration and flag changes needed.
109154

110155
**Step 1: Backup existing config**
111156
Copy current next.config.js or next.config.ts before making changes
112157

113-
**Step 2: Update configuration**
158+
**Step 2: Update experimental.cacheComponents flag**
159+
160+
The `experimental.cacheComponents` flag is the PRIMARY configuration change. Choose the right option based on your current config:
114161

115-
Option A - If starting fresh (no existing experimental.cacheComponents):
162+
**Option A - If starting fresh (no existing experimental flags):**
116163
```typescript
117164
// next.config.ts (or .js)
118165
const nextConfig = {
119166
experimental: {
120-
cacheComponents: true,
167+
cacheComponents: true, // Enable Cache Components
121168
},
122169
}
123170

124171
export default nextConfig
125172
```
126173

127-
Option B - If migrating from experimental.dynamicIO:
174+
**Option B - If migrating from experimental.dynamicIO:**
128175
```diff
129176
const nextConfig = {
130177
experimental: {
131-
- dynamicIO: true,
132-
+ cacheComponents: true,
178+
- dynamicIO: true, // Old name (deprecated)
179+
+ cacheComponents: true, // New name (current)
133180
},
134181
}
135182
```
136183

137-
Option C - If migrating from experimental.ppr:
184+
**Option C - If migrating from experimental.ppr:**
138185
```diff
139186
const nextConfig = {
140187
experimental: {
141-
- ppr: true,
142-
+ cacheComponents: true,
188+
- ppr: true, // Removed in Next.js 16
189+
+ cacheComponents: true, // Replacement feature
143190
},
144191
}
145192
```
146193

147194
⚠️ **Important for PPR Migration:**
148195
If you were using `experimental.ppr`, note that Cache Components has:
149-
- Different implementation details
150-
- Additional features and behaviors
196+
- Different implementation details and behavior
197+
- Additional features (cacheLife, cacheTag, "use cache: private", etc.)
198+
- Different static shell generation rules
151199
- May require code adjustments in your routes
152200
- Review route-level cache behavior after migration
153201

154-
**Step 3: Consider cacheLife profiles**
155-
If you're using `revalidateTag()`, update to the new signature:
156-
```typescript
157-
import { revalidateTag } from 'next/cache'
202+
**Step 3: Remove incompatible flags**
203+
204+
If present, REMOVE these flags (they conflict with Cache Components):
205+
```diff
206+
const nextConfig = {
207+
experimental: {
208+
cacheComponents: true,
209+
- ppr: true, // Remove - replaced by cacheComponents
210+
},
211+
}
212+
```
158213

159-
// Old (deprecated)
160-
revalidateTag('my-tag')
214+
**Step 4: Preserve compatible flags**
161215

162-
// New (with cacheLife profile)
163-
revalidateTag('my-tag', 'max') // Recommended for most cases
216+
These experimental flags CAN coexist with cacheComponents:
217+
- `turbo` - Turbopack configuration (separate feature)
218+
- `serverActions` - Server Actions config (separate feature)
219+
- `mdxRs` - MDX support (separate feature)
220+
- Other non-caching related flags
221+
222+
Example of valid combined config:
223+
```typescript
224+
const nextConfig = {
225+
experimental: {
226+
cacheComponents: true, // Cache Components
227+
turbo: { // Turbopack config (compatible)
228+
rules: { /* ... */ }
229+
},
230+
serverActions: { // Server Actions (compatible)
231+
bodySizeLimit: '2mb'
232+
},
233+
},
234+
}
164235
```
165236

166-
Built-in cacheLife profiles:
167-
- `'max'` - Long-lived content with background revalidation (recommended)
168-
- `'hours'` - Content that changes every few hours
169-
- `'days'` - Content that changes daily or less frequently
237+
**Step 5: Document Route Segment Config usage**
238+
239+
Search for existing Route Segment Config exports in your routes:
240+
- `export const dynamic = ...`
241+
- `export const revalidate = ...`
242+
- `export const fetchCache = ...`
243+
- `export const runtime = ...`
244+
- `export const preferredRegion = ...`
245+
246+
⚠️ **CRITICAL: Route Segment Config is DISABLED with Cache Components**
247+
248+
These options will cause build errors and MUST be migrated:
249+
- `dynamic: 'force-static'` → Use `"use cache"` directive
250+
- `dynamic: 'force-dynamic'` → Use Suspense boundary
251+
- `revalidate: 3600` → Use `cacheLife({ revalidate: 3600 })`
252+
- `fetchCache: 'force-cache'` → Use `"use cache"`
253+
254+
Document all Route Segment Config locations now - you'll migrate them in Phase 5.
255+
256+
**Step 6: Verify configuration changes**
257+
258+
After making changes, verify:
259+
-`experimental.cacheComponents: true` is set
260+
- ✅ Incompatible flags removed (`experimental.ppr`)
261+
- ✅ Compatible flags preserved (if any)
262+
- ✅ Route Segment Config locations documented
263+
- ✅ Config file syntax is valid (no syntax errors)
264+
265+
**What's Next:**
266+
With configuration updated, Phase 3 will start the dev server and Phase 4 will detect any runtime errors that need fixing.
170267

171268
## PHASE 3: Start Dev Server with MCP
172269
────────────────────────────────────────
@@ -464,13 +561,23 @@ Systematically verify each route and collect errors:
464561
- If chrome_devtools navigation fails, check if Chrome is installed
465562
- If Next.js MCP connection fails, the dev server may have crashed (rare)
466563

467-
## PHASE 5: Automated Error Fixing
564+
## PHASE 5: Automated Error Fixing & Boundary Setup
468565
────────────────────────────────────────
469566

470567
**Prerequisites:**
471568
- ✅ Dev server is still running from Phase 3 (do NOT restart it)
472-
- ✅ Error list collected from Phase 4
473-
- ✅ Fast Refresh will apply changes automatically
569+
- ✅ Comprehensive error list collected from Phase 4
570+
- ✅ Fast Refresh will apply changes automatically (no restart needed)
571+
572+
This phase handles ALL code changes needed for Cache Components:
573+
- Adding Suspense boundaries for dynamic content
574+
- Adding "use cache" directives for cacheable content
575+
- Fixing dynamic value errors with connection()
576+
- Adding generateStaticParams for route params
577+
- Migrating Route Segment Config to "use cache" + cacheLife
578+
- Setting up cache tags with cacheTag() for revalidation
579+
- Configuring cacheLife profiles for fine-grained control
580+
- Moving unavailable APIs outside cache scope
474581

475582
Fix errors systematically based on error type:
476583

@@ -846,11 +953,15 @@ Report findings in this format:
846953
[x] Package manager detected: [manager]
847954
[x] Existing config checked
848955
[x] Routes identified: [count] routes
956+
[x] Route Segment Config usage documented
849957
850-
## Phase 2: Configuration
851-
[x] Cache Components enabled in next.config
958+
## Phase 2: Configuration & Flags
959+
[x] Cache Components flag enabled: experimental.cacheComponents = true
852960
[x] Configuration backed up
853-
[ ] cacheLife profiles reviewed
961+
[x] Incompatible flags removed (experimental.ppr if present)
962+
[x] Compatible flags preserved
963+
[x] Route Segment Config locations documented for migration
964+
[x] Config file syntax validated
854965
855966
## Phase 3: Dev Server
856967
[x] Checked for existing servers/stale locks
@@ -874,35 +985,62 @@ Report findings in this format:
874985
- File: [file path]
875986
- Status: [Fixed/Pending]
876987
877-
## Phase 5: Fixes Applied
988+
## Phase 5: Boundary Setup & Code Changes
878989
[List all fixes made, grouped by error type]
879990
880-
### A. Blocking Route Errors Fixed: [count]
881-
- [file path]: Added Suspense boundary / Added "use cache" / Created loading.tsx
882-
- [file path]: [specific fix applied]
991+
### A. Suspense Boundaries Added: [count]
992+
- [file path]: Added Suspense boundary in page component
993+
- [file path]: Added Suspense boundary in layout
994+
- [file path]: Created loading.tsx file
995+
- ...
996+
997+
### B. "use cache" Directives Added: [count]
998+
- [file path]: Added "use cache" to page component (public cache)
999+
- [file path]: Added "use cache: private" to page component (prefetchable with cookies/params)
1000+
- [file path]: Added "use cache" to individual function
8831001
- ...
8841002
885-
### B. Dynamic Value Errors Fixed: [count]
886-
- [file path]: Added await connection() before Math.random()/Date()
1003+
### C. Dynamic Value Errors Fixed: [count]
1004+
- [file path]: Added await connection() before Math.random()
1005+
- [file path]: Added await connection() before new Date()
8871006
- ...
8881007
889-
### C. Route Params Errors Fixed: [count]
1008+
### D. Route Params Errors Fixed: [count]
8901009
- [file path]: Added generateStaticParams with known params
1010+
- [file path]: Added generateStaticParams for dynamic route
1011+
- ...
1012+
1013+
### E. Unavailable API Errors Fixed: [count]
1014+
- [file path]: Moved cookies() call outside cache scope
1015+
- [file path]: Moved headers() call outside cache scope
1016+
- [file path]: Changed to "use cache: private" to allow cookies/params
8911017
- ...
8921018
893-
### D. Unavailable API Errors Fixed: [count]
894-
- [file path]: Moved cookies()/headers() outside cache scope / Changed to "use cache: private"
1019+
### F. Route Segment Config Migrations: [count]
1020+
- [file path]: Removed export const dynamic = 'force-static', replaced with "use cache"
1021+
- [file path]: Removed export const revalidate = 3600, replaced with cacheLife({ revalidate: 3600 })
1022+
- [file path]: Removed export const fetchCache, replaced with "use cache"
8951023
- ...
8961024
897-
### E. Route Segment Config Migrations: [count]
898-
- [file path]: Removed export const dynamic, replaced with "use cache" + cacheLife
1025+
### G. Cache Tags Added: [count]
1026+
- [file path]: Added cacheTag('posts') for on-demand revalidation
1027+
- [file path]: Added cacheTag('products') for granular control
8991028
- ...
9001029
901-
### F. Cache Optimizations Added: [count]
902-
- [file path]: Added cacheLife profile for revalidation control
903-
- [file path]: Added cacheTag for granular revalidation
1030+
### H. cacheLife Profiles Configured: [count]
1031+
- [file path]: Added cacheLife({ revalidate: 900, expire: 3600 })
1032+
- [file path]: Added cacheLife('max') for long-lived content
1033+
- [file path]: Added cacheLife('hours') for frequently changing content
9041034
- ...
9051035
1036+
### Summary of All Code Changes:
1037+
- Total Suspense boundaries added: [count]
1038+
- Total "use cache" directives added: [count]
1039+
- Total generateStaticParams functions added: [count]
1040+
- Total Route Segment Config exports removed: [count]
1041+
- Total cache tags added: [count]
1042+
- Total cacheLife profiles configured: [count]
1043+
9061044
## Phase 6: Final Verification
9071045
[x] All routes return 200 OK (with dev server running)
9081046
[x] No errors in get_errors final check
@@ -913,11 +1051,40 @@ Report findings in this format:
9131051
## Migration Notes
9141052
[Any special notes about the migration, especially if migrating from PPR]
9151053
1054+
## Complete Changes Summary
1055+
This enablement process made the following comprehensive changes:
1056+
1057+
### Configuration Changes (Phase 2):
1058+
- ✅ Enabled experimental.cacheComponents flag in next.config
1059+
- ✅ Removed incompatible flags (experimental.ppr if present)
1060+
- ✅ Preserved compatible experimental flags
1061+
- ✅ Documented Route Segment Config for migration
1062+
1063+
### Boundary & Cache Setup (Phase 5):
1064+
- ✅ Added Suspense boundaries for dynamic content
1065+
- ✅ Added "use cache" directives for cacheable content
1066+
- ✅ Added "use cache: private" for prefetchable private content
1067+
- ✅ Created loading.tsx files where appropriate
1068+
- ✅ Added generateStaticParams for dynamic routes
1069+
1070+
### API Migrations (Phase 5):
1071+
- ✅ Moved cookies()/headers() calls outside cache scope
1072+
- ✅ Added await connection() for dynamic values
1073+
- ✅ Migrated Route Segment Config to "use cache" + cacheLife
1074+
- ✅ Removed all export const dynamic/revalidate/fetchCache
1075+
1076+
### Cache Optimization (Phase 5):
1077+
- ✅ Added cacheTag() calls for granular revalidation
1078+
- ✅ Configured cacheLife profiles for revalidation control
1079+
- ✅ Set up cache invalidation strategies
1080+
9161081
## Next Steps
9171082
- Monitor application behavior in development
9181083
- Test interactive features with Cache Components
919-
- Review cacheLife profile usage
1084+
- Review cacheLife profile usage for optimization
1085+
- Test prefetching in production build
9201086
- Consider enabling Turbopack file system caching for faster dev
1087+
- Monitor cache hit rates and adjust cacheLife profiles
9211088
9221089
## Troubleshooting Tips
9231090
- If cached components re-execute on every request: Check Suspense boundaries, consider "use cache: remote"
@@ -926,6 +1093,17 @@ Report findings in this format:
9261093
- If "use cache" with params fails: Add generateStaticParams
9271094
- If dynamic APIs fail in cache: Move outside cache scope or use "use cache: private"
9281095
- If Route Segment Config errors: Remove exports, use "use cache" + cacheLife instead
1096+
1097+
## What Was Accomplished
1098+
Cache Components is now fully enabled with:
1099+
- ✅ Configuration flags properly set
1100+
- ✅ All routes verified and working
1101+
- ✅ All boundaries properly configured
1102+
- ✅ All cache directives in place
1103+
- ✅ All API migrations completed
1104+
- ✅ Cache optimization strategies implemented
1105+
- ✅ Zero errors in final verification
1106+
- ✅ Production build tested and passing
9291107
```
9301108

9311109
# START HERE

src/mcp-prompts/enable-cache-components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { join } from "path"
55
export const enableCacheComponentsPrompt: Prompt = {
66
name: "enable-cache-components",
77
description:
8-
"Enable and verify Cache Components in Next.js 16. Configures experimental.cacheComponents, starts dev server with MCP enabled, verifies all routes, and fixes any errors automatically.",
8+
"Complete Cache Components setup for Next.js 16. Handles ALL steps: updates experimental.cacheComponents flag, removes incompatible flags, migrates Route Segment Config, starts dev server with MCP, detects all errors via chrome_devtools + get_errors, automatically fixes all issues by adding Suspense boundaries, 'use cache' directives, generateStaticParams, cacheLife profiles, cache tags, and validates everything with zero errors.",
99
arguments: [
1010
{
1111
name: "project_path",

0 commit comments

Comments
 (0)