How Next.js 16 Works
Next.js 16 uses the App Router with React Server Components by default. It introduces Cache Components with the "use cache" directive, Turbopack as the default bundler, and React 19.2 features.
1. Server-First Rendering
Components are Server Components by default. They:
- Run only on the server
- Can directly fetch data (no useEffect needed)
- Cannot use hooks, event handlers, or browser APIs
- Reduce client JavaScript bundle
Add 'use client' only when you need interactivity, state, or browser APIs.
2. BFF Pattern (Backend for Frontend)
Next.js acts as an intermediate layer between your React UI and backend APIs:
- Server Components fetch data from Rails during render
- Server Actions handle mutations by calling Rails APIs
- Route Handlers provide API endpoints when needed (webhooks, external integrations)
Keep sensitive logic (tokens, API keys) in the server layer - never expose to client.
3. Cache Components (New in Next.js 16)
Next.js 16 introduces explicit, opt-in caching with the "use cache" directive:
// next.config.ts
const nextConfig = {
cacheComponents: true,
};
"use cache"
export async function getProducts() {
// This function is cached
return await db.products.findMany()
}
- All dynamic code runs at request time by default
- Use
"use cache" to opt-in to caching pages, components, and functions
- Compiler automatically generates cache keys
- Replaces
experimental.dynamicIO and experimental.ppr flags
4. New Caching APIs
revalidateTag(tag, profile) - Now requires a cacheLife profile:
revalidateTag('products', 'max') // Built-in profiles: 'max', 'hours', 'days'
revalidateTag('products', { revalidate: 3600 }) // Custom time
updateTag(tag) - New! Immediate refresh (read-your-writes):
import { updateTag } from 'next/cache'
// Use in Server Actions for instant UI updates
updateTag('user-profile')
refresh() - New! Refresh uncached data only:
import { refresh } from 'next/cache'
// Use in Server Actions to refresh uncached data (notifications, metrics)
refresh()
5. File-Based Conventions
Special files in app/ directory:
page.tsx - Route UI
layout.tsx - Shared wrapper (persists across navigations)
loading.tsx - Suspense fallback
error.tsx - Error boundary
route.ts - API endpoint (Route Handler)
proxy.ts - Network boundary (replaces middleware.ts)
6. Turbopack (Default Bundler)
Turbopack is now the default bundler:
- 2-5× faster production builds
- Up to 10× faster Fast Refresh
- Opt out with
next dev --webpack or next build --webpack
7. React 19.2 Features
Next.js 16 includes React 19.2 with:
- View Transitions - Animate elements during navigation/state updates
- Activity - Hide UI with
display: none while maintaining state
- useEffectEvent - Extract non-reactive logic from Effects
- Build a new Next.js app
- Add a page or feature
- Add a Server Action (mutation)
- Add a Route Handler (API endpoint)
- Debug an issue
- Write tests
- Optimize performance
- Ship/deploy
Then read the matching workflow from workflows/ and follow it.
After reading the workflow, follow it exactly.
After Every Change
# 1. TypeScript compiles?
bunx tsc --noEmit
# 2. Lint passes?
bun run lint
# 3. Dev server runs?
bun run dev
Check browser for:
- No hydration errors in console
- No "use client" / "use server" boundary violations
- Data loads correctly from Rails API
Report to user:
- "TypeScript: ✓"
- "Lint: ✓"
- "Dev server: Running on localhost:3000"
- "Ready for you to verify [specific feature]"
Domain Knowledge
All in references/:
Architecture: app-router.md, project-structure.md, bff-patterns.md
Components: server-components.md, client-components.md
Data: data-fetching.md, server-actions.md, route-handlers.md
Navigation: redirecting.md
UX: loading-streaming.md, error-handling.md
Configuration: environment-variables.md, scripts.md
Security: security.md
Quality: typescript.md, testing.md, performance.md, accessibility.md, anti-patterns.md
Workflows
All in workflows/:
| File |
Purpose |
| build-new-app.md |
Create Next.js 16 app from scratch |
| add-page.md |
Add pages, components, layouts |
| add-server-action.md |
Server Actions for mutations |
| add-route-handler.md |
API endpoints (Route Handlers) |
| debug-app.md |
Fix errors, hydration issues, build failures |
| write-tests.md |
Unit, integration, E2E testing |
| optimize-performance.md |
Core Web Vitals, bundle size, caching |
| ship-app.md |
Deploy to Vercel, Docker, etc. |
1---2name: nextjs3description: Build Next.js 16 applications with App Router, React Server Components, Cache Components, and BFF patterns. Full lifecycle - build, debug, test, optimize, ship. Specializes in Next.js as client + BFF layer calling backend APIs.4---56<essential_principles>78## How Next.js 16 Works910Next.js 16 uses the App Router with React Server Components by default. It introduces Cache Components with the `"use cache"` directive, Turbopack as the default bundler, and React 19.2 features.1112### 1. Server-First Rendering1314Components are Server Components by default. They:15- Run only on the server16- Can directly fetch data (no useEffect needed)17- Cannot use hooks, event handlers, or browser APIs18- Reduce client JavaScript bundle1920Add `'use client'` only when you need interactivity, state, or browser APIs.2122### 2. BFF Pattern (Backend for Frontend)2324Next.js acts as an intermediate layer between your React UI and backend APIs:25- **Server Components** fetch data from Rails during render26- **Server Actions** handle mutations by calling Rails APIs27- **Route Handlers** provide API endpoints when needed (webhooks, external integrations)2829Keep sensitive logic (tokens, API keys) in the server layer - never expose to client.3031### 3. Cache Components (New in Next.js 16)3233Next.js 16 introduces **explicit, opt-in caching** with the `"use cache"` directive:3435```typescript36// next.config.ts37const nextConfig = {38 cacheComponents: true,39};40```4142```typescript43"use cache"4445export async function getProducts() {46 // This function is cached47 return await db.products.findMany()48}49```5051- All dynamic code runs at request time by default52- Use `"use cache"` to opt-in to caching pages, components, and functions53- Compiler automatically generates cache keys54- Replaces `experimental.dynamicIO` and `experimental.ppr` flags5556### 4. New Caching APIs5758**`revalidateTag(tag, profile)`** - Now requires a cacheLife profile:59```typescript60revalidateTag('products', 'max') // Built-in profiles: 'max', 'hours', 'days'61revalidateTag('products', { revalidate: 3600 }) // Custom time62```6364**`updateTag(tag)`** - New! Immediate refresh (read-your-writes):65```typescript66import { updateTag } from 'next/cache'67// Use in Server Actions for instant UI updates68updateTag('user-profile')69```7071**`refresh()`** - New! Refresh uncached data only:72```typescript73import { refresh } from 'next/cache'74// Use in Server Actions to refresh uncached data (notifications, metrics)75refresh()76```7778### 5. File-Based Conventions7980Special files in app/ directory:81- `page.tsx` - Route UI82- `layout.tsx` - Shared wrapper (persists across navigations)83- `loading.tsx` - Suspense fallback84- `error.tsx` - Error boundary85- `route.ts` - API endpoint (Route Handler)86- `proxy.ts` - Network boundary (replaces middleware.ts)8788### 6. Turbopack (Default Bundler)8990Turbopack is now the default bundler:91- 2-5× faster production builds92- Up to 10× faster Fast Refresh93- Opt out with `next dev --webpack` or `next build --webpack`9495### 7. React 19.2 Features9697Next.js 16 includes React 19.2 with:98- **View Transitions** - Animate elements during navigation/state updates99- **Activity** - Hide UI with `display: none` while maintaining state100- **useEffectEvent** - Extract non-reactive logic from Effects101102</essential_principles>103104<intake>105What would you like to do?1061071. Build a new Next.js app1082. Add a page or feature1093. Add a Server Action (mutation)1104. Add a Route Handler (API endpoint)1115. Debug an issue1126. Write tests1137. Optimize performance1148. Ship/deploy115116**Then read the matching workflow from `workflows/` and follow it.**117</intake>118119<routing>120| Response | Workflow |121|----------|----------|122| 1, "new", "create", "start", "init" | `workflows/build-new-app.md` |123| 2, "page", "feature", "add", "component" | `workflows/add-page.md` |124| 3, "action", "mutation", "form", "submit" | `workflows/add-server-action.md` |125| 4, "api", "route", "handler", "endpoint" | `workflows/add-route-handler.md` |126| 5, "debug", "fix", "error", "broken", "bug" | `workflows/debug-app.md` |127| 6, "test", "testing", "vitest", "playwright" | `workflows/write-tests.md` |128| 7, "performance", "optimize", "slow", "vitals" | `workflows/optimize-performance.md` |129| 8, "deploy", "ship", "vercel", "production" | `workflows/ship-app.md` |130| other | Clarify intent, then select workflow |131132**After reading the workflow, follow it exactly.**133</routing>134135<verification_loop>136137## After Every Change138139```bash140# 1. TypeScript compiles?141bunx tsc --noEmit142143# 2. Lint passes?144bun run lint145146# 3. Dev server runs?147bun run dev148```149150Check browser for:151- No hydration errors in console152- No "use client" / "use server" boundary violations153- Data loads correctly from Rails API154155Report to user:156- "TypeScript: ✓"157- "Lint: ✓"158- "Dev server: Running on localhost:3000"159- "Ready for you to verify [specific feature]"160161</verification_loop>162163<reference_index>164165## Domain Knowledge166167All in `references/`:168169**Architecture:** app-router.md, project-structure.md, bff-patterns.md170**Components:** server-components.md, client-components.md171**Data:** data-fetching.md, server-actions.md, route-handlers.md172**Navigation:** redirecting.md173**UX:** loading-streaming.md, error-handling.md174**Configuration:** environment-variables.md, scripts.md175**Security:** security.md176**Quality:** typescript.md, testing.md, performance.md, accessibility.md, anti-patterns.md177178</reference_index>179180<workflows_index>181182## Workflows183184All in `workflows/`:185186| File | Purpose |187|------|---------|188| build-new-app.md | Create Next.js 16 app from scratch |189| add-page.md | Add pages, components, layouts |190| add-server-action.md | Server Actions for mutations |191| add-route-handler.md | API endpoints (Route Handlers) |192| debug-app.md | Fix errors, hydration issues, build failures |193| write-tests.md | Unit, integration, E2E testing |194| optimize-performance.md | Core Web Vitals, bundle size, caching |195| ship-app.md | Deploy to Vercel, Docker, etc. |196197</workflows_index>