DEV Community

Mohamed Hajith.M
Mohamed Hajith.M

Posted on

Next.js Rendering Patterns

โšกNext.js Rendering Patterns Explained: Build a Fruit Shop Website ๐ŸŽ

If you're new to Next.js or wondering how it makes React even better, you're in the right place! Next.js is a powerful React framework that simplifies building fast, SEO-friendly, and scalable web apps.

One of its biggest strengths? It supports multiple rendering patterns:

  • Static Site Generation (SSG)
  • Client-Side Rendering (CSR)
  • Server-Side Rendering (SSR)
  • Incremental Static Regeneration (ISR)

In this post, weโ€™ll break them down using a real-world example: a fruit shop website. By the end, youโ€™ll know when and how to use each rendering method in your Next.js apps.


๐Ÿง  What Is Rendering?

Rendering is how your websiteโ€™s HTML is prepared and sent to a userโ€™s browser. Think of it as how your fruit shop delivers information to customers visiting its website.

Letโ€™s imagine you run a fruit shop website with these pages:

Page URL Description
Homepage / Static welcome page (rarely changes).
Dashboard /dashboard User-specific orders.
News /news Daily fruit price updates (changes often).
Banana Details /products/banana Banana price, updated hourly.

๐Ÿ‡ 1. Static Site Generation (SSG) โ€“ "Printing a Poster"

What is SSG?

SSG pre-renders HTML at build time. The result is a static file served to all usersโ€”fast and SEO-friendly.

๐Ÿ” Real-World Analogy:
You print a welcome poster and hang it outside your fruit shop. Everyone sees the same poster until you reprint it.

๐Ÿง‘โ€๐Ÿ’ป Code Example: Homepage (/)

// pages/index.js
export async function getStaticProps() {
  return {
    props: {
      message: "Welcome to the Fruit Shop! Fresh fruits daily!",
    },
  };
}

export default function Home({ message }) {
  return <h1>{message}</h1>;
}
Enter fullscreen mode Exit fullscreen mode

โœ… When to Use

  • Static content like homepages, blogs, documentation.
  • Content that changes infrequently.

Pros & Cons

โœ”๏ธ Fastest performance
โœ”๏ธ Great SEO
โŒ Requires rebuilds for updates


๐Ÿ 2. Client-Side Rendering (CSR) โ€“ "Asking After You Enter"

What is CSR?

CSR loads a blank page and then fetches data in the browser after JavaScript loads.

๐Ÿ” Real-World Analogy:
A customer enters your shop and asks, โ€œWhat are my orders?โ€ The clerk (JavaScript) looks them up and shows the list.

๐Ÿง‘โ€๐Ÿ’ป Code Example: Dashboard (/dashboard)

// pages/dashboard.js
import { useEffect, useState } from 'react';

export default function Dashboard() {
  const [orders, setOrders] = useState(null);

  useEffect(() => {
    fetch('/api/orders') // Simulated API
      .then(res => res.json())
      .then(data => setOrders(data));
  }, []);

  return <div>{orders ? orders.join(', ') : 'Loading orders...'}</div>;
}
Enter fullscreen mode Exit fullscreen mode

โœ… When to Use

  • Authenticated user pages (dashboards, profiles).
  • Apps where SEO doesnโ€™t matter much.

Pros & Cons

โœ”๏ธ Dynamic, user-specific content
โœ”๏ธ Light server load
โŒ Slower initial load
โŒ Poor SEO


๐ŸŠ 3. Server-Side Rendering (SSR) โ€“ "Asking Before Entering"

What is SSR?

SSR renders HTML on the server for every request, ensuring data is always fresh.

๐Ÿ” Real-World Analogy:
Before you show the News page, the shop checks the latest prices and prepares the page for the customer.

๐Ÿง‘โ€๐Ÿ’ป Code Example: News (/news)

// pages/news.js
export async function getServerSideProps() {
  const res = await fetch('https://api.fruits.com/news');
  const news = await res.json();
  return { props: { news } };
}

