Title: Understanding SSR, SSG, and ISR in Next.js – What, Why, and When to Use
If you’ve been exploring Next.js, you’ve likely come across terms like SSR, SSG, and ISR. These are different rendering strategies that can drastically affect your app’s performance, SEO, and scalability. In this blog, we’ll dive into what these strategies mean, how they work, and when to use each of them.
1. SSR (Server-Side Rendering)
What is SSR?
SSR stands for Server-Side Rendering. In this strategy, the HTML for a page is generated on the server at the time of each request. This means the user always gets the most updated content.
How it works:
When a user visits the page, Next.js runs a function (getServerSideProps
) on the server to fetch data and render the HTML. That freshly generated page is then sent to the browser.
Example use case:
export async function getServerSideProps(context) {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return {
props: { data },
};
}
When to use SSR:
- Your content changes frequently or in real-time.
- You need up-to-date data for every page request.
- Your page depends on user-specific data (like user dashboards or authenticated content).
- SEO is important, and the content must always be fresh.
2. SSG (Static Site Generation)
What is SSG?
Static Site Generation means that your HTML is generated at build time and served as a static file. Once the page is built, it remains unchanged until you rebuild your application.
How it works:
Next.js uses getStaticProps
to fetch data during the build process. The rendered HTML is saved and served to all users from the CDN—making it extremely fast.
Example use case:
export async function getStaticProps() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return {
props: { data },
};
}
When to use SSG:
- Your content doesn’t change often (e.g., blogs, documentation, landing pages).
- You want the fastest page load times.
- You want to maximize scalability with static hosting.
- SEO is important, and you can work with slightly older data.
3. ISR (Incremental Static Regeneration)
What is ISR?
ISR is a hybrid between SSG and SSR. It allows you to build static pages at build time, and then regenerate them periodically in the background without rebuilding the whole site.
How it works:
Pages are generated like SSG, but you can set a revalidate
interval. After that interval, the next request will trigger a regeneration of the page in the background. The newly updated page is then served to future users.
Example use case:
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
};
}
When to use ISR:
- Your content updates occasionally, but not every second.
- You want the speed of SSG with the flexibility of updates.
- You’re fetching data from a CMS like WordPress, Strapi, or Contentful.
- You want a balance between performance and data freshness.
Quick Comparison
Rendering Method | HTML Generation Time | Best For | Performance | SEO |
---|---|---|---|---|
SSR | On every request | Real-time, dynamic content | Medium | Excellent |
SSG | At build time | Static content, blogs | Fastest | Excellent |
ISR | Build time + background update | CMS content, product pages | Fast + flexible | Excellent |
Real-World Examples
- A blog using content from a CMS like Strapi or Contentful → ISR
- A pricing page that rarely changes → SSG
- A real-time analytics dashboard → SSR
- A news site homepage → ISR or SSR depending on update frequency
Final Thoughts
Next.js gives you the power to choose the best rendering method per page. You’re not restricted to one option — you can combine them based on the needs of your application.
To recap:
- Use SSR when content is highly dynamic and real-time.
- Use SSG when content is mostly static and performance is key.
- Use ISR when content changes periodically and you want to avoid full rebuilds.
Choosing the right strategy ensures your app is fast, scalable, and SEO-friendly, giving your users the best experience possible.
Top comments (0)