JS Guide
HomeQuestionsSearchResources
Search

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsnextjs
Next
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 | Automatic |

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

Code Examples

Pages Router structure and data fetching
// 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>
  );
}

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?