The Evolution of Rendering in React: From Client-Side to Server Components

 

🚀 The Evolution of Rendering in React: From Client-Side to Server Components

React has evolved massively in how it renders pages, moving from Client-Side Rendering (CSR) to Server Components (RSC) to improve performance, SEO, and user experience.

Let's break this down step by step:


🔥 1️⃣ Client-Side Rendering (CSR) – The Beginning

📌 What is CSR?

  • In Client-Side Rendering (CSR), the browser downloads a bare HTML file and a JavaScript bundle.
  • React runs entirely in the browser, generating the UI dynamically.

📌 How it Works?

  1. Browser downloads an empty HTML file.
  2. Loads a large JavaScript bundle containing React code.
  3. React renders components dynamically and updates the DOM.

📌 Example: CSR in React (Create React App)


function App() { return <h1>Hello, world!</h1>; } export default App;
  • The entire app loads and renders in the browser.
  • No content is present in the initial HTML file.

📌 Challenges of CSR:
Slow First Page Load – A blank page is shown until JavaScript loads.
Bad for SEO – Search engines see an empty HTML file.
Not optimized for performance – Large JavaScript bundles make apps slow.

📌 How to Fix? → Server-Side Rendering (SSR) & Static Site Generation (SSG)


2️⃣ Server-Side Rendering (SSR) & Static Site Generation (SSG)

To fix CSR’s slow loading times & SEO issues, React introduced Server-Side Rendering (SSR) & Static Site Generation (SSG).


⚡ Server-Side Rendering (SSR)

📌 What is SSR?

  • In Server-Side Rendering (SSR), the server renders React components into HTML before sending them to the browser.
  • The user sees the content immediately without waiting for JavaScript to load.

📌 How it Works?

  1. Browser requests a page → The server renders the React components into HTML.
  2. HTML is sent to the browser, so users see content immediately.
  3. JavaScript hydrates the page to add interactivity.

📌 Example: Next.js SSR


export async function getServerSideProps() { const res = await fetch("https://jsonplaceholder.typicode.com/posts/1"); const data = await res.json(); return { props: { data } }; } export default function Page({ data }) { return <h1>{data.title}</h1>; }
  • The server fetches data and generates HTML before sending it to the browser.

📌 Challenges of SSR:
Server Load – Rendering every request on the server can be slow.
Latency – Users wait for the server to respond before seeing anything.


📌 Static Site Generation (SSG) – Faster Alternative to SSR

📌 What is SSG?

  • Pre-renders pages at build time instead of on every request.
  • The HTML is cached and served instantly, making it super fast.

📌 How it Works?

  1. Pages are generated at build time (not on every request).
  2. Static HTML is stored on a CDN for instant loading.

📌 Example: Next.js SSG


export async function getStaticProps() { const res = await fetch("https://jsonplaceholder.typicode.com/posts/1"); const data = await res.json(); return { props: { data } }; } export default function Page({ data }) { return <h1>{data.title}</h1>; }

✔ Faster than SSR because HTML is pre-generated.
✔ Great for SEO and performance.

📌 Challenges of SSG:
Not real-time – The page is static until the next deployment.
Not dynamic – You need revalidation or API calls for fresh data.

📌 How to Fix? → Suspense for SSR


🚀 3️⃣ Suspense for Server-Side Rendering (SSR) – Faster Streaming

📌 What is Suspense SSR?

  • Allows progressive streaming – sends HTML as soon as it’s ready instead of waiting for everything.
  • Uses React Suspense to show loading states only where needed.

📌 How it Works?

  1. The server streams HTML chunks to the browser.
  2. Skeleton UI appears first, and then the data loads dynamically.

📌 Example: Streaming SSR with Suspense


import { Suspense } from "react"; function Page() { return ( <div> <h1>My Page</h1> <Suspense fallback={<p>Loading...</p>}> <UserProfile /> </Suspense> </div> ); }

Faster page load times.
Improves perceived performance.

📌 Challenges of Suspense SSR:
Still requires client-side JavaScript for interactivity.

📌 How to Fix? → Server Components (RSC)


🎉 4️⃣ Server Components (RSC) – The Best of Both Worlds

📌 What are Server Components?

  • React can now run components directly on the server, removing unnecessary client-side JavaScript.
  • No need to hydrate everything – only interactive parts need JavaScript.

📌 How it Works?

  1. The server renders UI components, including database calls.
  2. The browser receives only minimal JavaScript (only for interactive parts).

📌 Example: Server Component (.server.js)


// Runs ONLY on the server export default async function ServerComponent() { const res = await fetch("https://jsonplaceholder.typicode.com/posts/1"); const data = await res.json(); return <h1>{data.title}</h1>; }

No client-side JavaScript needed – everything is processed on the server.
Smaller bundles → Faster loading pages.

📌 Challenges of Server Components:
No access to useState, useEffect (since it runs on the server).
Need client components for interactivity.

📌 Solution? → Mix Server & Client Components!


🔄 5️⃣ Mixing Server & Client Components (Best Practice)

📌 Why use both?

  • Server Components for fast, efficient rendering & data fetching.
  • Client Components for interactivity like buttons & forms.

📌 Example: Mixing Both

// Server Component import ClientComponent from "./ClientComponent"; export default async function ServerComponent() { const res = await fetch("https://jsonplaceholder.typicode.com/posts/1"); const data = await res.json(); return ( <div> <h1>{data.title}</h1> <ClientComponent /> </div> ); } // Client Component "use client"; import { useState } from "react"; export default function ClientComponent() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>; }

Server Component fetches data efficiently.
Client Component handles interactivity.


🎯 Final Summary: Evolution of React Rendering

Rendering MethodProsCons
Client-Side Rendering (CSR)Interactive, dynamicSlow first load, bad for SEO
Server-Side Rendering (SSR)Better SEO, no blank screensSlower than SSG, server load
Static Site Generation (SSG)Fastest, CDN cachingNot real-time
Suspense SSRFaster streamingStill needs client-side JavaScript
Server Components (RSC)No client JavaScript neededNo useState, useEffect

🚀 By mixing Server & Client Components, we get the best performance!

Comments

Popular posts from this blog

JavaScript Must-Read Topics for Senior Developer Interviews

React.js Topics for a Mid-Senior Level Developer Interview Preparation

Must read Topics for Mid Level Node.js Developer Interview Preparation