ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β π nextjs/app/getting-started/project-structure β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
This page provides an overview of all the folder and file conventions in Next.js, and recommendations for organizing your project.
Top-level folders are used to organize your application's code and static assets.
<Image alt="Route segments to path segments" srcLight="/nextjs/light/top-level-folders.png" srcDark="/nextjs/dark/top-level-folders.png" width="1600" height="525" />
| | |
| ------------------------------------------------------------------ | ---------------------------------- |
| app | App Router |
| pages | Pages Router |
| public | Static assets to be served |
| src | Optional application source folder |
Top-level files are used to configure your application, manage dependencies, run proxy, integrate monitoring tools, and define environment variables.
| | |
| ---------------------------------------------------------------------------- | --------------------------------------- |
| Next.js | |
| next.config.js | Configuration file for Next.js |
| package.json | Project dependencies and scripts |
| instrumentation.ts | OpenTelemetry and Instrumentation file |
| proxy.ts | Next.js request proxy |
| .env | Environment variables |
| .env.local | Local environment variables |
| .env.production | Production environment variables |
| .env.development | Development environment variables |
| eslint.config.mjs | 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 |
Add page to expose a route, layout for shared UI such as header, nav, or footer, loading for skeletons, error for error boundaries, and route for APIs.
| | | |
| ----------------------------------------------------------------------------- | ------------------- | ---------------------------- |
| layout | .js .jsx .tsx | Layout |
| page | .js .jsx .tsx | Page |
| loading | .js .jsx .tsx | Loading UI |
| not-found | .js .jsx .tsx | Not found UI |
| error | .js .jsx .tsx | Error UI |
| global-error | .js .jsx .tsx | Global error UI |
| route | .js .ts | API endpoint |
| template | .js .jsx .tsx | Re-rendered layout |
| default | .js .jsx .tsx | Parallel route fallback page |
Folders define URL segments. Nesting folders nests segments. Layouts at any level wrap their child segments. A route becomes public when a page or route file exists.
| Path | URL pattern | Notes |
| --------------------------- | --------------- | ----------------------------- |
| app/layout.tsx | β | Root layout wraps all routes |
| app/blog/layout.tsx | β | Wraps /blog and descendants |
| app/page.tsx | / | Public route |
| app/blog/page.tsx | /blog | Public route |
| app/blog/authors/page.tsx | /blog/authors | Public route |
Parameterize segments with square brackets. Use [segment] for a single param, [...segment] for catchβall, and [[...segment]] for optional catchβall. Access values via the params prop.
| Path | URL pattern |
| ------------------------------- | -------------------------------------------------------------------- |
| app/blog/[slug]/page.tsx | /blog/my-first-post |
| app/shop/[...slug]/page.tsx | /shop/clothing, /shop/clothing/shirts |
| app/nextjs/[[...slug]]/page.tsx | /docs, /nextjs/layouts-and-pages, /nextjs/api-reference/use-router |
Organize code without changing URLs with route groups (group), and colocate non-routable files with private folders _folder.
| Path | URL pattern | Notes |
| ------------------------------- | ----------- | ----------------------------------------- |
| app/(marketing)/page.tsx | / | Group omitted from URL |
| app/(shop)/cart/page.tsx | /cart | Share layouts within (shop) |
| app/blog/_components/Post.tsx | β | Not routable; safe place for UI utilities |
| app/blog/_lib/data.ts | β | Not routable; safe place for utils |
These features fit specific UI patterns, such as slot-based layouts or modal routing.
Use @slot for named slots rendered by a parent layout. Use intercept patterns to render another route inside the current layout without changing the URL, for example, to show a details view as a modal over a list.
| Pattern (docs) | Meaning | Typical use case |
| ------------------------------------------------------------------------------------------- | -------------------- | ---------------------------------------- |
| @folder | Named slot | Sidebar + main content |
| (.)folder | Intercept same level | Preview sibling route in a modal |
| (..)folder | Intercept parent | Open a child of the parent as an overlay |
| (..)(..)folder | Intercept two levels | Deeply nested overlay |
| (...)folder | Intercept from root | Show arbitrary route in current view |
| | | |
| --------------------------------------------------------------------------------------------------------------- | ----------------------------------- | ------------------------ |
| 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 |
| | | |
| ----------------------------------------------------------------------------------------------------------- | ------------------- | ----------------- |
| _app | .js .jsx .tsx | Custom App |
| _document | .js .jsx .tsx | Custom Document |
| _error | .js .jsx .tsx | Custom Error Page |
| 404 | .js .jsx .tsx | 404 Error Page |
| 500 | .js .jsx .tsx | 500 Error Page |
| | | |
| ---------------------------------------------------------------------------------------------- | ------------------- | ----------- |
| Folder convention | | |
| index | .js .jsx .tsx | Home page |
| folder/index | .js .jsx .tsx | Nested page |
| File convention | | |
| index | .js .jsx .tsx | Home page |
| file | .js .jsx .tsx | Nested page |
| | | |
| ----------------------------------------------------------------------------------------------------------------- | ------------------- | -------------------------------- |
| Folder convention | | |
| [folder]/index | .js .jsx .tsx | Dynamic route segment |
| [...folder]/index | .js .jsx .tsx | Catch-all route segment |
| [[...folder]]/index | .js .jsx .tsx | Optional catch-all route segment |
| File convention | | |
| [file] | .js .jsx .tsx | Dynamic route segment |
| [...file] | .js .jsx .tsx | Catch-all route segment |
| [[...file]] | .js .jsx .tsx | Optional catch-all route segment |
Next.js is unopinionated about how you organize and colocate your project files. But it does provide several features to help you organize your project.
The components defined in special files are rendered in a specific hierarchy:
layout.jstemplate.jserror.js (React error boundary)loading.js (React suspense boundary)not-found.js (React error boundary for "not found" UI)page.js or nested layout.js<Image alt="Component Hierarchy for File Conventions" srcLight="/nextjs/light/file-conventions-component-hierarchy.png" srcDark="/nextjs/dark/file-conventions-component-hierarchy.png" width="1600" height="643" />
The components are rendered recursively in nested routes, meaning the components of a route segment will be nested inside the components of its parent segment.
<Image alt="Nested File Conventions Component Hierarchy" srcLight="/nextjs/light/nested-file-conventions-component-hierarchy.png" srcDark="/nextjs/dark/nested-file-conventions-component-hierarchy.png" width="1600" height="863" />
In the app directory, nested folders define route structure. Each folder represents a route segment that is mapped to a corresponding segment in a URL path.
However, even though route structure is defined through folders, a route is not publicly accessible until a page.js or route.js file is added to a route segment.
<Image alt="A diagram showing how a route is not publicly accessible until a page.js or route.js file is added to a route segment." srcLight="/nextjs/light/project-organization-not-routable.png" srcDark="/nextjs/dark/project-organization-not-routable.png" width="1600" height="444" />
And, even when a route is made publicly accessible, only the content returned by page.js or route.js is sent to the client.
<Image alt="A diagram showing how page.js and route.js files make routes publicly accessible." srcLight="/nextjs/light/project-organization-routable.png" srcDark="/nextjs/dark/project-organization-routable.png" width="1600" height="687" />
This means that project files can be safely colocated inside route segments in the app directory without accidentally being routable.
<Image alt="A diagram showing colocated project files are not routable even when a segment contains a page.js or route.js file." srcLight="/nextjs/light/project-organization-colocation.png" srcDark="/nextjs/dark/project-organization-colocation.png" width="1600" height="1011" />
Good to know: While you can colocate your project files in
appyou don't have to. If you prefer, you can keep them outside theappdirectory.
Private folders can be created by prefixing a folder with an underscore: _folderName
This indicates the folder is a private implementation detail and should not be considered by the routing system, thereby opting the folder and all its subfolders out of routing.
<Image alt="An example folder structure using private folders" srcLight="/nextjs/light/project-organization-private-folders.png" srcDark="/nextjs/dark/project-organization-private-folders.png" width="1600" height="849" />
Since files in the app directory can be safely colocated by default, private folders are not required for colocation. However, they can be useful for:
Good to know:
- While not a framework convention, you might also consider marking files outside private folders as "private" using the same underscore pattern.
- You can create URL segments that start with an underscore by prefixing the folder name with
%5F(the URL-encoded form of an underscore):%5FfolderName.- If you don't use private folders, it would be helpful to know Next.js special file conventions to prevent unexpected naming conflicts.
Route groups can be created by wrapping a folder in parenthesis: (folderName)
This indicates the folder is for organizational purposes and should not be included in the route's URL path.
<Image alt="An example folder structure using route groups" srcLight="/nextjs/light/project-organization-route-groups.png" srcDark="/nextjs/dark/project-organization-route-groups.png" width="1600" height="849" />
Route groups are useful for:
src folderNext.js supports storing application code (including app) inside an optional src folder. This separates application code from project configuration files which mostly live in the root of a project.
<Image alt="An example folder structure with the `src` folder" srcLight="/nextjs/light/project-organization-src-directory.png" srcDark="/nextjs/dark/project-organization-src-directory.png" width="1600" height="687" />
The following section lists a very high-level overview of common strategies. The simplest takeaway is to choose a strategy that works for you and your team and be consistent across the project.
Good to know: In our examples below, we're using
componentsandlibfolders as generalized placeholders, their naming has no special framework significance and your projects might use other folders likeui,utils,hooks,styles, etc.
appThis strategy stores all application code in shared folders in the root of your project and keeps the app directory purely for routing purposes.
<Image alt="An example folder structure with project files outside of app" srcLight="/nextjs/light/project-organization-project-root.png" srcDark="/nextjs/dark/project-organization-project-root.png" width="1600" height="849" />
appThis strategy stores all application code in shared folders in the root of the app directory.
<Image alt="An example folder structure with project files inside app" srcLight="/nextjs/light/project-organization-app-root.png" srcDark="/nextjs/dark/project-organization-app-root.png" width="1600" height="849" />
This strategy stores globally shared application code in the root app directory and splits more specific application code into the route segments that use them.
<Image alt="An example folder structure with project files split by feature or route" srcLight="/nextjs/light/project-organization-app-root-split.png" srcDark="/nextjs/dark/project-organization-app-root-split.png" width="1600" height="1011" />
To organize routes without affecting the URL, create a group to keep related routes together. The folders in parenthesis will be omitted from the URL (e.g. (marketing) or (shop)).
<Image alt="Organizing Routes with Route Groups" srcLight="/nextjs/light/route-group-organisation.png" srcDark="/nextjs/dark/route-group-organisation.png" width="1600" height="930" />
Even though routes inside (marketing) and (shop) share the same URL hierarchy, you can create a different layout for each group by adding a layout.js file inside their folders.
<Image alt="Route Groups with Multiple Layouts" srcLight="/nextjs/light/route-group-multiple-layouts.png" srcDark="/nextjs/dark/route-group-multiple-layouts.png" width="1600" height="768" />
To opt specific routes into a layout, create a new route group (e.g. (shop)) and move the routes that share the same layout into the group (e.g. account and cart). The routes outside of the group will not share the layout (e.g. checkout).
<Image alt="Route Groups with Opt-in Layouts" srcLight="/nextjs/light/route-group-opt-in-layouts.png" srcDark="/nextjs/dark/route-group-opt-in-layouts.png" width="1600" height="930" />
To apply a loading skeleton via a loading.js file to a specific route, create a new route group (e.g., /(overview)) and then move your loading.tsx inside that route group.
<Image alt="Folder structure showing a loading.tsx and a page.tsx inside the route group" srcLight="/nextjs/light/route-group-loading.png" srcDark="/nextjs/dark/route-group-loading.png" width="1600" height="444" />
Now, the loading.tsx file will only apply to your dashboard β overview page instead of all your dashboard pages without affecting the URL path structure.
To create multiple root layouts, remove the top-level layout.js file, and add a layout.js file inside each route group. This is useful for partitioning an application into sections that have a completely different UI or experience. The <html> and <body> tags need to be added to each root layout.
<Image alt="Route Groups with Multiple Root Layouts" srcLight="/nextjs/light/route-group-multiple-root-layouts.png" srcDark="/nextjs/dark/route-group-multiple-root-layouts.png" width="1600" height="687" />
In the example above, both (marketing) and (shop) have their own root layout.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