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.
| Folder | Description |
|---|---|
app | App Router |
pages | Pages Router |
public | Static assets to be served |
src | Optional 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.
| File | Description |
|---|---|
next.config.js | Configuration file for Next.js |
package.json | Project dependencies and scripts |
instrumentation.ts | OpenTelemetry and Instrumentation file |
middleware.ts | Next.js request middleware |
.env | Environment variables |
.env.local | Local environment variables |
.env.production | Production environment variables |
.env.development | Development environment variables |
.eslintrc.json | Configuration file for ESLint |
.gitignore | Git files and folders to ignore |
next-env.d.ts | TypeScript declaration file for Next.js |
tsconfig.json | Configuration file for TypeScript |
jsconfig.json | Configuration file for JavaScript |
App Router Filesβ
| File | Extensions | Description |
|---|---|---|
layout | .js .jsx .tsx | Shared UI for a segment and its children |
page | .js .jsx .tsx | Unique UI of a route and make routes publicly accessible |
loading | .js .jsx .tsx | Loading UI for a segment and its children |
not-found | .js .jsx .tsx | Not found UI for a segment and its children |
error | .js .jsx .tsx | Error UI for a segment and its children |
global-error | .js .jsx .tsx | Global error UI |
route | .js .ts | Server-side API endpoint |
template | .js .jsx .tsx | Specialized re-rendered Layout UI |
default | .js .jsx .tsx | Fallback UI for Parallel Routes |
Routing Conventionsβ
| Convention | Description |
|---|---|
folder | Route segment |
folder/folder | Nested route segment |
[folder] | Dynamic route segment |
[...folder] | Catch-all route segment |
[[...folder]] | Optional catch-all route segment |
(folder) | Route group (does not affect URL) |
_folder | Private folder (excluded from routing) |
@folder | Named slot for parallel routes |
(.)folder | Intercept same level |
(..)folder | Intercept one level above |
(..)(..)folder | Intercept two levels above |
(...)folder | Intercept from root |
Metadata Filesβ
| File | Extensions | Description |
|---|---|---|
favicon | .ico | Favicon file |
icon | .ico .jpg .jpeg .png .svg | App Icon file |
icon | .js .ts .tsx | Generated App Icon |
apple-icon | .jpg .jpeg .png | Apple App Icon file |
apple-icon | .js .ts .tsx | Generated Apple App Icon |
opengraph-image | .jpg .jpeg .png .gif | Open Graph image file |
opengraph-image | .js .ts .tsx | Generated Open Graph image |
twitter-image | .jpg .jpeg .png .gif | Twitter image file |
twitter-image | .js .ts .tsx | Generated Twitter image |
sitemap | .xml | Sitemap file |
sitemap | .js .ts | Generated Sitemap |
robots | .txt | Robots file |
robots | .js .ts | Generated 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
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:
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!


