Skip to main content

Project Structure of your Next.js app

Folder and File Conventions​

Top-level Folders​

Top-level folders are used to organize your application's code and static assets.

https://nodejs.org/

Node.js

FolderDescription
appApp Router
pagesPages Router
publicStatic assets to be served
srcOptional application source folder

Top-level Files​

Top-level files are used to configure your application, manage dependencies, run middleware, integrate monitoring tools, and define environment variables.

FileDescription
next.config.jsConfiguration file for Next.js
package.jsonProject dependencies and scripts
instrumentation.tsOpenTelemetry and Instrumentation file
middleware.tsNext.js request middleware
.envEnvironment variables
.env.localLocal environment variables
.env.productionProduction environment variables
.env.developmentDevelopment environment variables
.eslintrc.jsonConfiguration file for ESLint
.gitignoreGit files and folders to ignore
next-env.d.tsTypeScript declaration file for Next.js
tsconfig.jsonConfiguration file for TypeScript
jsconfig.jsonConfiguration file for JavaScript

App Router Files​

FileExtensionsDescription
layout.js .jsx .tsxShared UI for a segment and its children
page.js .jsx .tsxUnique UI of a route and make routes publicly accessible
loading.js .jsx .tsxLoading UI for a segment and its children
not-found.js .jsx .tsxNot found UI for a segment and its children
error.js .jsx .tsxError UI for a segment and its children
global-error.js .jsx .tsxGlobal error UI
route.js .tsServer-side API endpoint
template.js .jsx .tsxSpecialized re-rendered Layout UI
default.js .jsx .tsxFallback UI for Parallel Routes

Routing Conventions​

ConventionDescription
folderRoute segment
folder/folderNested route segment
[folder]Dynamic route segment
[...folder]Catch-all route segment
[[...folder]]Optional catch-all route segment
(folder)Route group (does not affect URL)
_folderPrivate folder (excluded from routing)
@folderNamed slot for parallel routes
(.)folderIntercept same level
(..)folderIntercept one level above
(..)(..)folderIntercept two levels above
(...)folderIntercept from root

Metadata Files​

FileExtensionsDescription
favicon.icoFavicon file
icon.ico .jpg .jpeg .png .svgApp Icon file
icon.js .ts .tsxGenerated App Icon
apple-icon.jpg .jpeg .pngApple App Icon file
apple-icon.js .ts .tsxGenerated Apple App Icon
opengraph-image.jpg .jpeg .png .gifOpen Graph image file
opengraph-image.js .ts .tsxGenerated Open Graph image
twitter-image.jpg .jpeg .png .gifTwitter image file
twitter-image.js .ts .tsxGenerated Twitter image
sitemap.xmlSitemap file
sitemap.js .tsGenerated Sitemap
robots.txtRobots file
robots.js .tsGenerated Robots file

Organizing Your Project​

Component Rendering Hierarchy​

Components defined in special files are rendered in a specific hierarchy:

layout.js
β”œβ”€ template.js
β”œβ”€ error.js (React error boundary)
β”œβ”€ loading.js (React suspense boundary)
β”œβ”€ not-found.js (React error boundary)
└─ page.js or nested layout.js

Colocation​

In the app directory, folders define the route structure. A route becomes publicly accessible only when you add a page.js or route.js file to that folder.

This allows you to safely place your project files (components, styles, tests) inside the app directory without making them accessible via URL.

Private Folders​

Create private folders by prefixing with underscore: _folderName

https://nodejs.org/

Node.js

These folders are excluded from routing and useful for:

  • Separating UI logic from routing logic
  • Organizing internal files consistently
  • Grouping files in code editors
  • Avoiding naming conflicts with Next.js conventions

You can create URL segments starting with underscore using %5FfolderName.

Route Groups​

Create route groups by wrapping folder names in parentheses: (folderName)

These folders are for organization only and won't appear in the URL path.

Use route groups to:

  • Organize routes by section (marketing, admin, shop)
  • Apply different layouts to route groups
  • Create multiple root layouts in your application

Using src Directory​

You can optionally store your application code inside a src folder. This separates application code from configuration files in the project root.

project-root/
β”œβ”€β”€ src/
β”‚ └── app/ # Your application code
β”œβ”€β”€ public/ # Static assets
β”œβ”€β”€ next.config.js # Configuration
└── package.json # Dependencies

Project Organization Examples​

Option 1: Files Outside app​

Keep the app directory for routing only, store components and utilities in the project root.

project-root/
β”œβ”€β”€ app/ # Routes only
β”œβ”€β”€ components/ # Reusable components
β”œβ”€β”€ lib/ # Utility functions
└── public/ # Static assets

Option 2: Files Inside app​

Store all application code inside the app directory.

project-root/
└── app/
β”œβ”€β”€ components/ # Reusable components
β”œβ”€β”€ lib/ # Utility functions
β”œβ”€β”€ page.tsx
└── layout.tsx

Option 3: Split by Feature​

Store shared code globally, route-specific code within route folders.

project-root/
└── app/
β”œβ”€β”€ components/ # Global components
β”œβ”€β”€ lib/ # Global utilities
β”œβ”€β”€ dashboard/
β”‚ β”œβ”€β”€ components/ # Dashboard components
β”‚ └── page.tsx
└── page.tsx

Advanced Routing Patterns​

Multiple Layouts with Route Groups​

Organize routes and apply different layouts without affecting URLs:

app/
β”œβ”€β”€ (marketing)/
β”‚ β”œβ”€β”€ layout.tsx # Marketing layout
β”‚ β”œβ”€β”€ about/page.tsx # URL: /about
β”‚ └── blog/page.tsx # URL: /blog
└── (shop)/
β”œβ”€β”€ layout.tsx # Shop layout
β”œβ”€β”€ products/page.tsx # URL: /products
└── cart/page.tsx # URL: /cart

Loading UI for Specific Routes​

Apply loading states to specific routes using route groups:

app/
└── dashboard/
β”œβ”€β”€ (overview)/
β”‚ β”œβ”€β”€ loading.tsx # Loading UI for overview
β”‚ └── page.tsx # URL: /dashboard
└── settings/
└── page.tsx # URL: /dashboard/settings

Multiple Root Layouts​

Create completely different experiences for different sections:

https://nodejs.org/

Node.js

app/
β”œβ”€β”€ (marketing)/
β”‚ β”œβ”€β”€ layout.tsx # Root layout with <html> and <body>
β”‚ └── page.tsx
└── (app)/
β”œβ”€β”€ layout.tsx # Different root layout
└── page.tsx

Each root layout must include <html> and <body> tags.

Conclusion​

Congratulations! You've mastered Next.js project structure. You now understand how to organize folders and files, use routing conventions like dynamic routes and route groups, and structure your application professionally. You're ready to build well-organized Next.js projects with confidence!