# The app directory vs. the pages directory
The key differences between the app
directory (which uses the App Router) and the pages
directory
(which uses the Pages Router) in Next.js. are:
Key differences
# Routing
- In the
pages
directory, routing is based on file names (e.g.,pages/about.js
creates a/about
route) . - The
app
directory uses folder-based routing. Each folder represents a route segment, and you create apage.js
file inside the folder to make it publicly accessible .
# File Structure
- The
app
directory introduces new special files likelayout.js
,loading.js
, anderror.js
that apply to specific route segments . - For example, app/blog/layout.js (opens new window)
would apply to all routes under
/blog
.
# Layouts
- In the
pages
directory, you'd use a custom_app.js
file for global layouts. - The
app
directory useslayout.js
files, which can be nested and composed, providing more flexible layouts per route segment .
# Data Fetching
- The
pages
directory usesgetStaticProps
,getServerSideProps
, andgetStaticPaths
for data fetching. - In the
app
directory, these are replaced with a new model where you can useasync/await
directly in your components and a newgenerateStaticParams
function for static path generation.
# Server Components
- By default, components in the
app
directory are React Server Components, which render on the server, reducing the amount of JavaScript sent to the client . - You can opt into client-side rendering by adding
'use client'
at the top of a file.
# Metadata and SEO
- In the
pages
directory, you'd typically usenext/head
for metadata. - The
app
directory introduces a new Metadata API, allowing you to define metadata directly in your components or in alayout.js
file .
# Loading UI:
The app
directory introduces a new loading.js
file for creating loading UI
with Suspense (opens new window) .
# Error Handling:
Instead of a global _error.js
page, the app
directory allows you to create error.js
f
iles for more granular error handling.
# Route Handlers:
API routes in the pages
directory (pages/api/*
) are replaced with route.js
files in the app
directory, offering more flexibility .
# Colocating Files
The app
directory allows you to colocate your components, styles, tests, and other related files next to your routes.
This means we can organize related files together within the same directory structure, close to where they are used.
# Parallel Routes and Intercepting Routes:
These are new advanced routing features available only in the app
directory .
# Incremental Adoption
The app
directory and App Router represent a significant evolution in Next.js,
offering more powerful and flexible ways to build your application.
They're designed to work alongside the pages
directory, allowing for incremental adoption .