# Next.js Patterns
+
+Production patterns for Next.js App Router: React Server Components, data fetching, caching strategies, Server Actions, streaming with Suspense, middleware, metadata, and error handling. This skill covers the architecture decisions that determine whether a Next.js app is fast, maintainable, and secure in production.
+
+## When to use
+
+- Building a new application on Next.js App Router
+- Migrating from Pages Router to App Router
+- Deciding server vs. client component boundaries
+- Implementing data fetching with caching and revalidation
+- Setting up authentication middleware
+- Configuring metadata and SEO for production
+- Implementing Server Actions for form mutations
+
+## When NOT to use
+
+- Non-Next.js React applications — use React patterns directly
+- Static sites with no server requirements — consider Astro or Vite
+- Backend API design — Next.js API routes are thin; use dedicated backend services for complex logic
+- CSS/design system work — use Tailwind Design System skill
+- Animation and motion — use Framer Motion or GSAP skills
−Production patterns for Next.js App Router: server components, data fetching, caching, middleware, and deployment.
+## Core concepts
−## Server vs. Client Components
+### Server vs. Client Components
| Concern | Server Component | Client Component |
|---------|-----------------|------------------|
−| Data fetching | Direct DB/API access | Via API route or server action |
+| Data fetching | Direct DB/API access | Via API route or Server Action |
| Bundle size | Zero JS shipped | Included in client bundle |
+| Interactivity | None (no hooks, no effects) | Full React interactivity |
+| Browser APIs | Not available | Full access (window, document) |
+| Default | Yes (no directive needed) | Requires `"use client"` |
+
+**Rule:** Default to Server Components. Add `"use client"` only when you need interactivity, effects, or browser APIs. Push the `"use client"` boundary as deep into the component tree as possible.
+
+### Caching layers
+
+| Layer | Scope | Control | Default |
+|-------|-------|---------|---------|
+| Request memoization | Single render | Automatic for `fetch()` | On |
+| Data cache | Across requests | `revalidate`, `no-store` | On |
+| Full route cache | Entire route | Static/dynamic rendering | Static if possible |
+| Router cache | Client-side | `router.refresh()`, `revalidatePath()` | On |
+
+## Workflow
+
+### 1. Define the route structure
+
+```
+app/
+ layout.tsx # root layout (Server Component)
+ page.tsx # home page
+ loading.tsx # loading UI for streaming
+ error.tsx # error boundary
+ not-found.tsx # 404 page
+ (marketing)/
+ page.tsx # marketing home with different layout
+ layout.tsx # marketing layout
+ dashboard/
+ layout.tsx # dashboard layout with sidebar
+ page.tsx # dashboard home
+ settings/
+ page.tsx # settings page
+```
+
+### 2. Server Actions for mutations
+
+```tsx
+"use server";
+
+import { revalidatePath } from "next/cache";
+import { redirect } from "next/navigation";
+
+export async function createPost(formData: FormData) {
+ const title = formData.get("title") as string;
+ const body = formData.get("body") as string;