Next.js provides multiple ways to fetch data, each suited for different use cases. Understanding them helps optimize performance, SEO, and user experience.
Method | When Data is Fetched? | Use Case |
---|---|---|
CSR (Client-Side Rendering) | After page loads (on client) | Non-SEO pages, user-specific data |
SSR (Server-Side Rendering) | On every request (server) | Dynamic pages with fresh data |
SSG (Static Site Generation) | At build time | Blogs, marketing pages (fast & cached) |
ISR (Incremental Static Regeneration) | Build time + on-demand updates | Static pages with occasional updates |
PPR (Partial Prerendering) | Hybrid: Some content pre-rendered, some fetched on client | Dynamic content with SEO optimization |
useEffect
(CSR)"use client"; // Required for client-side renderingimport { useState, useEffect } from "react";export default function CSRPage() {const [data, setData] = useState(null);useEffect(() => {fetch("https://api.example.com/data").then((res) => res.json()).then((data) => setData(data));}, []);return (<div><h1>Client-Side Rendered Data</h1>{data ? <pre>{JSON.stringify(data, null, 2)}</pre> : <p>Loading...</p>}</div>);}
✅ Pros: Works well for user-specific content (auth-required pages).
❌ Cons: Slower initial load, bad for SEO.
getServerSideProps
(SSR)// app/page.tsx (Server Component)export async function getServerSideProps() {const res = await fetch("https://api.example.com/data");const data = await res.json();return { props: { data } };}export default function SSRPage({ data }) {return (<div><h1>Server-Side Rendered Data</h1><pre>{JSON.stringify(data, null, 2)}</pre></div>);}
✅ Pros: Fresh data, SEO-friendly.
❌ Cons: Slower response time, higher server load.
getStaticProps
(SSG)// app/page.tsx (Server Component)export async function getStaticProps() {const res = await fetch("https://api.example.com/data");const data = await res.json();return { props: { data } };}export default function SSGPage({ data }) {return (<div><h1>Static Site Generated Data</h1><pre>{JSON.stringify(data, null, 2)}</pre></div>);}
✅ Pros: Super fast, scalable, SEO-friendly.
❌ Cons: Data can become stale.
revalidate
to refresh data at a set interval. revalidate
in ISRexport async function getStaticProps() {const res = await fetch("https://api.example.com/data");const data = await res.json();return { props: { data }, revalidate: 60 }; // Regenerates every 60 seconds}
✅ Pros: Fast like SSG, but can update without rebuilding the whole site.
❌ Cons: Data is not always 100% fresh.
// app/page.tsximport { Suspense } from "react";import Products from "./Products";export default function HomePage() {return (<div><h1>Welcome to Our Store</h1><Suspense fallback={<p>Loading products...</p>}><Products /></Suspense></div>);}
// app/Products.tsx (Server Component)export default async function Products() {const res = await fetch("https://api.example.com/products");const products = await res.json();return (<ul>{products.map((p) => (<li key={p.id}>{p.name}</li>))}</ul>);}
✅ Pros: SEO-friendly, fast, handles dynamic data well.
❌ Cons: More complex than SSG or SSR.
Feature | CSR | SSR | SSG | ISR | PPR |
---|---|---|---|---|---|
Data freshness | ✅ Latest | ✅ Latest | ❌ Stale | ✅ Auto-refresh | ✅ Latest |
SEO | ❌ No | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
Performance | ❌ Slow (JS heavy) | ❌ Slower (server fetch) | ✅ Fast (pre-built) | ✅ Fast | ✅ Fast |
Best for | Private dashboards | News feeds | Blogs, docs | E-commerce, product pages | Personalized SEO content |
Would you like a deep dive into React Server Components, caching strategies, or middleware in Next.js 15? 🚀
Quick Links
Legal Stuff
Social Media