JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsnextjs
Next

Learn the concept

File-Based Routing

nextjs
mid
routing

What are the key differences between Pages Router and App Router in Next.js?

nextjs
app-router
pages-router
routing
layouts
server-components
Quick Answer

Pages Router uses the `pages/` directory with getServerSideProps/getStaticProps for data fetching, while App Router uses the `app/` directory with React Server Components, nested layouts, and streaming - and is the recommended approach for new projects.

Detailed Explanation

Pages Router (Legacy):

  • Directory: pages/
  • Each file is a route (pages/about.tsx → /about)
  • Data fetching: getStaticProps, getServerSideProps, getStaticPaths
  • All components are Client Components
  • _app.tsx for global layout
  • _document.tsx for HTML structure

App Router (Recommended):

  • Directory: app/
  • Routes defined by folders with page.tsx
  • Server Components by default
  • Nested layouts with layout.tsx
  • Streaming with loading.tsx
  • Error handling with error.tsx
  • Data fetching directly in components with async/await
  • Built-in support for Suspense

Key Differences:

| Feature | Pages Router | App Router | |---------|-------------|------------| | Default Components | Client | Server | | Layouts | Single _app | Nested layout.tsx | | Data Fetching | Special functions | async components | | Streaming | Limited | Built-in | | Caching | Manual | Opt-in via "use cache" (v16) |

Next.js 16 Caching Model: Caching is explicit and opt-in using the \"use cache\" directive. There is no implicit caching of fetch requests. Use cacheLife() for time-based profiles and cacheTag() for on-demand revalidation.

Migration: Both can coexist in the same project for gradual migration.

Code Examples

Pages Router structure and data fetchingTSX
// pages/posts/[id].tsx - Pages Router
import { GetServerSideProps } from 'next';

type Props = {
  post: { title: string; content: string };
};

// Data fetching via special function
export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
  const { id } = ctx.params;
  const res = await fetch(`https://api.example.com/posts/${id}`);
  const post = await res.json();
  
  return { props: { post } };
};

// Component receives props from getServerSideProps
export default function PostPage({ post }: Props) {
  return (
    <article>
      <h1>{post.title}</h1>
      <p>{post.content}</p>
    </article>
  );
}

Real-World Applications

Use Cases

Gradual Migration

Running Pages Router and App Router side-by-side to incrementally migrate a large production app without rewrites

Complex Dashboard Layouts

Using App Router nested layouts for persistent sidebar, header, and tab navigation across dashboard sections

Streaming Content

Using App Router loading.tsx and Suspense for progressive content loading in data-heavy applications

Mini Projects

Pages-to-App Router Migration

intermediate

Take a simple Pages Router app with getServerSideProps and migrate it to App Router with Server Components and streaming

Nested Layout System

intermediate

Build a SaaS app with root layout (nav), dashboard layout (sidebar), and settings layout (tabs) using App Router

Industry Examples

Vercel

Migrated vercel.com incrementally from Pages Router to App Router while maintaining zero downtime

OpenAI

Uses App Router with nested layouts and streaming for the ChatGPT web interface

Resources

App Router vs Pages Router

docs

Migrating from Pages to App Router

docs

Related Questions

What's the difference between Server Components and Client Components in Next.js?

mid
components

How does data fetching work in Next.js?

mid
data-fetching
Next
How does data fetching work in Next.js?
Next