Folder structure in next js

The folder structure in a Next.js project depends on whether you use the App Router (introduced in Next.js 13) or the Pages Router (legacy approach). Below is the standard folder structure for both:


1๏ธโƒฃ App Router (Next.js 13+) – Recommended

Next.js introduced the app directory to enable React Server Components, Server Actions, and Streaming. The structure looks like this:

my-next-app/
โ”‚โ”€โ”€ public/            # Static assets (images, icons, etc.)
โ”‚โ”€โ”€ src/               # Source folder (optional)
โ”‚   โ”œโ”€โ”€ app/           # App Router (Replaces pages/)
โ”‚   โ”‚   โ”œโ”€โ”€ layout.tsx # Root layout (applies to all pages)
โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx   # Default homepage
โ”‚   โ”‚   โ”œโ”€โ”€ about/     # Route: /about
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx
โ”‚   โ”‚   โ”œโ”€โ”€ dashboard/ # Nested routes: /dashboard
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ settings/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx
โ”‚   โ”‚   โ”œโ”€โ”€ api/       # API routes (e.g., /api/hello)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ hello/ 
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts
โ”‚   โ”œโ”€โ”€ components/    # Reusable UI components
โ”‚   โ”œโ”€โ”€ styles/        # Global CSS or Tailwind config
โ”‚   โ”œโ”€โ”€ lib/           # Utility functions, API calls
โ”‚   โ”œโ”€โ”€ hooks/         # Custom hooks
โ”‚   โ”œโ”€โ”€ context/       # Context providers (optional)
โ”‚โ”€โ”€ .next/             # Build output (auto-generated)
โ”‚โ”€โ”€ node_modules/      # Installed dependencies
โ”‚โ”€โ”€ next.config.js     # Next.js config file
โ”‚โ”€โ”€ tailwind.config.js # Tailwind CSS config (if using Tailwind)
โ”‚โ”€โ”€ package.json       # Project dependencies & scripts
โ”‚โ”€โ”€ tsconfig.json      # TypeScript config (if using TypeScript)
โ”‚โ”€โ”€ .gitignore         # Ignore unnecessary files

๐Ÿ”ฅ Key Features in App Router

  • Uses React Server Components (RSC).
  • Supports Layouts (layout.tsx).
  • New API routes (app/api/route.ts).
  • Improved data fetching (Server Actions, Streaming).

2๏ธโƒฃ Pages Router (Legacy Approach)

Before Next.js 13, all pages were inside the pages/ directory.

my-next-app/
โ”‚โ”€โ”€ public/          # Static assets (images, icons, etc.)
โ”‚โ”€โ”€ pages/           # Page-based routing
โ”‚   โ”œโ”€โ”€ index.tsx    # Homepage (/)
โ”‚   โ”œโ”€โ”€ about.tsx    # About page (/about)
โ”‚   โ”œโ”€โ”€ dashboard/   # Nested pages
โ”‚   โ”‚   โ”œโ”€โ”€ index.tsx  # Dashboard main page
โ”‚   โ”‚   โ”œโ”€โ”€ settings.tsx # Dashboard settings (/dashboard/settings)
โ”‚   โ”œโ”€โ”€ api/         # API routes (e.g., /api/hello)
โ”‚   โ”‚   โ”œโ”€โ”€ hello.ts
โ”‚โ”€โ”€ components/      # Reusable UI components
โ”‚โ”€โ”€ styles/         # CSS or Tailwind styles
โ”‚โ”€โ”€ utils/          # Helper functions
โ”‚โ”€โ”€ .next/          # Build output (auto-generated)
โ”‚โ”€โ”€ node_modules/   # Installed dependencies
โ”‚โ”€โ”€ next.config.js  # Next.js config
โ”‚โ”€โ”€ package.json    # Dependencies & scripts
โ”‚โ”€โ”€ tsconfig.json   # TypeScript config (if using TypeScript)
โ”‚โ”€โ”€ .gitignore      # Ignore unnecessary files

๐Ÿ›‘ Key Differences Between App Router & Pages Router

FeatureApp Router (app/)Pages Router (pages/)
Server Componentsโœ… Yes (default)โŒ No
API Routesโœ… app/api/route.tsโœ… pages/api/hello.ts
Layoutsโœ… Built-in (layout.tsx)โŒ Manual _app.tsx
Streaming & Suspenseโœ… YesโŒ No
File-based Routingโœ… Yesโœ… Yes
Client-side Renderingโœ… Yes (with "use client")โœ… Yes

Which One Should You Use?

  • โœ… Use app/ (App Router) for new projects (recommended).
  • ๐Ÿ”„ Use pages/ (Pages Router) if migrating an old project.


Read other awesome articles inย Medium.com or in akcoding’s posts.

OR

Join us on YouTube Channel

OR Scan the QR Code to Directly open the Channel ๐Ÿ‘‰

AK Coding YouTube Channel

Share with