What is the difference between the traditional pages directory and the src/app directory in Next.js?
Hey folks! What’s up? Hope everything is good. Today, I’ll discuss the differences between the traditional pages directory and the src/app directory in Next.js. in my previous article, I explained how Next.js projects are different from React.js projects. where I dive deep into next.js and react.js.
The current version of Next.js
The current stable version of Next.js is Next.js 15, which was officially released in late 2024. This version introduced several improvements, including the stabilization of Turbopack, which provides faster development builds. It also offers better optimization for code splitting and caching.
Next.js 15 enhances the predictability of caching and server-side rendering and introduces features like Partial Prerendering (PPR) and new APIs for deferring tasks after a response.
With Next.js 15, developers can expect faster local development workflows, improved support for server components, and a smoother experience when scaling applications. For more detailed insights into these new features, it’s recommended to check the release notes and official documentation.
src/app directory
The src/app directory was introduced in Next.js 13, as part of a major overhaul of the framework. This change aimed to improve the developer experience and offer more flexibility for building applications, particularly with the introduction of features like React Server Components and Concurrent Rendering.
The app
directory is an experimental feature in Next.js 13, and it became a central part of the framework in subsequent minor releases. It provides a new way to structure applications, offering features like layouts, nested routes, templates, and server-side rendering out of the box.
With the release of Next.js 13.2 and later versions, the src/app
directory was promoted from an experimental feature to a more stable, production-ready feature, although some aspects may still evolve as Next.js continues to improve its API and features.
Key Releases Related to src/app
:
Next.js 13 (October 2022): Introduced the
app
directory as part of the new app structure, along with support for React Server Components and improved routing capabilities.Next.js 13.2+ (2023): The
app
directory gained more stability and additional features like layouts, templates, and concurrent rendering.
So, if you're working with Next.js 13 or later, you have the option to use the src/app
directory to structure your app more flexibly, but it's still essential to follow the Next.js documentation to ensure compatibility with future releases.
image taken from: https://nextjs.org/docs/app/getting-started/project-structure
pages directory:
The traditional pages
the directory was introduced in the first release of Next.js, which was Next.js 1.0 in October 2016. It was part of the foundational design of Next.js, showcasing its core concept of file-based routing.
Highlights from the Introduction:
Each file in the
pages
directory automatically mapped to a route, simplifying the process of defining application routes.Dynamic routes and API routes were added in subsequent updates, further enhancing the functionality of the
pages
directory.
The pages
directory became a hallmark feature of Next.js and remained the default for several years until the app
directory was introduced in Next.js 13 (released in October 2022). This newer approach offered enhanced capabilities while retaining backward compatibility with the traditional pages
directory.
In Next.js, the introduction of the src/app
directory marks a shift from the traditional pages
directory, bringing new features and concepts. Here's a breakdown of the differences between the two:
1. Routing
pages
Directory (Traditional)Each file inside the
pages
directory automatically creates a route. For example,pages/index.js
corresponds to the root route (/
), andpages/about.js
corresponds to/about
.Dynamic routes are created using file names wrapped in square brackets, such as
pages/[id].js
.
src/app
DirectoryThe
app
directory introduces file-based routing with an emphasis on layouts, templates, and more advanced routing features like nested routes and server components.It's more flexible in terms of layouts and components that can be shared across routes. You can define layouts within this directory to wrap parts of your app with specific UI structures.
2. Folder Structure
pages
DirectoryThe
pages
directory contains only the pages of your application.It’s a flat structure where the page names are directly tied to routes.
src/app
DirectoryThe
app
directory allows you to structure your app more modularly.It can include
layouts
,templates
,pages
,components
, and other application-related files, making it a more holistic directory for managing the whole application.app
also supports the concept of nested routing with layouts. For example, you can define a layout inapp/layout.js
and then define nested layouts for specific sections of your site.
3. Layouts and Nested Routes
pages
Directory- The traditional
pages
directory doesn’t have built-in support for layouts or nesting. While you can create shared layouts in components and include them in each page, this needs to be manually implemented.
- The traditional
src/app
DirectoryThe
app
directory supports nested layouts. You can define layouts that apply to multiple routes, which are inherited by child routes.For example, you could have a layout for the entire app (e.g.,
app/layout.js
), and inside any sub-folder (e.g.,app/dashboard/layout.js
), you could define a layout specific to that section.Layouts in the
app
directory allow for more optimized rendering by React Server Components.
4. Server Components and Suspense
pages
Directory- The
pages
directory does not support React Server Components or Suspense out of the box. If you want to use them, you need to manage it manually.
- The
src/app
Directory- The
app
directory natively supports React Server Components and Suspense. This allows for optimized server-side rendering and incremental static regeneration (ISR) to reduce client-side JavaScript bundle size and improve performance.
- The
5. API Routes
pages
Directory- API routes are defined under
pages/api/
(e.g.,pages/api/hello.js
for an API endpoint).
- API routes are defined under
src/app
Directory- In the
app
directory, API routes are still defined within thepages/api
folder. However, Next.js is evolving, and future versions may bring changes to how these API routes are structured in relation to theapp
directory.
- In the
6. File Naming and Conventions
pages
Directory- File naming in
pages
directly impacts routing. Each file corresponds to a route, and dynamic routes are defined using brackets (e.g.,[id].js
).
- File naming in
src/app
DirectoryThe file structure in the
app
directory follows a more modular approach, where files likelayout.js
,page.js
,template.js
, anderror.js
play key roles.The concept of templates and error boundaries can be leveraged to control the UI structure across multiple pages or error handling for specific routes.
7. Static Site Generation (SSG) and Server-Side Rendering (SSR)
pages
Directory- You can use
getStaticProps
andgetServerSideProps
for static generation and server-side rendering respectively in thepages
directory.
- You can use
src/app
Directory- The
app
directory provides more advanced control over rendering with automatic static optimization, server-side rendering (SSR), and streaming support for layouts and pages.
- The
The src/app
directory offers a more advanced approach by providing modularity with layouts, templates, React Server Components, and support for Suspense. This directory provides scalability and flexibility when building larger applications that require dynamic layouts and advanced rendering techniques. It represents a modern method for structuring Next.js applications, especially when utilizing server-side rendering and the concurrent rendering features of React.