Server-Side Rendering

SSR Workflow:

  1. Browser requests a page like https://example.com.
  2. Browser sends a GET request to the server.
  3. Server generates HTML by execute the app code (server framework):
    1. Runs the template engine or framework logic.
    2. Fetches necessary data from databases or APIs.
    3. Generates the full HTML page with all content filled in.
<!DOCTYPE html>
<html>
  <head>
    <title>My Blog</title>
  </head>
  <body>
    <h1>Latest Posts</h1>
    <ul>
      <li>Alice's Post</li>
      <li>Bob's Post</li>
      <li>Charlie's Post</li>
    </ul>
  </body>
</html>
  1. Browser receives HTML, then parses the HTML and renders the page immediately.
  2. User now sees the fully built, static page. (not interactive yet)
  3. Browser downloads JS (CSR code).
  4. To make it dynamic page (like a SPA. interactive), do hydration.
    1. JS “attaches” to server-rendered HTML to make it interactive on the client side.
    2. It binds JS logic to the HTML elements, so everything becomes interactive.
  5. Page now become fully interactive (no visual flicker).

Advantages of SSR

  1. Faster initial page load
  2. SEO-friendly