Lädt...


🔧 The difference of writing CSR, SSR and SSG in Next.js App Router and Page Router.


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Table of Contents

  1. At the beginning
  2. What is CSR, SSR, SSG?
  3. Conclusion

At the beginning

When I first studied CSR, SSR and SSG, I struggled to understand these concepts. Recently, the introduction of App Router, which include different writing styles, has made it even more challenging. Additionally, there are still many articles that mix up these concepts.
I wrote this article to clearly the difference between them.

Client-Side Rendering(CSR)

  • Rendering HTML on the client side.
  • Receive empty HTML, CSS, JavaScript from the server side, and then create HTML using initial data.
  • The page is slow because of data acquisition and creating HTML on the client side each time.
  • Use only pages where you want to update data on the client side depending on user operation, etc.

In Next.js, there are two ways we can implement client-side rendering.

  1. Using React hooks like useEffect(), useState().
  2. Using a data fetching library SWR.

Here is an example.

'use client'

import React, { useEffect, useState } from "react";

const CsrPage = () => {
  const [products, setProducts] = useState<Product[]>([]);

  const fetchData = async () => {
    const res = await fetch("https://dummyjson.com/products");
    const data = await res.json();

    setProducts(data.products);
  };

  useEffect(() => {
    fetchData();
  }, []);

  return (
    <>
      <h3>Built with CSR</h3>
      <br />
      <ul>
        {products.map((product) => (
          <li key={product.id}>
            {product.id}: {product.title}
          </li>
        ))}
      </ul>
    </>
  );
};

export default CsrPage;

In page router, we don't need to use use client on the top of file  because there is no concept of client component and server component.

The process flow of CSR

Image description

Server-Side Rendering(SSR)

  • Rendering HTML on the server side, not on the client side.
  • Create HTML on the server side and return to the client after the server receives the request and retrieves the necessary data.
  • The page is fast because the server generates only the necessary pages, and the browser just displays them.
  • Use SSR only on pages where SSG can't(the website with content that changes for each user).

In SSR, we use { cache: "no-store" } in the second argument of fetch(). And don't use useEffect(), useState().

Here is an example.

import React from "react";

const SsrPage = () => {
  const res = await fetch("https://dummyjson.com/products", {
    cache: "no-store",
  });
  const data = await res.json();

  return (
    <>
      <h3>Built with SSR</h3>
      <br />
      <ul>
        {data.products.map((product) => (
          <li key={product.id}>
            {product.id}: {product.title}
          </li>
        ))}
      </ul>
    </>
  );
};

export default SsrPage;

In page router, we need to export an async function called getServerSideProps. But In App Router, don't use getServerSideProps.

The process flow of SSR

Image description

Static Site Generation(SSG)

  • Complete the HTML construction of the entire site, including data fetching with the API, on the server side in advance of the build process.
  • Just return this file when requested.
  • The page is fast by having the page cached in the CDN.
  • SEO-friendly

In SSG, we use { cache: "force-cache" } in the second argument of fetch() (Default value { cache: 'force-cache' }, so it can be omitted). And don't use useEffect(), useState().

Here is an example.

import React from "react";

const SsgPage = () => {
  const res = await fetch("https://dummyjson.com/products", {
    cache: "force-cache",  // we can be omitted
  });
  const data = await res.json();

  return (
    <>
      <h3>Built with SSG</h3>
      <br />
      <ul>
        {data.products.map((product) => (
          <li key={product.id}>
            {product.id}: {product.title}
          </li>
        ))}
      </ul>
    </>
  );
};

export default SsgPage;

In page router, we need to export an async function called getStaticProps. But In App Router, don't use getStaticProps.

The process flow of SSG

Image description

Conclusion

This article can be covered CSR, SSR and SSG that beginners should understand at first. In addition to them, there is also ISR to render the web page. I hope this article will serve as a first step for those new to Next.js.

...

🔧 The difference of writing CSR, SSR and SSG in Next.js App Router and Page Router.


📈 113.68 Punkte
🔧 Programmierung

🔧 How to understand the concepts of Next.js such as CSR , SSR, SSG, ISR, RSC, SPA, and Streaming SSR?


📈 86.59 Punkte
🔧 Programmierung

