Back to the logbook

Understanding Client-side Rendering and Server-side Rendering

4 min readFirst published on Medium
  • server-side-rendering
  • client-side-rendering
  • front-end-development
  • nextjs

Introduction

As a seasoned frontend engineer, you’ve likely traversed the expansive terrain of web development and encountered the enigmatic terms ‘client-side rendering’ (CSR) and ‘server-side rendering’ (SSR). Pause for a moment and ask yourself: ‘Can I confidently elucidate what CSR and SSR entail, and perhaps more importantly, distinguish the two?’

If your answer is a resounding ‘YES,’ then pat yourself on the back. You’re already on your way to mastering these concepts. However, if the answer leans toward the uncertain side, rest assured, you’re in the right place. Join us on a journey to demystify CSR and SSR, and emerge with a comprehensive understanding of their intricacies. But before we dive into these concepts, let’s take a step back and explore the fascinating evolution of front-end development.

The Evolution of Frontend Development

Frontend development has traversed an intriguing evolutionary path, significantly impacting how we render and interact with web content. To understand the current landscape of Client-Side Rendering (CSR) and Server-Side Rendering (SSR), it’s essential to revisit the origins of web rendering.

Server-Side Rendering (SSR): The Early Days

In the nascent days of the web, rendering primarily occurred on the server-side. Web servers were responsible for crafting fully-rendered HTML pages, which were sent to the browser for display. This server-centric rendering approach resulted in consistent page content but limited interactivity.

Client-Side Rendering (CSR): The Rise of Interactivity

The web’s evolution witnessed the rise of Client-Side Rendering (CSR) with the advent of JavaScript and AJAX. In CSR, the browser takes center stage, dynamically rendering content on the client-side. This shift brought about rich, interactive web applications.

A Return to Server-Side Rendering (SSR): Balancing Act

In recent years, frontend development has come full circle with a resurgence of Server-Side Rendering (SSR). The push for SSR arises from the need for better search engine optimization (SEO), improved initial page load times, and the ability to provide consistent content to users with varying devices and capabilities. Modern frameworks, such as Next.js and Nuxt.js, make SSR approachable.

Having examined the historical context and workflow behind Server-Side Rendering (SSR), Client-Side Rendering (CSR), and the resurgence of SSR, we are now well-prepared to dive deeper into each rendering method. In the following sections, we’ll explore the intricacies of both CSR and SSR. By the end of this exploration, you’ll have a comprehensive understanding of when and how to employ these techniques effectively in your web development projects.

Client-Side Rendering (CSR)

Client-side rendering, often synonymous with “single-page applications” (SPAs), fundamentally relies on rendering web content within the user’s browser. In CSR, the client’s web browser plays a central role in rendering content, offering a seamless and responsive user experience. To comprehend how CSR works, let’s break down the process step by step:

  1. Initial Load: When a user accesses/requests a web page, the browser retrieves a minimal HTML page from the server, this initial HTML typically includes references to CSS styles and JavaScript files but lacks the main content.
  2. Dynamic Content: Now once the initial HTML is loaded, JavaScript takes over. the javascript is often in the form of frontend frameworks or libraries, it sends requests to the server to fetch data necessary for the next step. These requests are commonly made through API calls.
  3. Rendering: With the data in hand, JavaScript dynamically assembles the content on the client-side. This process involves manipulating the Document Object Model (DOM), which is a representation of the web page’s structure in the browser, the newly created content is then seamlessly displayed to the user, appearing as if it were there from the start. Let’s delve into the mechanics of CSR with illustrative code examples:

In the CSR example above:

  • The process begins when a user accesses a web page that includes the index.html file. The browser initiates an HTTP request to the server to fetch this HTML file.
  • The index.html file is a minimal HTML structure, including a <head> section that specifies the page title and a <body> section where the content will be displayed. Notably, it includes a <div> element with the id attribute set to "app" and a reference to an external JavaScript file, "app.js," using a <script> tag.
  • The JavaScript code in app.js takes over once the HTML file is loaded in the browser.
  • In the code, a reference to an HTML element with the id "app" is obtained using document.getElementById('app'). This element is where the dynamic content will be inserted.
  • Next, the script simulates data that would typically be fetched from an API. It creates a JavaScript object named data with a message property.
  • The final step involves rendering the dynamic content to the web page. The JavaScript code uses the root element obtained earlier and inserts HTML content into it.
  • In this case, it sets the content of the <div> with the id "app" to an <h1> element with the message from the data object, resulting in the text "Hello, Client-Side Rendering!" being displayed on the web page.

The result is that the user sees a web page with the message “Hello, Client-Side Rendering!” displayed as a heading (an <h1> element). This entire process occurs on the client-side, without requiring a full page reload, allowing for a dynamic and interactive user experience. The key takeaway is that CSR shifts most rendering tasks to the client.

Server-Side Rendering (SSR)

Contrastingly, Server-Side Rendering (SSR) is a method of rendering web content where the server is responsible for generating fully-rendered HTML pages before delivering them to the client’s browser. Here’s a direct breakdown of how SSR works:

  1. Initial Request: When a user accesses a web page implemented with SSR, their browser sends a request to the server.
  2. Server Processing: The server receives the request and processes it. This may involve fetching data from a database or an external source.
  3. HTML Generation: With the necessary data at hand, the server generates the complete HTML content for the web page. This HTML includes the page’s structure, content, and any initial data.
  4. Sending HTML to the Browser: Once the HTML is generated, the server sends it back to the user’s browser as a response to the initial request.

5. Initial Display: The browser receives the fully-rendered HTML and displays it immediately. This results in a faster initial page load, as the user sees the content without waiting for additional client-side rendering.

6. Hydration (Optional): If the web application requires interactivity or additional dynamic content, JavaScript code can be sent alongside the initial HTML. This JavaScript “hydrates” the page, meaning it attaches event listeners and enhances interactivity without fully re-rendering the page. This step is optional in SSR.

In essence, SSR focuses on generating complete web pages on the server-side, which allows for faster initial rendering and better search engine optimization. While SSR offers advantages, it also introduces some complexities in terms of server load and infrastructure, but it remains a valuable approach in various web development scenarios.

When to use either of them?

I’m confident that you now have a clear understanding of when to choose between CSR and SSR. However, if any doubts remain, here’s a concise guide to help you determine when each approach is most suitable:

  1. Use CSR when:
  • You want to build highly interactive web applications, like social media platforms or online gaming.
  • You prioritize faster navigation within your app.
  • SEO is not a primary concern, or you can employ techniques like server-side rendering for critical pages.

2. Use SSR when:

  • You need optimal SEO performance and content discoverability.
  • Fast initial page load time is crucial, especially for content-rich websites.
  • You have content that is publicly accessible and needs to be indexed by search engines.

Conclusion

Understanding the differences between Client-Side Rendering and Server-Side Rendering is essential for web developers. Both approaches have their advantages and use cases, and the choice between them should align with your project’s specific needs. By selecting the right rendering method, you can deliver an optimal user experience and achieve your desired web application functionality.

Message in a bottle

Hiring, building something, or want to argue about your FPL captain? Write it, cork it, throw it. It washes up in my inbox, usually within a tide.

0/1200