MongoDB

Understanding Server-Side Rendering (SSR), Static Site Generation (SSG), and Client-Side Rendering (CSR)

Learn the differences between SSR, SSG, and CSR in modern web development and when to use each for faster and scalable web applications.

D

DevBucket

Understanding SSR, SSG, and CSR in Modern Web Development

Modern web applications use different rendering techniques to deliver content quickly and efficiently. The three most common rendering methods are:

  • Client-Side Rendering (CSR)
  • Server-Side Rendering (SSR)
  • Static Site Generation (SSG)

Understanding these approaches helps developers build faster and more scalable web applications.


1. Client-Side Rendering (CSR)

In Client-Side Rendering, the browser downloads a minimal HTML file along with JavaScript. The JavaScript then builds the UI inside the browser.

How CSR Works

  1. Browser requests a webpage
  2. Server sends a basic HTML file
  3. JavaScript loads in the browser
  4. JavaScript fetches data from APIs
  5. UI is rendered dynamically

Example

import { useEffect, useState } from "react";

export default function Posts() {
  const [posts, setPosts] = useState([]);

  useEffect(() => {
    fetch("/api/posts")
      .then(res => res.json())
      .then(data => setPosts(data));
  }, []);

  return (
    <div>
      {posts.map(post => (
        <p key={post.id}>{post.title}</p>
      ))}
    </div>
  );
}

Advantages

  • Highly interactive user interfaces
  • Smooth user experience
  • Reduced server processing

Disadvantages

  • Slower initial page load
  • Poor SEO performance
  • Requires JavaScript enabled in the browser

Frameworks like React primarily use CSR by default.


2. Server-Side Rendering (SSR)

In Server-Side Rendering, the server generates the complete HTML page for each request and sends it to the browser.

How SSR Works

  1. User requests a page
  2. Server fetches the required data
  3. Server generates full HTML
  4. Browser displays ready-to-render content

Example in Next.js

export async function getServerSideProps() {
  const res = await fetch("https://api.example.com/posts");
  const posts = await res.json();

  return {
    props: { posts }
  };
}

Advantages

  • Better SEO
  • Faster first content paint
  • Content is available immediately

Disadvantages

  • Higher server load
  • Slower response if server traffic is high

SSR is useful for:

  • E-commerce websites
  • News websites
  • Content-heavy platforms

3. Static Site Generation (SSG)

In Static Site Generation, HTML pages are generated during build time rather than during request time.

How SSG Works

  1. Project is built by the developer
  2. Static HTML pages are generated
  3. Pages are deployed to a CDN
  4. Users receive prebuilt pages instantly

Example in Next.js

export async function getStaticProps() {
  const res = await fetch("https://api.example.com/posts");
  const posts = await res.json();

  return {
    props: { posts }
  };
}

Advantages

  • Extremely fast performance
  • CDN friendly
  • Excellent SEO
  • Low server costs

Disadvantages

  • Requires rebuilding the site when content changes
  • Not suitable for frequently updated data

SSG is best for:

  • Blogs
  • Documentation websites
  • Marketing pages
  • Landing pages

SSR vs SSG vs CSR Comparison

FeatureCSRSSRSSG
Initial LoadSlowFastVery Fast
SEOPoorGoodExcellent
Server LoadLowHighVery Low
Best Use CaseWeb AppsDynamic SitesStatic Content

When to Use Each Rendering Method

Use CSR when

  • Building dashboards
  • Creating highly interactive apps
  • SEO is not important

Use SSR when

  • Content changes frequently
  • SEO is important
  • Pages must always show fresh data

Use SSG when

  • Content rarely changes
  • Maximum performance is required
  • You want global CDN distribution

Conclusion

Choosing the right rendering strategy is important for building modern web applications.

  • CSR works best for interactive applications.
  • SSR improves SEO and dynamic content delivery.
  • SSG provides the fastest performance with prebuilt pages.

Frameworks like Next.js allow developers to combine all three approaches depending on the page requirements.

Share this article