Rendering Strategies

Loading concept...

πŸš€ 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:

  1. You click a news article
  2. Server fetches the latest comments
  3. Server builds the complete page
  4. 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:

  1. You write an article
  2. At build time, Next.js creates the HTML
  3. When visitors come, they get the pre-made page
  4. 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:

  1. Page is pre-built at build time
  2. Customer visits and gets fast page
  3. After 60 seconds, Next.js checks for updates
  4. 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:

  1. Browser loads empty page + JavaScript
  2. JavaScript fetches your personal data
  3. Browser builds what you see
  4. 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:

  1. SSR = Fresh meal every time (slower but always current)
  2. SSG = Pre-made meals (lightning fast but can be stale)
  3. ISR = Pre-made + auto-refresh (best of both worlds)
  4. 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! πŸš€

Loading story...

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Interactive content is coming soon.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Cheatsheet is coming soon.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Quiz is coming soon.

Flashcard Preview

Flashcard - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Flashcards are coming soon.