Agent Skills - Phần 2: Next.js Best Practices
Tài liệu tổng hợp Next.js Best Practices dùng làm nguồn tham chiếu cho Agent Skills (ví dụ trong Cursor), bao gồm các chủ đề: Routing, Rendering, Data Fetching, Images, Fonts, Metadata, API, Middleware, Environment, Performance và Security. Mỗi guideline được đánh giá mức độ quan trọng và có ví dụ code cụ thể. Nội dung dựa trên bộ guideline từ ui-ux-pro-max-skill, giúp developers và AI Agents xây dựng ứng dụng Next.js hiệu quả và tuân thủ best practices.
Tài liệu này tổng hợp Next.js Best Practices dùng làm nguồn tham chiếu cho Agent Skills (ví dụ trong Cursor).
Nội dung dựa trên bộ guideline từ ui-ux-pro-max-skill — file nextjs.csv.
Cập nhật: 08/02/2026.
Mức độ: 🔴 Cao · 🟡 Trung bình · 🟢 Thấp
Ví dụ code: + nên · - tránh
1. Routing
1.1 🟡 Use App Router for new projects
App Router (app/) là cách tiếp cận được khuyến nghị cho Next.js 14+.
- Nên: Dùng
app/directory vớipage.tsxcho route mới.
+ app/dashboard/page.tsx
- Không nên: Bắt đầu project mới với
pages/.
- pages/dashboard.tsx
1.2 🟡 Use file-based routing
Routing nên hoàn toàn dựa trên cấu trúc file trong app/.
- Nên: Dùng
page.tsxcho route vàlayout.tsxcho layout.
+ app/blog/[slug]/page.tsx
- Không nên: Tự cấu hình router thủ công.
- Custom router setup
1.3 🟢 Colocate related files
Colocate component, styles, tests ngay cạnh route để dễ bảo trì.
- Nên: Đặt component liên quan cạnh
page.tsx.
+ app/dashboard/_components/
- Không nên: Một
components/dashboard/chung cho mọi nơi.
- components/dashboard/
1.4 🟢 Use route groups for organization
Route groups giúp tổ chức folder mà không ảnh hưởng URL.
- Nên: Dùng dấu ngoặc
()cho route groups.
+ (marketing)/about/page.tsx
- Không nên: Tạo folder thật làm thay đổi URL.
- marketing/about/page.tsx
1.5 🟡 Handle loading states with loading.tsx
Loading UI nên tách riêng bằng loading.tsx cho từng route.
- Nên: Tạo
loading.tsxcạnhpage.tsx.
+ app/dashboard/loading.tsx
- Không nên: Dùng
useStateloading trong chính page cho mọi thứ.
- useState for loading in page
1.6 🔴 Handle errors with error.tsx
Error nên được bắt ở cấp route bằng error.tsx.
- Nên: Tạo
error.tsxvới hàmresetđể retry.
+ app/dashboard/error.tsx
- Không nên:
try/catchlặp lại trong mọi component.
- try/catch in page component