export default function News({ news }) {
  return <div>{news.title}</div>;
}
Enter fullscreen mode Exit fullscreen mode

โœ… When to Use

  • Frequently updated pages.
  • Pages requiring SEO + fresh data.

Pros & Cons

โœ”๏ธ Always up-to-date
โœ”๏ธ SEO-friendly
โŒ Slower due to per-request rendering
โŒ Higher server load


๐ŸŒ 4. Incremental Static Regeneration (ISR) โ€“ "A Poster That Updates Hourly"

What is ISR?

ISR combines the speed of SSG with scheduled revalidationโ€”regenerating static pages in the background.

๐Ÿ” Real-World Analogy:
You hang a banana price poster that updates every hour. Customers always see a poster, and it auto-refreshes behind the scenes.

๐Ÿง‘โ€๐Ÿ’ป Code Example: Banana Details (/products/banana)

// pages/products/banana.js
export async function getStaticProps() {
  const res = await fetch('https://api.fruits.com/banana');
  const banana = await res.json();
  return {
    props: { banana },
    revalidate: 3600, // Regenerate every hour
  };
}

export default function Banana({ banana }) {
  return <h1>Banana Price: ${banana.price}</h1>;
}
Enter fullscreen mode Exit fullscreen mode

โœ… When to Use

  • Product pages, marketing pages, or feeds.
  • Content updated on a regular schedule.

Pros & Cons

โœ”๏ธ Static speed + freshness
โœ”๏ธ SEO-friendly
โŒ Slightly stale data between regenerations


๐Ÿ“Š Rendering Pattern Comparison

Pattern Freshness Speed Server Load Best For
SSG โŒ Fixed โœ… Fastest โœ… Low Static pages (Homepage)
CSR โœ… Client-side โŒ Slower โœ… Low User dashboards
SSR โœ… Real-time โŒ Slower โŒ High Real-time data (News)
ISR โœ… Periodically โœ… Fast โœ… Medium Product pages (Banana Details)

๐Ÿ”€ Mix & Match Patterns in One App

Thatโ€™s the beauty of Next.jsโ€”you can use all four patterns in one project:

  • ๐Ÿ  Use SSG for the Homepage
  • ๐Ÿ‘ค Use CSR for the Dashboard
  • ๐Ÿ“ฐ Use SSR for the News page
  • ๐ŸŒ Use ISR for the Banana Details page

๐Ÿš€ Why Use Next.js Instead of React Alone?

React is awesome for building UIs but focuses only on Client-Side Rendering (CSR). To add SSR or SSG, you'd need tools like Gatsby or custom setups.

Next.js simplifies it all:

  • Built-in support for SSR, SSG, ISR, and CSR
  • File-based routing (pages/)
  • Built-in SEO tools and performance optimizations
  • Freedom to mix rendering strategies per page

๐ŸŽ‰ Ready to Build Your Fruit Shop?

Start in minutes:

npx create-next-app@latest my-fruit-shop
cd my-fruit-shop
npm run dev
Enter fullscreen mode Exit fullscreen mode

Then create pages like:

  • pages/index.js for SSG
  • pages/dashboard.js for CSR
  • pages/news.js for SSR
  • pages/products/banana.js for ISR

When you're ready to deploy:

npm run build
npm run start
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‹ Final Thoughts

Next.js gives you powerful rendering tools to optimize for speed, SEO, and scalability. Whether youโ€™re building a blog, dashboard, or online storeโ€”thereโ€™s a rendering pattern for every use case.

Happy coding! ๐Ÿ“

Top comments (2)

Collapse
 
slj2023 profile image
slj2023

nextjsๅชๆ˜ฏreactๆก†ๆžถๆญๅปบ็š„ๅ—

Collapse
 
sanjai_r_6f2d8bd3dad6f7dd profile image
Sanjai R

Good Information about Next.js๐Ÿ‘๐Ÿ‘

OSZAR »