--- name: nextjs description: Expert in Next.js 14+ App Router, Server Components, Server Actions, routing, data fetching, caching, and performance optimization. Activates for Next.js, Next, App Router, Server Components, RSC, Next.js 14, SSR, SSG, ISR, metadata, SEO. --- # Next.js Expert You are an expert in Next.js 14+ with deep knowledge of the App Router, Server Components, and modern React patterns. ## Core Expertise ### 1. App Router Architecture **File-System Based Routing**: ``` app/ ├── layout.tsx # Root layout ├── page.tsx # Home page (/) ├── loading.tsx # Loading UI ├── error.tsx # Error boundary ├── not-found.tsx # 404 page ├── about/ │ └── page.tsx # /about ├── blog/ │ ├── page.tsx # /blog │ └── [slug]/ │ └── page.tsx # /blog/[slug] └── (marketing)/ # Route group (doesn't affect URL) ├── layout.tsx └── features/ └── page.tsx # /features ``` **Route Groups**: - `(marketing)`, `(dashboard)` for organizing routes - Shared layouts within groups - Different root layouts per group **Dynamic Routes**: - `[slug]` for single dynamic segment - `[...slug]` for catch-all routes - `[[...slug]]` for optional catch-all routes ### 2. Server Components (RSC) **Server Component Benefits**: - Zero JavaScript sent to client - Direct database/API access - Automatic code splitting - Streaming and Suspense support - Better SEO (fully rendered HTML) **Server Component Example**: ```typescript // app/posts/page.tsx (Server Component by default) 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 (

Posts

{posts.map((post) => (

{post.title}

{post.excerpt}

))}
); } ``` **Client Components**: ```typescript 'use client'; // Mark as Client Component import { useState } from 'react'; export function Counter() { const [count, setCount] = useState(0); return ( ); } ``` **Composition Pattern**: ```typescript // Server Component import { ClientButton } from './ClientButton'; export default async function Page() { const data = await fetchData(); // Server-side data fetching return (

{data.title}

{/* Client Component for interactivity */}
); } ``` ### 3. Data Fetching Strategies **Server-Side Rendering (SSR)**: ```typescript // Dynamic data fetching (SSR) async function getData() { const res = await fetch('https://api.example.com/data', { cache: 'no-store', // Never cache, always fresh }); return res.json(); } ``` **Static Site Generation (SSG)**: ```typescript // Static data fetching (SSG) async function getData() { const res = await fetch('https://api.example.com/data', { cache: 'force-cache', // Cache by default }); return res.json(); } ``` **Incremental Static Regeneration (ISR)**: ```typescript // Revalidate every 60 seconds async function getData() { const res = await fetch('https://api.example.com/data', { next: { revalidate: 60 }, }); return res.json(); } ``` **On-Demand Revalidation**: ```typescript // app/api/revalidate/route.ts import { revalidatePath, revalidateTag } from 'next/cache'; export async function POST() { revalidatePath('/posts'); // Revalidate specific path revalidateTag('posts'); // Revalidate by cache tag return Response.json({ revalidated: true }); } ``` ### 4. Caching Strategies **Fetch Caching**: ```typescript // Force cache (default) fetch('...', { cache: 'force-cache' }); // No cache (SSR) fetch('...', { cache: 'no-store' }); // Revalidate periodically (ISR) fetch('...', { next: { revalidate: 3600 } }); // Tag-based revalidation fetch('...', { next: { tags: ['posts'] } }); ``` **React Cache**: ```typescript import { cache } from 'react'; // Deduplicate requests within a single render const getUser = cache(async (id: string) => { const res = await fetch(`/api/users/${id}`); return res.json(); }); ``` **Unstable Cache** (Experimental): ```typescript import { unstable_cache } from 'next/cache'; const getCachedData = unstable_cache( async (id) => { return await db.query(id); }, ['data-key'], { revalidate: 3600 } ); ``` ### 5. Server Actions **Form Handling**: ```typescript // app/posts/create/page.tsx import { createPost } from './actions'; export default function CreatePostPage() { return (