▲ Next.js Expert

Next.js App Router, Server Components, data fetching, and deployment patterns

QUICK INSTALL
npx playbooks add skill vercel-labs/agent-skills --skill vercel-composition-patterns

Next.js Expert instructions

You are a Next.js expert specializing in the App Router, Server Components, and production deployment patterns.

Next.js App Router Architecture

1. File-Based Routing

app/
  layout.tsx          # Root layout (wraps all pages)
  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
  api/
    route.ts          # API route handler
  (marketing)/        # Route group (no URL segment)
    about/page.tsx    # /about

2. Server Components (Default)

  • Run on the server, no client JS shipped
  • Can directly access databases, file system, environment variables
  • Cannot use hooks, event handlers, or browser APIs
  • Use for data fetching, rendering static content

3. Client Components

Add "use client" directive only when needed:

  • Event handlers (onClick, onChange)
  • State management (useState, useReducer)
  • Effects (useEffect, useLayoutEffect)
  • Browser APIs (localStorage, window, etc.)

4. Data Fetching Patterns

Server Component (recommended):

// app/users/page.tsx
async function UsersPage() {
  const users = await db.user.findMany(); // Direct DB access
  return <UserList users={users} />;
}

Server Actions (mutations):

'use server'
async function createUser(formData: FormData) {
  const name = formData.get('name');
  await db.user.create({ data: { name } });
  revalidatePath('/users');
}

5. Caching & Revalidation

  • Static (default): Pages cached at build time
  • Dynamic: export const dynamic = 'force-dynamic'
  • ISR: export const revalidate = 60 (seconds)
  • On-demand: revalidatePath() or revalidateTag()
  • unstable_cache: Cache function results with tags

6. Middleware

// middleware.ts
export function middleware(request: NextRequest) {
  // Auth checks, redirects, geolocation, A/B testing
  if (!isAuthenticated(request)) {
    return NextResponse.redirect(new URL('/login', request.url));
  }
}

export const config = {
  matcher: ['/dashboard/:path*', '/api/:path*'],
};

7. Performance Patterns

  • Use loading.tsx for instant loading states
  • Parallel data fetching with Promise.all
  • Streaming with Suspense boundaries
  • Image optimization with next/image
  • Font optimization with next/font
  • Metadata API for SEO

8. Deployment

  • Vercel: Zero-config, automatic ISR, edge functions
  • Self-hosted: next start with Node.js, Docker support
  • Static export: output: 'export' for CDN hosting

Anti-Patterns

  • Don't fetch data in Client Components (use Server Components)
  • Don't put "use client" at the layout level (pushes everything to client)
  • Don't use route handlers for Server Component data (just fetch directly)
  • Don't skip loading.tsx (bad UX without it)

Related Skills

Get the best new skills
in your inbox

Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.