🔧 A Deep dive into CSR, SSR, SSG and ISR


📈 62.31 Punkte
🔧 Programmierung

🔧 The Ultimate Guide to Web Rendering: Improving Performance with CSR, SSR, SSG, and ISR


📈 62.31 Punkte
🔧 Programmierung

🔧 Exploring Web Rendering Strategies: A Deep Dive into CSR, SSR, SSG and ISG


📈 62.31 Punkte
🔧 Programmierung

🔧 What do SSR, CSR, ISR and SSG mean? A complete guide for web developers


📈 62.31 Punkte
🔧 Programmierung

🔧 CSR vs SSR vs SSG


📈 61.36 Punkte
🔧 Programmierung

🔧 Understanding the difference between Static Site Generation (SSG) and Server Side Rendering (SSR)


📈 52.2 Punkte
🔧 Programmierung

🔧 Server-Side Rendering (SSR) vs. Client-Side Rendering (CSR): The Fascinating World of Page Rendering


📈 47.04 Punkte
🔧 Programmierung

🔧 Mastering Next.js: My Journey from SSG to SSR Through Trial and Error


📈 46.86 Punkte
🔧 Programmierung

🔧 Understanding the Differences: Server Side Rendering (SSR) vs Static Site Generation (SSG) in Next.js


📈 45.91 Punkte
🔧 Programmierung

🔧 SSR 🆚 SSG: A Game-Changer for Your Web App ☄️


📈 42.89 Punkte
🔧 Programmierung

🔧 SSR 🆚 SSG: A Game-Changer for Your Web App ☄️


📈 42.89 Punkte
🔧 Programmierung

🔧 What are Hydration, CSR, and SSR in React and NextJS?


📈 41.64 Punkte
🔧 Programmierung

🔧 Understanding Next.js Page Routing vs App Routing and SSR Changes in Next.js 14


📈 41.53 Punkte
🔧 Programmierung

🔧 What is Server Side Rendering (SSR) and Static Site Generation (SSG)?


📈 40.86 Punkte
🔧 Programmierung

🔧 Understanding SSR and SSG in Modern Web Development


📈 40.86 Punkte
🔧 Programmierung

🔧 Basic Differences Between SSR and SSG


📈 40.86 Punkte
🔧 Programmierung

🔧 ASTRO JS | P2 | SSG and SSR


📈 40.86 Punkte
🔧 Programmierung

🔧 The Junior Developer's Complete Guide to SSR, SSG and SPA


📈 40.86 Punkte
🔧 Programmierung

🔧 🔍 SSR vs. CSR: Understanding the Differences and When to Use Them


📈 40.68 Punkte
🔧 Programmierung

🔧 SSR and CSR Explained


📈 40.68 Punkte
🔧 Programmierung

🔧 Angular 17, does SSR/SSG really work?


📈 39.91 Punkte
🔧 Programmierung

🔧 SSG vs SSR


📈 39.91 Punkte
🔧 Programmierung

🔧 Server Side Rendering in JavaScript – SSR vs CSR Explained


📈 39.73 Punkte
🔧 Programmierung

🔧 Understanding Server-Side Rendering (SSR) vs. Client-Side Rendering (CSR)


📈 39.73 Punkte
🔧 Programmierung

🔧 Client-Side Rendering (CSR) Vs Server-Side Rendering (SSR)


📈 39.73 Punkte
🔧 Programmierung

🔧 CSR ve SSR


📈 39.73 Punkte
🔧 Programmierung

🔧 Client Side Rendering (CSR) vs Server Side Rendering (SSR): Simplified Story


📈 39.73 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 39.49 Punkte
🔧 Programmierung

🔧 Episode 24/27: SSR Hybrid Rendering & Full SSR Guide


📈 36.56 Punkte
🔧 Programmierung

🔧 Writing a SSG in Go


📈 33.08 Punkte
🔧 Programmierung

🔧 UseRouter import from next/navigation or next/router in App Router Next JS?


📈 32.31 Punkte
🔧 Programmierung

🔧 What is The Difference Between Server Side Rendering (SSR) and React Server Components?


📈 30.57 Punkte
🔧 Programmierung

matomo