JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsnextjs
Prev

Learn the concept

Rendering Strategies

nextjs
junior
rendering

What's the difference between SSR and SSG in Next.js?

nextjs
ssr
ssg
rendering
static-generation
server-side
use-cache
Quick Answer

SSG (Static Site Generation) generates HTML at build time for fast, cacheable pages; SSR (Server-Side Rendering) generates HTML on each request for dynamic, real-time content.

Detailed Explanation

Static Site Generation (SSG):

  • HTML generated at build time
  • Pages are pre-rendered and cached
  • Served from CDN (fastest)
  • Best for: blogs, docs, marketing pages
  • Data fetched once at build

Server-Side Rendering (SSR):

  • HTML generated on each request
  • Fresh data on every page load
  • Runs on server for each visitor
  • Best for: personalized content, real-time data
  • Higher server load

App Router (Recommended): In the App Router, components are Server Components by default. Rendering strategy is determined by data access patterns:

  • Static (SSG): No dynamic data access → pre-rendered at build time
  • Dynamic (SSR): Accessing cookies(), headers(), searchParams, or connection() → rendered per request
  • Cached: Use "use cache" directive to explicitly cache component output

Choosing Between Them:

  • Can content be pre-rendered? → Static (default)
  • Does content change per user/request? → Dynamic
  • Need fresh data with caching control? → "use cache" with cacheLife()

Incremental Static Regeneration (ISR):

  • Regenerate static pages after deployment using revalidate
  • Time-based or on-demand revalidation

Note: In Next.js 16, fetch requests are not cached by default. Caching is opt-in via the "use cache" directive.

Code Examples

App Router — Static and Dynamic renderingTSX
// app/posts/[id]/page.tsx — App Router (recommended)

type Props = {
  params: Promise<{ id: string }>;
};

// Static Generation — use generateStaticParams
export async function generateStaticParams() {
  const posts = await fetch('https://api.example.com/posts').then(r => r.json());
  return posts.map((post: { id: number }) => ({
    id: post.id.toString(),
  }));
}

export default async function PostPage({ params }: Props) {
  const { id } = await params;
  const post = await fetch(`https://api.example.com/posts/${id}`);
  return <article>{post.title}</article>;
}

// For dynamic (SSR) — access dynamic APIs
import { cookies } from 'next/headers';

export default async function DashboardPage() {
  const cookieStore = await cookies(); // Makes this route dynamic
  const token = cookieStore.get('token');
  const data = await fetchDashboard(token);
  return <div>{data.content}</div>;
}

Real-World Applications

Use Cases

News Website with ISR

Statically generate article pages at build time, then use ISR to refresh them periodically. Breaking news pages use dynamic rendering for real-time updates.

Personalized Dashboard

Use SSR (dynamic rendering via `cookies()`) for user-specific dashboards that show personalized data, recommendations, and settings on every request.

Marketing Pages with SSG

Pre-render landing pages, pricing, and feature pages at build time for instant loads and CDN caching. Content teams update via CMS with on-demand revalidation.

Mini Projects

Rendering Strategy Showcase

beginner

Build an app with three pages demonstrating each rendering strategy: a static About page (SSG), a dynamic User Profile page (SSR via cookies), and a cached Products page (`"use cache"` with `cacheLife`). Show timestamps to prove when each was rendered.

Hybrid E-Commerce Store

intermediate

Build a store where product listing pages are statically generated with `generateStaticParams`, product detail pages use `"use cache"` with hourly revalidation, and the shopping cart uses dynamic rendering for personalization.

Industry Examples

Tripadvisor

Tripadvisor uses a hybrid rendering approach — static generation for destination guides and SEO pages, with server-side rendering for personalized search results and user reviews.

Washington Post

The Washington Post uses incremental static regeneration for news articles, allowing pages to be served from CDN while automatically updating when new content is published.

Resources

Rendering Fundamentals

docs

Static and Dynamic Rendering

docs

Related Questions

Explain the different rendering strategies in Next.js (SSR, SSG, ISR) and when to use each approach.

senior
rendering

How does data fetching work in Next.js?

mid
data-fetching
Previous
What is next/image and why should you use it over the regular img tag?
Prev