nextjs
Installation
SKILL.md
Next.js
Overview
Next.js is a full-stack React framework featuring the App Router with Server Components, Server Actions for mutations, and multiple rendering strategies (SSG, SSR, ISR, PPR). It provides automatic code splitting, image optimization, and deployment options from Vercel to self-hosted Docker.
Instructions
- When creating routes, use file-based routing in the
app/directory withpage.tsxfor pages,layout.tsxfor persistent layouts,loading.tsxfor streaming, anderror.tsxfor error boundaries. - When building components, default to Server Components (no directive needed) for zero client-side JavaScript, and add
"use client"only for components needing event handlers, hooks, or browser APIs. - When fetching data, query databases directly in Server Components with
async/await, usefetch()with caching options (revalidate,force-cache), and co-locate data fetching with the component that needs it. - When handling mutations, use Server Actions with
"use server"directive and<form action={...}>for progressive enhancement, then callrevalidatePath()orrevalidateTag()after mutations. - When choosing rendering, default to ISR with
revalidatefor most pages, usegenerateStaticParams()for fully static pages, anddynamic = "force-dynamic"only when fresh data is required on every request. - When adding middleware, use
middleware.tsat the project root for auth redirects, geolocation, and A/B testing with matcher config to scope it to specific routes. - When optimizing, use
next/imagefor all images (WebP/AVIF, lazy loading),next/fontfor zero layout shift fonts, andgenerateMetadata()for dynamic SEO.
Examples
Example 1: Build a dashboard with Server Components
Related skills