Chuyển đến nội dung
Skillpedia
Website và giao diện (frontend)48k · #5301

nextjs-best-practices

Skill này làm gì

Hướng dẫn này dạy cách dùng Next.js, một framework để xây dựng website và ứng dụng web, theo các thực hành tốt nhất. Nó giải thích cách chia mã giữa máy chủ và trình duyệt, cách lấy dữ liệu và tổ chức trang.

Khi nào nên dùng

  • ✓Khi bắt đầu một dự án Next.js mới.
  • ✓Khi tái cấu trúc một dự án hiện có để cải thiện hiệu năng.
  • ✓Khi tìm hiểu về component phía máy chủ và phía client.

Liên kết skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/nextjs-best-practices/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/nextjs-best-practices/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm sao để tổ chức các trang trong dự án Next.js?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Next.js chia công việc giữa máy chủ (nơi trang được dựng) và trình duyệt (nơi người dùng tương tác). Nó dùng các quy ước về tệp và mẫu cụ thể để tối ưu tốc độ và trải nghiệm người dùng.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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

Type Use
Server Data fetching, layout, static content
Client Forms, buttons, interactive UI

2. Data Fetching Patterns

Fetch Strategy

Pattern Use
Default Static (cached at build)
Revalidate ISR (time-based refresh)
No-store Dynamic (every request)

Data Flow

Source Pattern
Database Server Component fetch
API fetch with caching
User input Client state + server action

3. Routing Principles

File Conventions

File Purpose
page.tsx Route UI
layout.tsx Shared layout
loading.tsx Loading state
error.tsx Error boundary
not-found.tsx 404 page

Route Organization

Pattern Use
Route groups (name) Organize without URL
Parallel routes @slot Multiple same-level pages
Intercepting (.) Modal overlays

4. API Routes

Route Handlers

Method Use
GET Read data
POST Create data
PUT/PATCH Update data
DELETE Remove 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

Type Use
Static export Fixed metadata
generateMetadata Dynamic per-route

Essential Tags

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

7. Caching Strategy

Cache Layers

Layer Control
Request fetch options
Data revalidate/tags
Full route route config

Revalidation

Method Use
Time-based revalidate: 60
On-demand revalidatePath/Tag
No cache no-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' everywhere Server by default
Fetch in client components Fetch in server
Skip loading states Use loading.tsx
Ignore error boundaries Use error.tsx
Large client bundles Dynamic 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.

When to Use

This skill is applicable to execute the workflow or actions described in the overview.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
Câu hỏi thường gặp

Server Components và Client Components là gì?

Server Components chạy trên máy chủ và phù hợp để lấy dữ liệu. Client Components chạy trên trình duyệt và dùng cho tương tác, như nút bấm và biểu mẫu.

Next.js lấy dữ liệu như thế nào?

Bạn có thể lấy dữ liệu tĩnh (lúc build), cập nhật định kỳ hoặc mỗi lần có yêu cầu, tùy theo nhu cầu của bạn.

“Server Actions” là gì?

Đây là các hàm chạy trên máy chủ để thực hiện các hành động như gửi biểu mẫu hoặc cập nhật dữ liệu, một cách an toàn và hiệu quả.

Chi tiết

Mô tả gốc của tác giả

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

repo: sickn33/agentic-awesome-skills

Độ phổ biến

Sao GitHub
48k
Vị trí xếp hạng
#5301
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự