Next.js Development Guidelines
Project Structure
This project uses Next.js 16.1.1 with the App Router. Key conventions:
- •App Directory: Use the
app/directory for all routes and layouts - •File-based Routing: Routes are created using the file system in the
app/directory - •Layouts: Use
layout.tsxfiles for shared UI across routes - •Pages: Use
page.tsxfiles to create route segments - •Server Components by Default: All components are Server Components unless marked with
'use client'
Component Guidelines
Server vs Client Components
- •Default to Server Components: Most components should be Server Components for better performance
- •Use Client Components (
'use client') only when needed for:- •Interactive features (onClick, onChange, etc.)
- •Browser APIs (localStorage, window, etc.)
- •React hooks (useState, useEffect, useContext, etc.)
- •Third-party libraries that require client-side JavaScript
Component Patterns
typescript
// Server Component (default)
export default function ServerComponent() {
return <div>Server Component</div>;
}
// Client Component
'use client';
import { useState } from 'react';
export default function ClientComponent() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
Routing
- •Dynamic Routes: Use
[param]folders for dynamic segments (e.g.,app/users/[id]/page.tsx) - •Route Groups: Use
(folderName)for organization without affecting the URL - •Nested Routes: Create nested layouts by adding
layout.tsxin subdirectories - •Loading States: Use
loading.tsxfor route-level loading UI - •Error Boundaries: Use
error.tsxfor route-level error handling - •Not Found: Use
not-found.tsxfor custom 404 pages
Data Fetching
- •Server Components: Fetch data directly in Server Components using async/await
- •No useEffect for Data: Don't use useEffect for data fetching in Server Components
- •API Routes: Create API routes in
app/api/directory usingroute.tsfiles - •Caching: Leverage Next.js caching with
fetchoptions (cache,revalidate, etc.)
typescript
// Server Component data fetching
export default async function Page() {
const data = await fetch('https://api.example.com/data', {
next: { revalidate: 3600 } // Revalidate every hour
});
const json = await data.json();
return <div>{json.title}</div>;
}
Image Optimization
- •Always use
next/image: Import fromnext/imagefor optimized images - •Required Props: Always provide
width,height, andaltprops - •Priority: Use
priorityprop for above-the-fold images - •Responsive Images: Use
fillwith a container for responsive images
typescript
import Image from 'next/image';
<Image
src="/image.jpg"
alt="Description"
width={500}
height={300}
priority // for above-the-fold images
/>
Metadata and SEO
- •Metadata Export: Use the
metadataexport inlayout.tsxorpage.tsx - •Dynamic Metadata: Use
generateMetadatafunction for dynamic metadata - •Type Safety: Import
Metadatatype fromnext
typescript
import type { Metadata } from 'next';
export const metadata: Metadata = {
title: 'Page Title',
description: 'Page description',
};
Styling
- •Tailwind CSS: This project uses Tailwind CSS v4
- •CSS Modules: Can be used alongside Tailwind if needed
- •Global Styles: Add global styles in
app/globals.css - •Dark Mode: Support dark mode using Tailwind's
dark:variant
TypeScript
- •Type Safety: Leverage TypeScript for all components and functions
- •Props Types: Define proper types for component props
- •Route Params: Type route parameters using
paramsprop types - •Search Params: Type search parameters using
searchParamsprop types
typescript
// Typed route params
export default function UserPage({
params,
}: {
params: { id: string };
}) {
return <div>User {params.id}</div>;
}
Best Practices
- •
Performance:
- •Use Server Components by default
- •Implement proper image optimization
- •Leverage Next.js caching strategies
- •Use dynamic imports for large client components
- •
Code Organization:
- •Keep components in logical directories
- •Use colocation for related files
- •Create reusable components in shared directories
- •
Error Handling:
- •Use
error.tsxfor error boundaries - •Implement proper error states in components
- •Handle loading states with
loading.tsx
- •Use
- •
API Routes:
- •Use
route.tsfiles inapp/api/directory - •Export named functions (GET, POST, etc.) for HTTP methods
- •Return proper Response objects
- •Use
typescript
// app/api/users/route.ts
export async function GET() {
return Response.json({ users: [] });
}
export async function POST(request: Request) {
const body = await request.json();
return Response.json({ created: body });
}
- •Environment Variables:
- •Use
.env.localfor local development - •Prefix client-side variables with
NEXT_PUBLIC_ - •Access server-side variables directly
- •Use
Common Patterns
- •Layouts: Create nested layouts for shared UI across route segments
- •Templates: Use
template.tsxfor UI that needs to remount on navigation - •Parallel Routes: Use
@foldersyntax for parallel routes - •Intercepting Routes: Use
(.)foldersyntax for intercepting routes
Development Commands
- •
npm run dev- Start development server - •
npm run build- Build for production - •
npm run start- Start production server - •
npm run lint- Run ESLint