SSR (Server-Side Rendering) and ISR (Incremental Static Regeneration) are both strategies for rendering pages in Next.js, but they have key differences in when and how pages are generated and served.
Here's a detailed comparison between SSR and ISR:
π’ Server-Side Rendering (SSR)
SSR generates the page on every request. When a user visits a page, Next.js fetches data, generates the HTML on the server, and sends it back to the client.
-
Rendering Time: On-demand, at the time of the request.
-
Use Case: When you need up-to-date content on every request, like for dynamic data that changes frequently (e.g., news websites, e-commerce product pages).
- Next.js uses
getServerSideProps
to enable SSR.
- Next.js uses
Example of SSR:
// pages/example.js
export async function getServerSideProps() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return {
props: {
data,
},
};
}
const ExamplePage = ({ data }) => (
<div>
<h1>Server-Side Rendered Page</h1>
<p>{data.info}</p>
</div>
);
export default ExamplePage;
- π Latency: Longer since the page is generated for every request.
- π Reusability: No caching out of the box; data is fetched every time, leading to more server load.
- βοΈ SEO: Good for SEO since HTML is delivered fully rendered.
π΅ Incremental Static Regeneration (ISR)
ISR allows you to statically generate pages at build time, but with the ability to regenerate them on-demand in the background when a user visits, based on a specified time interval. This provides the benefit of static site generation (SSG) while keeping content up-to-date.
-
Rendering Time: Pages are pre-generated at build time and regenerated after a specified time interval.
-
Use Case: For pages that require occasional updates but don't need to be re-rendered on every request, such as blogs, documentation, or content-heavy sites.
- Next.js uses
getStaticProps
withrevalidate
to enable ISR.
- Next.js uses
Example of ISR:
// pages/example.js
export async function getStaticProps() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return {
props: {
data,
},
revalidate: 60, // Regenerate the page every 60 seconds
};
}
const ExamplePage = ({ data }) => (
<div>
<h1>Incrementally Static Regenerated Page</h1>
<p>{data.info}</p>
</div>
);
export default ExamplePage;
- π Latency: Very low after initial generation, since the content is cached as static HTML. Once regenerated, the next user gets the fresh static version.
- π Reusability: ISR caches the page for the duration of the
revalidate
interval. The next request within that interval gets the cached version. - βοΈ SEO: Excellent for SEO as the HTML is served statically but updated as necessary.
π Key Differences Between SSR and ISR
Feature | SSR (Server-Side Rendering) | ISR (Incremental Static Regeneration) |
---|---|---|
Page Generation Time | On every request | On demand, after initial static generation |
Data Freshness | Always up-to-date (since it fetches per request) | Fresh after the revalidate interval |
Speed of Serving Pages | Slower (re-generated each time) | Faster (serves pre-built static pages) |
Server Load | Higher (rebuild on every request) | Lower (regenerates in the background) |
Use Case | Dynamic, constantly updating content | Content that updates infrequently but needs to stay relatively fresh |
Example | News sites, e-commerce product pages | Blogs, marketing pages, documentation |
SEO | Good for SEO | Excellent for SEO (since pages are static) |
Caching | No automatic caching | Automatic caching, controlled by revalidate |
Next.js Methods Used | getServerSideProps | getStaticProps with revalidate |
π Visualization of Differences:
ββββββββββββββ βββββββββββββββ
β User A β β User B β
ββββββ¬ββββββββ ββββββ¬βββββββββ
β β
βΌ βΌ
ββββββββββββββββββββ ββββββββββββββββββββββββ
β Server-Side Renderβ β Incremental Static β
β Every Request β β Revalidate after N β
β `SSR` β β seconds (ISR) β
ββββββββββββββββββββ ββββββββββββββββββββββββ
β² β
β βΌ
Server Load Static Cached Pages
- SSR: The page is rendered each time a user requests it, leading to higher server load but fresh data on every request.
- ISR: The page is pre-rendered once at build time, and then periodically updated after a specified revalidation period, reducing server load and improving performance while maintaining freshness.
Conclusion:
- Use SSR when you need to fetch data that must be up-to-date on every request (e.g., live data, frequently updated content).
- Use ISR when your content updates infrequently or has a predictable update cycle, as it allows you to serve fast static pages while still keeping content relatively fresh.