Understanding how Next.js renders your pages is crucial for building fast, scalable, and SEO-friendly web applications. In this article, weβll explore Client-Side Rendering (CSR), Server-Side Rendering (SSR), Static Site Generation (SSG), and Incremental Static Regeneration (ISR) β their differences, pros/cons, and when to use them.
π Overview
Next.js offers four main rendering strategies:
- CSR (Client-Side Rendering): Rendering happens in the browser.
- SSR (Server-Side Rendering): HTML is rendered on each request.
- SSG (Static Site Generation): HTML is generated at build time.
- ISR (Incremental Static Regeneration): Pages are pre-built but can be revalidated after deployment.
Key Concepts
- Hydration: Turning static HTML into an interactive React app.
- FCP (First Contentful Paint): When first content appears.
- TTI (Time To Interactive): When page is fully usable.
- SEO: How search engines crawl and index your content.
β‘ Client-Side Rendering (CSR)
How it Works
- Minimal HTML is served.
- Browser downloads JavaScript.
- JavaScript fetches data and renders UI.
β Pros & β Cons
- β Great for interactivity
- β Light on the server
- β Poor SEO (blank HTML initially)
- β Slower first load
Example
"use client"
import { useState, useEffect } from 'react';
export default function CSRExample() {
const [posts, setPosts] = useState([]);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/posts').then(res => res.json()).then(setPosts);
}, []);
return (
<div>
<h1>Posts (CSR)</h1>
{posts.map(p => <p key={p.id}>{p.title}</p>)}
</div>
);
}
π Best For: Dashboards, private content, real-time apps.
π Server-Side Rendering (SSR)
How it Works
- Request comes in.
- Server fetches data.
- HTML is generated and sent.
- Browser hydrates the page.
β Pros & β Cons
- β Fresh data every request
- β Great SEO
- β Slower response time
- β Higher server load
Example
export async function callApi() {
const res = await fetch('https://jsonplaceholder.typicode.com/posts');
const posts = await res.json();
return posts
}
export default async function SSRExample() {
const posts = await callApi()
return <ul>{posts.map(p => <li key={p.id}>{p.title}</li>)}</ul>;
}
π Best For: E-commerce, news, personalized content.
π Static Site Generation (SSG)
How it Works
- At build time, Next.js generates HTML.
- Pages are cached on CDN.
- Super-fast delivery.
β Pros & β Cons
- β Fastest load times
- β Great SEO
- β Very scalable
- β Stale content until rebuild
Example
import { notFound } from "next/navigation";
async function getPost(id) {
const res = await fetch(
`https://jsonplaceholder.typicode.com/posts/${id}`,
);
if (!res.ok) {
return null;
}
return res.json();
}
export async function generateStaticParams() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts");
const posts = await res.json();
return posts.map((post) => ({
id: post.id.toString(),
}));
}
export default async function PostPage({ params }) {
const post = await getPost(params.id);
if (!post) {
notFound();
}
return (
<main>
<h1>{post.title}</h1>
<p>{post.body}</p>
</main>
);
}
π Best For: Blogs, docs, marketing pages.
π Incremental Static Regeneration (ISR)
How it Works
- Pages are generated at build time (like SSG).
- Pages are revalidated in the background.
- Next visitors get the fresh content.
β Pros & β Cons
- β Speed of SSG + freshness of SSR
- β No full rebuilds needed
- β Temporary stale content
Example
export const revalidate = 60
import { notFound } from "next/navigation";
async function getPost(id) {
const res = await fetch(
`https://jsonplaceholder.typicode.com/posts/${id}`,
);
if (!res.ok) {
return null;
}
return res.json();
}
export async function generateStaticParams() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts");
const posts = await res.json();
return posts.map((post) => ({
id: post.id.toString(),
}));
}
export default async function PostPage({ params }) {
const post = await getPost(params.id);
if (!post) {
notFound();
}
return (
<main>
<h1>{post.title}</h1>
<p>{post.body}</p>
</main>
);
}
π Best For: E-commerce catalogs, news sites, frequently updated blogs.
π Comparison Chart
| Feature | CSR | SSR | SSG | ISR |
|---|---|---|---|---|
| Initial Load | Slow | Medium | Fastest | Fast |
| SEO | Poor | Excellent | Excellent | Excellent |
| Freshness | High | High | Low | Medium |
| Server Load | Low | High | Very Low | Low |
| Best For | Apps | Dynamic pages | Blogs, docs | Hybrid needs |
π‘ When to Use What
- CSR: Dashboards, authenticated apps, real-time updates.
- SSR: SEO + fresh content, e-commerce, user-specific pages.
- SSG: Marketing sites, blogs, documentation.
- ISR: Large sites, e-commerce catalogs, news portals.
π― Exercises for You
- Pick a blog and implement SSG with dynamic routes.
- Create a dashboard with CSR (fetch on client).
- Build a news site homepage with ISR.
- Compare performance using Lighthouse.
π Final Thoughts
Each rendering strategy in Next.js serves different needs:
- CSR = Flexibility
- SSR = Real-time + SEO
- SSG = Performance
- ISR = Balance between speed and freshness
By mixing them wisely, you can build fast, SEO-friendly, and scalable apps.
Top comments (0)