Next.js App Router principles. Server Components, data fetching, routing patterns.
Add this skill
npx mdskills install sickn33/nextjs-best-practicesComprehensive Next.js App Router reference with clear decision trees and organized best practices
1---2name: nextjs-best-practices3description: Next.js App Router principles. Server Components, data fetching, routing patterns.4allowed-tools: Read, Write, Edit, Glob, Grep5---67# Next.js Best Practices89> Principles for Next.js App Router development.1011---1213## 1. Server vs Client Components1415### Decision Tree1617```18Does it need...?19│20├── useState, useEffect, event handlers21│ └── Client Component ('use client')22│23├── Direct data fetching, no interactivity24│ └── Server Component (default)25│26└── Both?27 └── Split: Server parent + Client child28```2930### By Default3132| Type | Use |33|------|-----|34| **Server** | Data fetching, layout, static content |35| **Client** | Forms, buttons, interactive UI |3637---3839## 2. Data Fetching Patterns4041### Fetch Strategy4243| Pattern | Use |44|---------|-----|45| **Default** | Static (cached at build) |46| **Revalidate** | ISR (time-based refresh) |47| **No-store** | Dynamic (every request) |4849### Data Flow5051| Source | Pattern |52|--------|---------|53| Database | Server Component fetch |54| API | fetch with caching |55| User input | Client state + server action |5657---5859## 3. Routing Principles6061### File Conventions6263| File | Purpose |64|------|---------|65| `page.tsx` | Route UI |66| `layout.tsx` | Shared layout |67| `loading.tsx` | Loading state |68| `error.tsx` | Error boundary |69| `not-found.tsx` | 404 page |7071### Route Organization7273| Pattern | Use |74|---------|-----|75| Route groups `(name)` | Organize without URL |76| Parallel routes `@slot` | Multiple same-level pages |77| Intercepting `(.)` | Modal overlays |7879---8081## 4. API Routes8283### Route Handlers8485| Method | Use |86|--------|-----|87| GET | Read data |88| POST | Create data |89| PUT/PATCH | Update data |90| DELETE | Remove data |9192### Best Practices9394- Validate input with Zod95- Return proper status codes96- Handle errors gracefully97- Use Edge runtime when possible9899---100101## 5. Performance Principles102103### Image Optimization104105- Use next/image component106- Set priority for above-fold107- Provide blur placeholder108- Use responsive sizes109110### Bundle Optimization111112- Dynamic imports for heavy components113- Route-based code splitting (automatic)114- Analyze with bundle analyzer115116---117118## 6. Metadata119120### Static vs Dynamic121122| Type | Use |123|------|-----|124| Static export | Fixed metadata |125| generateMetadata | Dynamic per-route |126127### Essential Tags128129- title (50-60 chars)130- description (150-160 chars)131- Open Graph images132- Canonical URL133134---135136## 7. Caching Strategy137138### Cache Layers139140| Layer | Control |141|-------|---------|142| Request | fetch options |143| Data | revalidate/tags |144| Full route | route config |145146### Revalidation147148| Method | Use |149|--------|-----|150| Time-based | `revalidate: 60` |151| On-demand | `revalidatePath/Tag` |152| No cache | `no-store` |153154---155156## 8. Server Actions157158### Use Cases159160- Form submissions161- Data mutations162- Revalidation triggers163164### Best Practices165166- Mark with 'use server'167- Validate all inputs168- Return typed responses169- Handle errors170171---172173## 9. Anti-Patterns174175| ❌ Don't | ✅ Do |176|----------|-------|177| 'use client' everywhere | Server by default |178| Fetch in client components | Fetch in server |179| Skip loading states | Use loading.tsx |180| Ignore error boundaries | Use error.tsx |181| Large client bundles | Dynamic imports |182183---184185## 10. Project Structure186187```188app/189├── (marketing)/ # Route group190│ └── page.tsx191├── (dashboard)/192│ ├── layout.tsx # Dashboard layout193│ └── page.tsx194├── api/195│ └── [resource]/196│ └── route.ts197└── components/198 └── ui/199```200201---202203> **Remember:** Server Components are the default for a reason. Start there, add client only when needed.204
Full transparency — inspect the skill content before installing.