April 1, 2025

Static Site Generation (SSG) vs Incremental Static Regeneration (ISR) vs Server-Side Rendering (SSR) trong Next.js
So sánh các phương pháp render phổ biến trong Next.js và cách chọn phương pháp phù hợp cho dự án của bạn.
Giới thiệu
Next.js cung cấp ba phương pháp render chính: Static Site Generation (SSG), Incremental Static Regeneration (ISR) và Server-Side Rendering (SSR). Mỗi phương pháp có ưu nhược điểm riêng, ảnh hưởng đến hiệu suất, SEO và trải nghiệm người dùng.
Trong bài viết này, chúng ta sẽ so sánh chi tiết từng phương pháp để giúp bạn chọn lựa phù hợp cho dự án của mình.
1. Static Site Generation (SSG)
SSG tạo ra các trang tĩnh ngay tại thời điểm build. Khi người dùng truy cập, họ sẽ nhận được phiên bản HTML đã được tạo sẵn mà không cần xử lý thêm trên server.
Ưu điểm:
- Hiệu suất cao: Trang tải nhanh vì chỉ cần phục vụ file HTML tĩnh.
- SEO tốt: Nội dung đã có sẵn để các công cụ tìm kiếm lập chỉ mục.
- Giảm tải server: Không cần xử lý request mỗi lần có người truy cập.
Nhược điểm:
- Không phù hợp cho nội dung thay đổi thường xuyên.
- Cần build lại toàn bộ trang nếu có thay đổi dữ liệu.
2. Incremental Static Regeneration (ISR)
ISR là phiên bản nâng cấp của SSG, cho phép cập nhật nội dung tĩnh mà không cần build lại toàn bộ ứng dụng.
Ưu điểm:
- Hiệu suất cao như SSG.
- Có thể cập nhật nội dung động theo chu kỳ.
- Phù hợp cho blog, trang tin tức, danh sách sản phẩm...
Nhược điểm:
- Cập nhật không tức thời: Có độ trễ giữa lần cập nhật dữ liệu và lần request đầu tiên.
- Có thể gây lỗi hydration nếu dữ liệu thay đổi giữa server và client.
3. Server-Side Rendering (SSR)
SSR render trang trên server khi có request từ người dùng. Mỗi lần truy cập, server tạo HTML mới dựa trên dữ liệu mới nhất.
Ưu điểm:
- Dữ liệu luôn cập nhật mới nhất.
- Phù hợp với nội dung động, ví dụ trang cá nhân hóa hoặc bảng giá cập nhật liên tục.
- Hỗ trợ SEO tốt hơn so với Client-Side Rendering (CSR).
Nhược điểm:
- Hiệu suất thấp hơn SSG/ISR vì mỗi request phải render lại trên server.
- Tăng tải cho server.
- Tăng thời gian phản hồi so với SSG/ISR.
4. So sánh SSG, ISR và SSR
Phương pháp | Tốc độ tải trang | Cập nhật nội dung | SEO | Phù hợp với |
---|---|---|---|---|
SSG | 🚀 Rất nhanh | ❌ Cần build lại | ✅ Tốt | Blog, tài liệu, trang giới thiệu |
ISR | 🚀 Rất nhanh | 🔄 Cập nhật theo chu kỳ | ✅ Tốt | Trang tin tức, danh sách sản phẩm |
SSR | 🕒 Chậm hơn | ✅ Luôn cập nhật | ✅ Tốt | Trang cá nhân hóa, dữ liệu thay đổi liên tục |
5. Khi nào nên chọn SSG, ISR hay SSR?
- Dùng SSG nếu nội dung ít thay đổi và cần hiệu suất cao.
- Dùng ISR nếu cần cập nhật nội dung nhưng vẫn muốn giữ hiệu suất của SSG.
- Dùng SSR nếu dữ liệu thay đổi liên tục và cần hiển thị mới nhất cho mỗi request.
Kết luận
Việc chọn giữa SSG, ISR và SSR phụ thuộc vào yêu cầu của dự án. Nếu ưu tiên hiệu suất và SEO, SSG hoặc ISR là lựa chọn tốt. Nếu cần nội dung động cập nhật liên tục, SSR sẽ phù hợp hơn.
Static Site Generation (SSG) vs Incremental Static Regeneration (ISR) vs Server-Side Rendering (SSR) in Next.js
A comparison of popular rendering methods in Next.js and how to choose the right approach for your project.
Introduction
Next.js provides three main rendering methods: Static Site Generation (SSG), Incremental Static Regeneration (ISR), and Server-Side Rendering (SSR). Each method has its own pros and cons, impacting performance, SEO, and user experience.
In this article, we’ll compare these rendering strategies in detail to help you choose the right one for your project.
1. Static Site Generation (SSG)
SSG generates static HTML at build time. When a user visits the page, the server serves the pre-rendered HTML without further computation.
Advantages:
- High performance: Fast loading times due to serving static files.
- SEO-friendly: Content is readily available for search engine indexing.
- Low server load: No server-side processing on each request.
Disadvantages:
- Not ideal for frequently changing content.
- Requires a full rebuild for any content updates.
2. Incremental Static Regeneration (ISR)
ISR extends SSG by allowing you to update static content in the background without rebuilding the entire app.
Advantages:
- High performance similar to SSG.
- Supports dynamic content updates on a schedule.
- Great for blogs, news, product listings, etc.
Disadvantages:
- Updates are not immediate: There may be a delay between data changes and page updates.
- Hydration issues can occur if server and client content differ.
3. Server-Side Rendering (SSR)
SSR renders the page on the server upon each request. Every time a user visits the page, the server generates fresh HTML based on the latest data.
Advantages:
- Always up-to-date content.
- Ideal for dynamic pages like personalized dashboards or real-time pricing.
- Better SEO than purely client-side rendering (CSR).
Disadvantages:
- Slower performance than SSG/ISR due to server-side rendering on every request.
- Higher server load.
- Longer time-to-first-byte (TTFB) compared to SSG/ISR.
4. Comparison of SSG, ISR, and SSR
Method | Page Load Speed | Content Update | SEO | Best Use Cases |
---|---|---|---|---|
SSG | 🚀 Very fast | ❌ Requires rebuild | ✅ Excellent | Blogs, documentation, static pages |
ISR | 🚀 Very fast | 🔄 Scheduled updates | ✅ Excellent | News, product listings |
SSR | 🕒 Slower | ✅ Real-time updates | ✅ Excellent | Personalized content, live data |
5. When to choose SSG, ISR, or SSR?
- Use SSG for static content with infrequent updates and need for fast performance.
- Use ISR when content needs to update periodically but performance is still a priority.
- Use SSR when you need fresh, real-time content on every request.
Conclusion
Choosing between SSG, ISR, and SSR depends on your project’s needs. If performance and SEO are top priorities, SSG or ISR are ideal. If your app requires real-time data updates, SSR is the way to go.