DEV Community

Hongster
Hongster

Posted on

Server-Side Rendering (SSR) : Understand in 3 Minutes

Problem Statement

Server-Side Rendering (SSR) is a technique where a web page is rendered into HTML on the server and sent to the browser fully formed. You encounter this problem when your sleek, modern JavaScript application loads in the browser and users stare at a blank screen or a loading spinner before they can interact with anything. You might also hear from your marketing team that your app's pages aren't showing up properly in search engine results, even though the content is there.

Core Explanation

With standard Client-Side Rendering (CSR), your browser downloads a bare-bones HTML file and a mountain of JavaScript. It then executes all that JavaScript to build the page's structure and content. This process takes time, leading to the "white screen of waiting."

SSR flips this model. Here's how it works, step-by-step:

  • Initial Request: When you navigate to a URL, your browser requests that page from the server.
  • Server-Side Processing: The server doesn't send an empty shell. Instead, it runs the relevant application code (your React, Vue, etc.) on the server.
  • HTML Generation: The server executes the code to determine what the page should look like and renders it into a complete HTML document.
  • Delivery & Hydration: The server sends this fully populated HTML file to the browser, which can paint it immediately. Then, the browser downloads and executes the JavaScript bundle, which "takes over" the static page, attaching event handlers and making it interactive—a process called hydration.

Think of it like ordering at a restaurant. CSR is when the waiter brings you all the raw ingredients and a recipe—you have to cook the meal yourself before eating. SSR is when the kitchen (the server) cooks the meal and delivers it ready to eat. You can start eating (seeing content) immediately, even before you get the utensils (interactivity).

Practical Context

Use SSR when:

  • Search Engine Optimization (SEO) is critical. Search engine crawlers can easily read the fully rendered HTML.
  • You need fast initial page loads for content-heavy public pages (blog posts, marketing sites, product pages).
  • Users are on low-powered devices or slow networks, as the server does the heavy lifting upfront.

Avoid SSR when:

  • Your application is a highly interactive, authenticated dashboard (like an admin panel or internal tool). The complexity of SSR often outweighs the benefits here.
  • You have very limited server resources, as rendering on the server adds computational load.

Why should you care? SSR directly impacts core business metrics: it improves user-perceived performance, increases SEO rankings, and can boost conversion rates for public-facing sites. If you're building anything users or search engines need to see quickly, SSR is on the table.

Quick Example

Imagine a React component for a product page:

function ProductPage({ product }) {
  return (
    <div>
      <h1>{product.name}</h1>
      <img src={product.imageUrl} alt={product.name} />
      <p>{product.description}</p>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In a CSR app, the browser receives an <div id="root"></div> placeholder and must fetch data and run React to fill it in.

With SSR (using a Node.js server with Express), the server can render this component to a string before sending the page:

app.get('/product/:id', async (req, res) => {
  const product = await fetchProductData(req.params.id);
  const html = ReactDOMServer.renderToString(<ProductPage product={product} />);
  res.send(`
    <html>
      <body>
        <div id="root">${html}</div>
        <script src="/client-bundle.js"></script>
      </body>
    </html>
  `);
});
Enter fullscreen mode Exit fullscreen mode

The browser instantly receives the HTML containing the product's name, image, and description, displaying them before the client-bundle.js even loads.

Key Takeaway

Prioritize SSR when your page's initial content is its primary value, as it eliminates the rendering wait time for both users and search engines. For a practical, batteries-included way to implement SSR, explore a framework like Next.js.

Top comments (0)