Client-Side Rendering (CSR)

Client-Side Rendering (CSR)

October 4, 2024

renderingcsr

What is Client side rendering

Client-side rendering (CSR) is a method where web page content is rendered directly in the browser using JavaScript. Instead of receiving a fully rendered page from the server, the browser gets an almost empty HTML document and fetches the necessary JavaScript to render the page dynamically. The content is then displayed on the user's device after the JavaScript code has executed.

How It Works:

  • Initial Request: When the browser sends the first request, the server sends back an HTML file that mostly contains links to JavaScript files (e.g., React, Vue).
  • Rendering: Once the browser receives the files, JavaScript executes, fetching any additional data (typically through APIs or other asynchronous methods), and renders the web page content dynamically within the browser.

napkin-selection%20(1).png

Example By Reactjs

Here’s a simple example of how CSR might look using React (a common framework for client-side rendering):

import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';

function App() {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch('/api/data')
      .then((response) => response.json())
      .then((result) => setData(result));
  }, []);

  if (!data) return <div>Loading...</div>;

  return (
    <div>
      <h1>Data from API</h1>
      <p>{data.message}</p>
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById('root'));

In this example:

  • The server sends back minimal HTML with an empty div and JavaScript files.
  • React fetches the data from /api/data and renders it dynamically in the browser.

mov1_lpohhw.mov

Pros of Client Side Rendering:

  1. Fast User Interactions: Once the initial JavaScript is loaded, interactions (e.g., navigation between pages) are fast because they happen within the browser without requiring a new page load from the server.
  2. Rich User Experience: CSR enables rich, interactive, and responsive web applications with real-time data updates and complex animations.
  3. Reduced Server Load: The server only serves the initial HTML shell and API data, reducing its workload, as all the rendering is handled by the client.
  4. Single-Page Applications (SPAs): Perfect for SPAs, where different parts of a web app need to update dynamically without reloading the entire page.

napkin-selection%20(2).png

Cons of Client Side Rendering:

  1. Initial Load Time: The initial page load is often slower because the browser must download and execute JavaScript before displaying any content.
  2. SEO Challenges: Search engines have difficulty crawling CSR sites because the content is loaded dynamically, which can impact SEO performance, though modern search engines are improving in rendering JavaScript.
  3. Heavy Reliance on JavaScript: If a user’s browser does not support JavaScript or has it disabled, the page will not work correctly.
  4. Poor Performance on Low-End Devices: Since rendering is handled on the client-side, users with slower devices or browsers may experience performance issues.

napkin-selection%20(3).png

Technologies Supporting CSR:

  1. React – One of the most popular JavaScript libraries for building user interfaces, widely used for CSR.
  2. Vue.js – Another popular framework known for its simplicity and flexibility.
  3. Angular – A framework that supports building large-scale client-side rendered applications.
  4. Svelte – A modern framework that compiles components to highly efficient imperative code for client-side rendering.
  5. Next.js (can support CSR) – Although Next.js is known for server-side rendering (SSR), it can also support CSR through dynamic imports and client-side fetching.

Each of these tools offers libraries or modules to fetch data from APIs, render components on the client side, and manage the dynamic content loading efficiently.


Client-side rendering is well-suited for modern, interactive web apps like dashboards, social media platforms, and other apps that prioritize user interactivity over fast initial load times or SEO.