π Next.js Rendering Strategies: The Restaurant Kitchen Story
Imagine you run a restaurant. How you prepare food for customers can make or break their experience. Next.js rendering strategies work exactly like different kitchen styles!
π― The Big Picture
Think of a webpage as a meal and the server as the kitchen.
| Kitchen Style | Next.js Strategy | When Food is Ready |
|---|---|---|
| Cook when ordered | SSR | Fresh per customer |
| Pre-cook everything | SSG | Ready before doors open |
| Pre-cook + refresh periodically | ISR | Ready + updated regularly |
| Customer cooks at table | CSR | Made at the table |
π³ Server-Side Rendering (SSR)
What Is It?
SSR = Cook fresh for every customer
When someone visits your page, the server cooks the entire meal (builds the HTML) right then and serves it hot.
Simple Example
Imagine a news website:
- You click a news article
- Server fetches the latest comments
- Server builds the complete page
- You see everything instantly!
// pages/news.js
export async function getServerSideProps() {
// This runs on EVERY request
const news = await fetch('api/latest-news')
return { props: { news } }
}
When to Use SSR?
β Use when:
- Data changes frequently (stocks, news)
- Content is personalized per user
- You need real-time information
β Avoid when:
- Content rarely changes
- Speed is critical (SSR is slower)
The Trade-off
| Pros | Cons |
|---|---|
| Always fresh data | Slower (cooks every time) |
| Great for SEO | More server work |
| Real-time content | Higher costs |
πͺ Static Site Generation (SSG)
What Is It?
SSG = Pre-cook everything before opening
The kitchen prepares ALL meals before the restaurant opens. When customers arrive, food is served instantly!
Simple Example
Think of a blog post:
- You write an article
- At build time, Next.js creates the HTML
- When visitors come, they get the pre-made page
- Super fast! No waiting!
// pages/blog/[slug].js
export async function getStaticProps({ params }) {
// Runs at BUILD time only
const post = await getPost(params.slug)
return { props: { post } }
}
export async function getStaticPaths() {
// Which pages to pre-build
return {
paths: [
{ params: { slug: 'hello-world' } },
{ params: { slug: 'intro-nextjs' } }
],
fallback: false
}
}
When to Use SSG?
β Use when:
- Content rarely changes (blog, docs)
- Same content for everyone
- Speed is critical
β Avoid when:
- Data changes often
- Content is user-specific
The Trade-off
| Pros | Cons |
|---|---|
| Lightning fast | Stale until rebuild |
| Cheap to host | Build takes time |
| Best for SEO | Not for live data |
π Incremental Static Regeneration (ISR)
What Is It?
ISR = Pre-cook + refresh the menu periodically
This is the best of both worlds! Pre-cook meals, but update them automatically every few minutes.
Simple Example
An e-commerce product page:
- Page is pre-built at build time
- Customer visits and gets fast page
- After 60 seconds, Next.js checks for updates
- Next visitor gets the fresh version!
// pages/products/[id].js
export async function getStaticProps({ params }) {
const product = await getProduct(params.id)
return {
props: { product },
revalidate: 60 // Refresh every 60 seconds!
}
}
The Magic of revalidate
revalidate: 60 means:
βββββββββββββββββββββββββββββββββββββββ
β 0-60 sec: Serve cached page β
β After 60s: Rebuild in background β
β Next visitor: Gets fresh page β
βββββββββββββββββββββββββββββββββββββββ
When to Use ISR?
β Use when:
- Content updates periodically (prices, inventory)
- You want speed + freshness
- Building all pages takes too long
β Avoid when:
- Data must be real-time (stocks)
- Content is user-specific
The Trade-off
| Pros | Cons |
|---|---|
| Fast like SSG | Not truly real-time |
| Fresh like SSR | Slight delay for updates |
| Easy to set up | Needs static hosting support |
π¨ Client-Side Rendering (CSR)
What Is It?
CSR = Let the customer cook at their table
The kitchen sends ingredients and a recipe. The customerβs browser cooks the meal!
Simple Example
A dashboard with user data:
- Browser loads empty page + JavaScript
- JavaScript fetches your personal data
- Browser builds what you see
- Interactive immediately!
// components/Dashboard.js
import { useState, useEffect } from 'react'
function Dashboard() {
const [data, setData] = useState(null)
useEffect(() => {
// Runs in the BROWSER
fetch('/api/my-data')
.then(res => res.json())
.then(setData)
}, [])
if (!data) return <p>Loading...</p>
return <div>{data.name}'s Dashboard</div>
}
When to Use CSR?
β Use when:
- Page is behind login
- Data is user-specific
- SEO doesnβt matter
- Highly interactive features
β Avoid when:
- SEO is important
- Users have slow devices
- Initial load speed matters
The Trade-off
| Pros | Cons |
|---|---|
| Very interactive | Slow initial load |
| No server needed | Bad for SEO |
| User-specific data | βLoadingβ¦β spinners |
πΊοΈ Decision Flowchart
graph TD A["New Page Needed"] --> B{SEO Important?} B -->|No| C["CSR"] B -->|Yes| D{Data Changes?} D -->|Never| E["SSG"] D -->|Sometimes| F["ISR"] D -->|Every Request| G["SSR"]
π― Quick Comparison
| Strategy | Build Time | Request Time | Freshness |
|---|---|---|---|
| SSG | β Generated | Served | Stale |
| SSR | β Skip | Generated | Fresh |
| ISR | β Generated | Served + Refresh | Balanced |
| CSR | β Skip | Browser builds | Fresh |
π‘ Real-World Examples
| Website Type | Best Strategy | Why? |
|---|---|---|
| Blog | SSG | Content rarely changes |
| News site | SSR | Fresh articles needed |
| E-commerce | ISR | Prices update, but not per-second |
| Dashboard | CSR | User-specific, behind login |
| Marketing page | SSG | Never changes, needs speed |
| Stock ticker | SSR | Real-time data critical |
π§ββοΈ Pro Tips
Mixing Strategies
You can use different strategies for different pages!
/ β SSG (marketing page)
/blog β SSG (articles)
/products β ISR (prices update)
/dashboard β CSR (user data)
/news β SSR (live updates)
Fallback for ISR
When someone visits a page that wasnβt pre-built:
export async function getStaticPaths() {
return {
paths: [...],
fallback: 'blocking'
// or true, or false
}
}
| Fallback | Behavior |
|---|---|
false |
404 if not pre-built |
true |
Show loading, then page |
blocking |
Wait, then show page |
π You Did It!
You now understand the four cooking styles of Next.js:
- SSR = Fresh meal every time (slower but always current)
- SSG = Pre-made meals (lightning fast but can be stale)
- ISR = Pre-made + auto-refresh (best of both worlds)
- CSR = Cook in browser (great for private, interactive stuff)
Pick the right strategy for each page, and your Next.js app will be fast, fresh, and fantastic! π
