โก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>;
}
โ 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>;
}
โ 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>;
}
โ 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>;
}
โ 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
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
๐ 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)
nextjsๅชๆฏreactๆกๆถๆญๅปบ็ๅ
Good Information about Next.js๐๐