Skip to main content

Understanding SSR, SSG, and ISR in Next.js: Pages Router vs. App Router

Next.js Pages Router vs App Router

Next.js Pages Router vs App Router

Next.js provides two routing and data-fetching strategies: Pages Router (pages/) and App Router (app/). Each has its own way of handling Server-Side Rendering (SSR), Static Site Generation (SSG), and Incremental Static Regeneration (ISR).

Comparison Table

Feature Pages Router (pages/) App Router (app/)
SSR (Server-Side Rendering) getServerSideProps fetch() with cache: "no-store"
SSG (Static Site Generation) getStaticProps fetch() with cache: "force-cache"
ISR (Incremental Static Regeneration) getStaticProps + revalidate: X fetch() with { next: { revalidate: X } }
Client-Side Rendering (CSR) Using useEffect() with fetch() Using useEffect() with fetch() (same as Pages Router)
Nested Layouts Not supported Supported via layout.tsx
Middleware Available via middleware.ts Available via middleware.ts

Server-Side Rendering (SSR)

Pages Router (pages/)

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

App Router (app/)

async function getData() {
    const res = await fetch('https://api.example.com/data', { cache: "no-store" });
    return res.json();
}
    

Static Site Generation (SSG)

Pages Router (pages/)

export async function getStaticProps() {
    const res = await fetch('https://api.example.com/data');
    const data = await res.json();
    return { props: { data } };
}
    

App Router (app/)

async function getData() {
    const res = await fetch('https://api.example.com/data', { cache: "force-cache" });
    return res.json();
}
    

Incremental Static Regeneration (ISR)

Pages Router (pages/)

export async function getStaticProps() {
    const res = await fetch('https://api.example.com/data');
    const data = await res.json();
    return { props: { data }, revalidate: 60 };
}
    

App Router (app/)

async function getData() {
    const res = await fetch('https://api.example.com/data', { next: { revalidate: 60 } });
    return res.json();
}
    

Client-Side Rendering (CSR)

import { useEffect, useState } from 'react';

export default function Component() {
    const [data, setData] = useState(null);

    useEffect(() => {
        fetch('https://api.example.com/data')
            .then(res => res.json())
            .then(setData);
    }, []);

    if (!data) return <p>Loading...</p>;
    return <pre>{JSON.stringify(data, null, 2)}</pre>;
}
    

Nested Layouts in App Router

// app/layout.tsx
export default function Layout({ children }) {
    return (
        <div>
            <header>Header</header>
            <main>{children}</main>
            <footer>Footer</footer>
        </div>
    );
}
    

Conclusion

The App Router provides a more flexible and modern approach with nested layouts, React Server Components, and better data fetching capabilities. However, Pages Router is still useful for existing projects.

Comments

Popular posts from this blog

How to Convert a Next.js Website into a PWA and TWA

🚀 Complete Guide: Converting Next.js to PWA & TWA Transform your Next.js website into a Progressive Web App and wrap it as an Android application using Trusted Web Activity. Every step explained in detail! 📋 Table of Contents What is a PWA? What is a TWA? Prerequisites & Setup Step 1: Convert Next.js to PWA Install Dependencies Configure next.config.js Create Web App Manifest Prepare App Icons Update Document Head Build and Test PWA Step 2: Wrap PWA as TWA Create Asset Links File Install Bubblewrap CLI ...

DevOps Best Practices

 # DevOps Best Practices: Your Ultimate Guide to Modern Software Development In today's fast-paced tech world, DevOps isn't just a buzzword – it's a game-changer. Let's dive into the essential practices that can transform your software development process. ![DevOps Lifecycle](https://blogger.googleusercontent.com/img/placeholder.png) ## 🔄 1. Continuous Integration (CI) - The Foundation Think of CI as your code's quality guardian. Every time developers push code, automated tests run to catch issues early. Here's what makes great CI: - Automated builds triggered with every commit - Comprehensive test suites running automatically - Code quality checks integrated into the pipeline - Quick feedback loops to developers **Pro Tip:** Start with simple automated tests and gradually build up your test suite. Remember, it's better to have a few reliable tests than many unreliable ones. ## 🚀 2. Continuous Delivery (CD) - From Code to Customer CD ensures your software ...

Ultimate Guide to Sending Data in React: Axios & Fetch Explained

Ultimate Guide to Sending Data in React using Axios and Fetch Whether you're building a login form, uploading files, or submitting a complex dataset to your backend, understanding how to send data in React is essential. In this comprehensive guide, we'll explore all the major ways to send data using Axios and the native Fetch API , including formats like JSON , form-data , x-www-form-urlencoded , and other advanced techniques. This guide is designed for developers from beginner to advanced levels, providing both theoretical understanding and practical implementation examples. By the end of this tutorial, you'll have a complete understanding of how data flows from React components to backend servers, and you'll know exactly which method to choose for different scenarios. 📌 Why Learn Different Data Formats? Before diving into implementation details, it's crucial to understand why different data formats exist...