Nextjs Best Practices

davila7/claude-code-templates/cli-tool/components/skills/development/nextjs-best-practices

by davila78da17d671b6fNo license32K starsListed Oct 8, 2026Updated Oct 8, 2026Repository updated today

Next.js App Router principles. Server Components, data fetching, routing patterns.

Instructions onlySoftware Development
AI-generated overview

Reference guidance on Next.js App Router best practices, covering Server and Client Components, data fetching, routing, caching and performance.

What it does
Provides a structured set of Next.js App Router principles organized into sections such as Server versus Client Components, data fetching patterns, routing conventions, API route handlers, performance, metadata, caching, server actions and anti-patterns. It presents decision trees, comparison tables and a sample project structure as reference material. It produces guidance rather than code or files.
When to use it
Use when planning or reviewing a Next.js App Router codebase and needing conventions for component boundaries, data fetching, routing or caching. Also useful as a checklist when refactoring toward server-first patterns.
Requirements
No scripts or tooling; it is instructions only. The agent needs read, write, edit, glob and grep tools per its declared allowed tools.

Next.js Best Practices

Principles for Next.js App Router development.


1. Server vs Client Components

Decision Tree

Does it need...?│├── useState, useEffect, event handlers│   └── Client Component ('use client')│├── Direct data fetching, no interactivity│   └── Server Component (default)│└── Both?     └── Split: Server parent + Client child

By Default

TypeUse
ServerData fetching, layout, static content
ClientForms, buttons, interactive UI

2. Data Fetching Patterns

Fetch Strategy

PatternUse
DefaultStatic (cached at build)
RevalidateISR (time-based refresh)
No-storeDynamic (every request)

Data Flow

SourcePattern
DatabaseServer Component fetch
APIfetch with caching
User inputClient state + server action

3. Routing Principles

File Conventions

FilePurpose
page.tsxRoute UI
layout.tsxShared layout
loading.tsxLoading state
error.tsxError boundary
not-found.tsx404 page

Route Organization

PatternUse
Route groups (name)Organize without URL
Parallel routes @slotMultiple same-level pages
Intercepting (.)Modal overlays

4. API Routes

Route Handlers

MethodUse
GETRead data
POSTCreate data
PUT/PATCHUpdate data
DELETERemove data

Best Practices

  • Validate input with Zod
  • Return proper status codes
  • Handle errors gracefully
  • Use Edge runtime when possible

5. Performance Principles

Image Optimization

  • Use next/image component
  • Set priority for above-fold
  • Provide blur placeholder
  • Use responsive sizes

Bundle Optimization

  • Dynamic imports for heavy components
  • Route-based code splitting (automatic)
  • Analyze with bundle analyzer

6. Metadata

Static vs Dynamic

TypeUse
Static exportFixed metadata
generateMetadataDynamic per-route

Essential Tags

  • title (50-60 chars)
  • description (150-160 chars)
  • Open Graph images
  • Canonical URL

7. Caching Strategy

Cache Layers

LayerControl
Requestfetch options
Datarevalidate/tags
Full routeroute config

Revalidation

MethodUse
Time-basedrevalidate: 60
On-demandrevalidatePath/Tag
No cacheno-store

8. Server Actions

Use Cases

  • Form submissions
  • Data mutations
  • Revalidation triggers

Best Practices

  • Mark with 'use server'
  • Validate all inputs
  • Return typed responses
  • Handle errors

9. Anti-Patterns

❌ Don't✅ Do
'use client' everywhereServer by default
Fetch in client componentsFetch in server
Skip loading statesUse loading.tsx
Ignore error boundariesUse error.tsx
Large client bundlesDynamic imports

10. Project Structure

app/├── (marketing)/     # Route group│   └── page.tsx├── (dashboard)/│   ├── layout.tsx   # Dashboard layout│   └── page.tsx├── api/│   └── [resource]/│       └── route.ts└── components/    └── ui/

Remember: Server Components are the default for a reason. Start there, add client only when needed.

Source and attribution

Source:davila7/claude-code-templatesincli-tool/components/skills/development/nextjs-best-practicesat commit8da17d6

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal

Nextjs Best Practices · cli-tool/components/skills/development/nextjs-best-practices Agent Skill | SourceWeft