Should You Use Next.js Pages or App Directory?

CraftedX
4 min readAug 5, 2023

--

Pages Router was the main way to create routes in Next.js before Next.js 13 introduced the App Router, which includes new file conventions to easily create pages, shared layouts, and templates.

Is it worth Migrating to the App Router, and leveraging React’s latest features?

Pages Directory

You can create pages as React components in the pages directory, and each page is associated with a specific route. You can also create layouts to wrap your pages and have consistent elements across multiple pages (like the Navbar and Footer). This helps in organizing and structuring your application.

Example of the Pages Directory

// pages/index.tsx
import React from 'react';

const Home: React.FC = () => {
return <h1>Hello, world!</h1>;
};

export default Home;

The App Directory

The new app directory is introduced alongside the pages directory to support incremental adoption and provide new features like server-side rendering and React Server Components.

By the way, Incremental adoption is basically an approach to implementing change in a gradual and phased manner, rather than all at once.

Example of the App Directory

// app/pages/index.tsx
import React from 'react';
import Layout from '../components/Layout';

const Home: React.FC = () => {
return (
<Layout>
<h1>Hello, world!</h1>
</Layout>
);
};

export default Home;
Pros and Cons

Pages Directory

Pros:

  • Easy to understand & utilize
  • Built-in support for server-side rendering & React Server Components
  • Good for small applications
  • Simple & straightforward routing
  • Good for static pages
  • Automatic routing based on file structure

Cons:

  • Limited flexibility and support for shared layouts and nested routing
  • Hard to maintain for large applications, May require additional configuration for more complex routing needs
  • Hard to share components between pages
  • May not be suitable for all use cases

App Directory

Pros:

  • More flexible
  • Supports Next.js 13 new features like layouts, error components, and loading components
  • Better for large applications
  • Leverages React’s server components for better performance
  • Better for dynamic pages
  • Provides more advanced routing and layout capabilities
  • Better for sharing components between pages
  • Offers a new way of building apps with welcomed improvements to architecture

Cons:

  • More complex
  • Experimental and not yet considered stable for production use
  • Steep learning curve
  • Some features, like nested layouts, caching may still have limitations & problems
  • Requires more setup and may not be necessary for simpler applications
pages vs app directory

How do I decide which directory to use?

How?

The decision to use either the Pages or App directory depends on the complexity of your application.

If you have a small application with static pages, the Pages directory is a good choice. If you have a large application with dynamic pages, the App directory is a better choice.

Still Can’t decide yet!, Consider the following:

  • Use the Pages directory if you want simple and straightforward routing, automatic routing based on file structure, and easy creation of pages as React components.
  • Use the App directory if you need more advanced routing and layout capabilities, want to leverage React’s server components for better performance, and want to customize your app’s behavior or layout.
  • Consider using both directories if you want to incrementally adopt the App directory while still using the Pages directory for simpler pages.
  • Keep in mind that the App directory is experimental and not yet considered stable for production use, and may require additional learning and understanding of the new paradigm.

Ultimately, the decision of which directory to use depends on the specific needs and goals of your project.

Dig Deeper: Check out my other article to answer the question:

Is It Worth switching to the New Next.js 13 App Router?

I hope that helped. If you have any other questions or inquiries, please feel free to share them below, I’m still new to the web development and programming world, so any suggestions for improvement are always welcome.

Feel free to check out my portfolio, buy me a coffee, or shoot me an email at th.dev.design@gmail.com.

If this article was helpful (or if you’re just feeling generous), please click the clap 👏 button below a few times. Your support means the world to us! ⬇⬇

--

--

CraftedX

Web Developer | Tech Writer | English Student #nextjs #react #typescript