--- name: nextjs-app-router-expert description: Expert in Next.js 14/15 App Router architecture, React Server Components (RSC), Server Actions, and modern full-stack React development. Specializes in routing patterns, data fetching strategies, caching, streaming, and deployment optimization. version: 1.0.0 metadata: category: frontend tags: - nextjs - react - app-router - rsc - server-components - full-stack pairs-with: - skill: react-performance-optimizer reason: RSC and streaming SSR in App Router require React-specific performance optimization - skill: vercel-deployment reason: Vercel is the primary deployment platform optimized for Next.js App Router features - skill: fullstack-debugger reason: App Router debugging spans server components, client hydration, and API routes - skill: typescript-advanced-patterns reason: App Router server/client component types benefit from advanced TypeScript generics --- # Next.js App Router Expert ## Overview Expert in Next.js 14/15 App Router architecture, React Server Components (RSC), Server Actions, and modern full-stack React development. Specializes in routing patterns, data fetching strategies, caching, streaming, and deployment optimization. ## When to Use - Starting a new Next.js project with App Router - Migrating from Pages Router to App Router - Implementing complex routing patterns (parallel, intercepting routes) - Optimizing data fetching with RSC and caching - Setting up Server Actions for mutations - Configuring middleware for auth/redirects - Debugging hydration errors or RSC issues - Deploying to Vercel, Cloudflare, or self-hosted ## Capabilities ### Routing Architecture - File-based routing with `app/` directory - Dynamic routes (`[slug]`, `[...catchAll]`, `[[...optional]]`) - Route groups `(group)` for organization - Parallel routes `@modal`, `@sidebar` - Intercepting routes `(.)`, `(..)`, `(..)(..)` - Loading and error boundaries per route segment ### React Server Components - Server vs Client component boundaries - `'use client'` directive placement - Composition patterns (server wrapping client) - Streaming with Suspense boundaries - Progressive rendering strategies ### Data Fetching - `fetch()` with automatic deduplication - Caching strategies (`force-cache`, `no-store`, `revalidate`) - `generateStaticParams()` for static generation - Incremental Static Regeneration (ISR) - On-demand revalidation with `revalidatePath()` / `revalidateTag()` ### Server Actions - Form mutations with `'use server'` - Optimistic updates with `useOptimistic` - Progressive enhancement (works without JS) - Error handling and validation - Redirect after mutation ### Middleware & Edge - `middleware.ts` for auth, redirects, rewrites - Edge Runtime vs Node.js Runtime - Geolocation and conditional routing - A/B testing and feature flags ### Performance Optimization - Image optimization with `next/image` - Font optimization with `next/font` - Script loading strategies - Bundle analysis and code splitting - Partial prerendering (PPR) ## Dependencies Works well with: - `react-performance-optimizer` - React-specific performance patterns - `vercel-deployment` - Vercel deployment configuration - `cloudflare-worker-dev` - Edge deployment patterns - `postgresql-optimization` - Database queries for RSC ## Examples ### Basic Route Structure ``` app/ ├── layout.tsx # Root layout (required) ├── page.tsx # Home page (/) ├── loading.tsx # Loading UI ├── error.tsx # Error boundary ├── not-found.tsx # 404 page ├── blog/ │ ├── page.tsx # /blog │ └── [slug]/ │ ├── page.tsx # /blog/:slug │ └── loading.tsx # Per-route loading └── (auth)/ # Route group (no URL impact) ├── login/ │ └── page.tsx # /login └── register/ └── page.tsx # /register ``` ### Server Component with Data Fetching ```typescript // app/posts/page.tsx import { Suspense } from 'react'; async function getPosts() { const res = await fetch('https://api.example.com/posts', { next: { revalidate: 3600 }, // ISR: revalidate every hour }); return res.json(); } export default async function PostsPage() { const posts = await getPosts(); return (

Blog Posts

}>
); } ``` ### Server Action Form ```typescript // app/contact/page.tsx import { redirect } from 'next/navigation'; import { revalidatePath } from 'next/cache'; async function submitContact(formData: FormData) { 'use server'; const email = formData.get('email') as string; const message = formData.get('message') as string; // Validate if (!email || !message) { throw new Error('Email and message required'); } // Save to database await db.contacts.create({ email, message }); // Revalidate and redirect revalidatePath('/contact'); redirect('/contact/success'); } export default function ContactPage() { return (