Dự án Next.js 15 với cấu trúc chuẩn cho production.
src/
├── app/ # App Router - Routes & Layouts
│ ├── layout.tsx # Root layout
│ ├── page.tsx # Homepage
│ ├── loading.tsx # Loading UI (Suspense)
│ ├── error.tsx # Error boundary
│ ├── not-found.tsx # 404 page
│ └── globals.css # Global styles
│
├── components/ # React Components
│ ├── ui/ # Base UI components (Button, Input, Card...)
│ └── features/ # Feature-specific components
│
├── lib/ # Utilities & Configurations
│ ├── utils.ts # Helper functions (cn, formatDate...)
│ └── fonts.ts # Font configurations
│
├── types/ # TypeScript Types & Interfaces
│ └── index.ts # Shared types
│
├── hooks/ # Custom React Hooks
│ └── use-mounted.ts # Example hook
│
├── actions/ # Server Actions
│ └── example.ts # Server-side mutations
│
└── constants/ # Constants & Enums
└── index.ts # App-wide constants
- Mỗi folder = 1 route
page.tsx= route endpointlayout.tsx= shared layoutloading.tsx= loading state (Suspense)error.tsx= error boundary- Server Components by default
ui/- Components cơ bản, không logic nghiệp vụfeatures/- Components có logic nghiệp vụ cụ thể- Mỗi component 1 folder riêng nếu có nhiều files
- Helper functions thuần túy
- Configurations (fonts, API clients...)
- Không chứa React components
- Shared types/interfaces
- Tránh duplicate type definitions
- Export centralized
- Chỉ Client Components hooks
- Prefix với
use - Tái sử dụng logic
- Server-side mutations
- Form submissions
- Database operations
- Directive
'use server'
- App-wide constants
- Routes, API endpoints
- Magic numbers/strings
npm run dev- TypeScript strict mode - Không dùng
any - Server Components - Mặc định, chỉ thêm
'use client'khi cần - Tailwind CSS - Styling duy nhất
- cn() - Merge Tailwind classes
- Semantic HTML - Accessibility first
- Result pattern - Error handling có kiểu
- Thêm UI components vào
components/ui/ - Tạo routes mới trong
app/ - Viết Server Actions trong
actions/ - Thêm custom hooks vào
hooks/ - Define types trong
types/