What is client-side rendering in Next JS?

Next JS is a powerful React framework that allows us to build server-side rendered (SSR) and client-side rendered (CSR) applications with ease. In this Answer, we will focus on client-side rendering in Next JS.

Client-side rendering

Client-side rendering (CSR) is a rendering technique where the web page’s content is generated and displayed on the client-side (i.e., the browser) using JavaScript. In contrast to server-side rendering (SSR), where the server generates the HTML and sends it to the client, CSR allows the browser to request the raw data from the server and render it into the final content on the user’s device. It allows the browser to generate and render content using JavaScript, providing dynamic updates without full page reloads.

Client-Side Rendering
Client-Side Rendering

Advantages

  • Improved user experience: CSR provides faster page loads and smoother interactions since the initial rendering is done on the client’s device without waiting for server processing.

  • Reduced server load: With CSR, the server only sends raw data, reducing the load on the server compared to SSR, where the server has to generate the entire HTML.

  • Dynamic content: CSR allows you to fetch and render data on-the-fly, enabling dynamic content updates without refreshing the entire page.

Use cases

  • Real-time data: Applications that require real-time data updates, such as social media feeds or live chat, can benefit from CSR.

  • Interactive user interfaces: CSR is ideal for interactive applications where content changes rapidly based on user interactions.

  • Personalized dashboards: CSR is commonly used to build personalized dashboards that display user-specific data.

Drawbacks

Here are the drawbacks of client-side rendering in Next JS:

  1. Slower initial page load time: Client-side rendering requires the browser to download the JavaScript bundle and execute it, resulting in slower initial page load times, especially on slower devices or networks.

  2. SEO concerns: Search engine crawlers may struggle to index and understand the content on CSR pages, potentially affecting search engine rankings.

  3. Dependency on JavaScript: Client-side rendering heavily relies on JavaScript execution in the browser, which can lead to issues on browsers with no JavaScript support.

  4. Performance on low-end devices: CSR can be resource-intensive, causing slower performance on low-end devices or older browsers.

  5. Security risks: Client-side rendering can introduce security risks, such as cross-site scripting (XSS) vulnerabilities, if not handled properly.

  6. Increased memory usage: The browser's memory usage increases with client-side rendering due to JavaScript execution.

Implementation

Let's now implement a simple example to better understand CSR.

Setup and project initialization

Before starting, ensure you have Node js and npm installed on your system. Create a new Next JS project using the following commands:

npx create-next-app my-csr-app
cd my-csr-app

Your project structure should be like this.

Next JS project structure
Next JS project structure

Creating a simple CSR page

Create a new file index.js inside the pages directory:

import React, { useState } from 'react';

const CSRExamplePage = () => {
  const [count, setCount] = useState(0);

  const handleIncrement = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <h1>Client-Side Rendering Example</h1>
      <p>Count: {count}</p>
      <button onClick={handleIncrement}>Increment</button>
    </div>
  );
};

export default CSRExamplePage;

In the code above, we’ve created a simple page with a counter that increments when the increment button is clicked. This counter is entirely managed on the client-side.

Testing

Now, let's see how to check whether the page is using client-side rendering (CSR) or not. Follow these steps to check.

  • Load the page: Open the webpage in your browser.

  • Observe initial load: Watch how the page loads. If there is a slight delay before all the content appears, it's likely CSR. This is because a minimal HTML document is sent first, and JavaScript then fills in the rest of the content.

  • Inspect page source: Right-click anywhere on the webpage and select "View Page Source" or "Inspect Element". The resulting HTML should be very minimal for a CSR page, often with a few div elements and a lot of JavaScript. This JavaScript is responsible for rendering the rest of the page.

  • Monitor network traffic: Right-click on the webpage, select "Inspect Element", and then navigate to the "Network" tab. Refresh the page. The network traffic shows all the files the page is loading. If you see the page loading many JavaScript files, it's likely CSR.

  • Check dynamic content: CSR pages will frequently update content dynamically based on user interactions. In your provided code, for instance, the count value increases dynamically when the "Increment" button is clicked.

Conclusion

Client-side rendering can greatly improve performance, provide real-time updates, and create more engaging user interfaces. However, it’s essential to carefully consider when and where to use CSR in your Next JS applications, as SSR might still be more suitable for certain cases.

Copyright ©2024 Educative, Inc. All rights reserved