Skip to content

Commit 224447c

Browse files
Getting Started Docs: Improve "Project Structure" page (#72399)
Closes: - https://linear.app/vercel/issue/DOC-3695/page-project-structure This PR: - Splits the page between the app/pages and the router docs. - Merge the colocation page from BYA.
1 parent 5f0adad commit 224447c

File tree

7 files changed

+179
-210
lines changed

7 files changed

+179
-210
lines changed

docs/01-app/01-getting-started/02-project-structure.mdx

Lines changed: 169 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
2-
title: Next.js Project Structure
2+
title: Project Structure and Organization
33
nav_title: Project Structure
4-
description: A list of folders and files conventions in a Next.js project
4+
description: Learn about the folder and file conventions in a Next.js project, and how to organize your project.
55
---
66

7-
This page provides an overview of the project structure of a Next.js application. It covers top-level files and folders, configuration files, and routing conventions within the `app` and `pages` directories.
7+
This page provides an overview of the folder and file conventions in Next.js, as well as tips for organizing your project.
88

9-
Click the file and folder names to learn more about each convention.
9+
## Folder and file conventions
1010

11-
## Top-level folders
11+
### Top-level folders
1212

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

@@ -27,7 +27,7 @@ Top-level folders are used to organize your application's code and static assets
2727
| [`public`](/docs/app/building-your-application/optimizing/static-assets) | Static assets to be served |
2828
| [`src`](/docs/app/building-your-application/configuring/src-directory) | Optional application source folder |
2929

30-
## Top-level files
30+
### Top-level files
3131

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

@@ -48,9 +48,7 @@ Top-level files are used to configure your application, manage dependencies, run
4848
| `tsconfig.json` | Configuration file for TypeScript |
4949
| `jsconfig.json` | Configuration file for JavaScript |
5050

51-
## `app` Routing Conventions
52-
53-
The following file conventions are used to define routes and handle metadata in the [`app` router](/docs/app).
51+
<AppOnly>
5452

5553
### Routing Files
5654

@@ -66,27 +64,27 @@ The following file conventions are used to define routes and handle metadata in
6664
| [`template`](/docs/app/api-reference/file-conventions/template) | `.js` `.jsx` `.tsx` | Re-rendered layout |
6765
| [`default`](/docs/app/api-reference/file-conventions/default) | `.js` `.jsx` `.tsx` | Parallel route fallback page |
6866

69-
### Nested Routes
67+
### Nested routes
7068

7169
| | |
7270
| ---------------------------------------------------------------------------- | -------------------- |
7371
| [`folder`](/docs/app/building-your-application/routing#route-segments) | Route segment |
7472
| [`folder/folder`](/docs/app/building-your-application/routing#nested-routes) | Nested route segment |
7573

76-
### Dynamic Routes
74+
### Dynamic routes
7775

7876
| | |
7977
| --------------------------------------------------------------------------------------------------------- | -------------------------------- |
8078
| [`[folder]`](/docs/app/building-your-application/routing/dynamic-routes#convention) | Dynamic route segment |
8179
| [`[...folder]`](/docs/app/building-your-application/routing/dynamic-routes#catch-all-segments) | Catch-all route segment |
8280
| [`[[...folder]]`](/docs/app/building-your-application/routing/dynamic-routes#optional-catch-all-segments) | Optional catch-all route segment |
8381

84-
### Route Groups and Private Folders
82+
### Route Groups and private folders
8583

86-
| | |
87-
| ----------------------------------------------------------------------------------- | ------------------------------------------------ |
88-
| [`(folder)`](/docs/app/building-your-application/routing/route-groups#convention) | Group routes without affecting routing |
89-
| [`_folder`](/docs/app/building-your-application/routing/colocation#private-folders) | Opt folder and all child segments out of routing |
84+
| | |
85+
| --------------------------------------------------------------------------------- | ------------------------------------------------ |
86+
| [`(folder)`](/docs/app/building-your-application/routing/route-groups#convention) | Group routes without affecting routing |
87+
| [`_folder`](#private-folders) | Opt folder and all child segments out of routing |
9088

9189
### Parallel and Intercepted Routes
9290

@@ -98,9 +96,9 @@ The following file conventions are used to define routes and handle metadata in
9896
| [`(..)(..)folder`](/docs/app/building-your-application/routing/intercepting-routes#convention) | Intercept two levels above |
9997
| [`(...)folder`](/docs/app/building-your-application/routing/intercepting-routes#convention) | Intercept from root |
10098

101-
### Metadata File Conventions
99+
### Metadata file conventions
102100

103-
#### App Icons
101+
#### App icons
104102

105103
| | | |
106104
| --------------------------------------------------------------------------------------------------------------- | ----------------------------------- | ------------------------ |
@@ -110,7 +108,7 @@ The following file conventions are used to define routes and handle metadata in
110108
| [`apple-icon`](/docs/app/api-reference/file-conventions/metadata/app-icons#apple-icon) | `.jpg` `.jpeg`, `.png` | Apple App Icon file |
111109
| [`apple-icon`](/docs/app/api-reference/file-conventions/metadata/app-icons#generate-icons-using-code-js-ts-tsx) | `.js` `.ts` `.tsx` | Generated Apple App Icon |
112110

113-
#### Open Graph and Twitter Images
111+
#### Open Graph and Twitter images
114112

115113
| | | |
116114
| --------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | -------------------------- |
@@ -128,11 +126,11 @@ The following file conventions are used to define routes and handle metadata in
128126
| [`robots`](/docs/app/api-reference/file-conventions/metadata/robots#static-robotstxt) | `.txt` | Robots file |
129127
| [`robots`](/docs/app/api-reference/file-conventions/metadata/robots#generate-a-robots-file) | `.js` `.ts` | Generated Robots file |
130128

131-
## `pages` Routing Conventions
129+
</AppOnly>
132130

133-
The following file conventions are used to define routes in the [`pages` router](/docs/pages).
131+
<PagesOnly>
134132

135-
### Special Files
133+
### Files conventions
136134

137135
| | | |
138136
| ----------------------------------------------------------------------------------------------------------- | ------------------- | ----------------- |
@@ -153,7 +151,7 @@ The following file conventions are used to define routes in the [`pages` router]
153151
| [`index`](/docs/pages/building-your-application/routing/pages-and-layouts#index-routes) | `.js` `.jsx` `.tsx` | Home page |
154152
| [`file`](/docs/pages/building-your-application/routing/pages-and-layouts) | `.js` `.jsx` `.tsx` | Nested page |
155153

156-
### Dynamic Routes
154+
### Dynamic routes
157155

158156
| | | |
159157
| ----------------------------------------------------------------------------------------------------------------- | ------------------- | -------------------------------- |
@@ -165,3 +163,151 @@ The following file conventions are used to define routes in the [`pages` router]
165163
| [`[file]`](/docs/pages/building-your-application/routing/dynamic-routes) | `.js` `.jsx` `.tsx` | Dynamic route segment |
166164
| [`[...file]`](/docs/pages/building-your-application/routing/dynamic-routes#catch-all-segments) | `.js` `.jsx` `.tsx` | Catch-all route segment |
167165
| [`[[...file]]`](/docs/pages/building-your-application/routing/dynamic-routes#optional-catch-all-segments) | `.js` `.jsx` `.tsx` | Optional catch-all route segment |
166+
167+
</PagesOnly>
168+
169+
<AppOnly>
170+
171+
## Organizing your project
172+
173+
Apart from [folder and file conventions](/docs/app/getting-started/project-structure), 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.
174+
175+
### Colocation
176+
177+
In the `app` directory, [nested folder hierarchy](/docs/app/building-your-application/routing#route-segments) defines route structure. Each folder represents a route segment that is mapped to a corresponding segment in a URL path.
178+
179+
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.
180+
181+
<Image
182+
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."
183+
srcLight="/docs/light/project-organization-not-routable.png"
184+
srcDark="/docs/dark/project-organization-not-routable.png"
185+
width="1600"
186+
height="444"
187+
/>
188+
189+
And, even when a route is made publicly accessible, only the **content returned** by `page.js` or `route.js` is sent to the client.
190+
191+
<Image
192+
alt="A diagram showing how page.js and route.js files make routes publicly accessible."
193+
srcLight="/docs/light/project-organization-routable.png"
194+
srcDark="/docs/dark/project-organization-routable.png"
195+
width="1600"
196+
height="687"
197+
/>
198+
199+
This means that **project files** can be **safely colocated** inside route segments in the `app` directory without accidentally being routable.
200+
201+
<Image
202+
alt="A diagram showing colocated project files are not routable even when a segment contains a page.js or route.js file."
203+
srcLight="/docs/light/project-organization-colocation.png"
204+
srcDark="/docs/dark/project-organization-colocation.png"
205+
width="1600"
206+
height="1011"
207+
/>
208+
209+
> **Good to know**:
210+
>
211+
> - While you **can** colocate your project files in `app` you don't **have** to. If you prefer, you can [keep them outside the `app` directory](#store-project-files-outside-of-app).
212+
213+
### Private folders
214+
215+
Private folders can be created by prefixing a folder with an underscore: `_folderName`
216+
217+
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.
218+
219+
<Image
220+
alt="An example folder structure using private folders"
221+
srcLight="/docs/light/project-organization-private-folders.png"
222+
srcDark="/docs/dark/project-organization-private-folders.png"
223+
width="1600"
224+
height="849"
225+
/>
226+
227+
Since files in the `app` directory can be [safely colocated by default](#colocation), private folders are not required for colocation. However, they can be useful for:
228+
229+
- Separating UI logic from routing logic.
230+
- Consistently organizing internal files across a project and the Next.js ecosystem.
231+
- Sorting and grouping files in code editors.
232+
- Avoiding potential naming conflicts with future Next.js file conventions.
233+
234+
> **Good to know**:
235+
>
236+
> - While not a framework convention, you might also consider marking files outside private folders as "private" using the same underscore pattern.
237+
> - 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`.
238+
> - If you don't use private folders, it would be helpful to know Next.js [special file conventions](/docs/app/getting-started/project-structure#routing-files) to prevent unexpected naming conflicts.
239+
240+
### Route groups
241+
242+
Route groups can be created by wrapping a folder in parenthesis: `(folderName)`
243+
244+
This indicates the folder is for organizational purposes and should **not be included** in the route's URL path.
245+
246+
<Image
247+
alt="An example folder structure using route groups"
248+
srcLight="/docs/light/project-organization-route-groups.png"
249+
srcDark="/docs/dark/project-organization-route-groups.png"
250+
width="1600"
251+
height="849"
252+
/>
253+
254+
Route groups are useful for:
255+
256+
- [Organizing routes into groups](/docs/app/building-your-application/routing/route-groups#organize-routes-without-affecting-the-url-path) e.g. by site section, intent, or team.
257+
- Enabling nested layouts in the same route segment level:
258+
- [Creating multiple nested layouts in the same segment, including multiple root layouts](/docs/app/building-your-application/routing/route-groups#creating-multiple-root-layouts)
259+
- [Adding a layout to a subset of routes in a common segment](/docs/app/building-your-application/routing/route-groups#opting-specific-segments-into-a-layout)
260+
261+
### `src` directory
262+
263+
Next.js supports storing application code (including `app`) inside an optional [`src` directory](/docs/app/building-your-application/configuring/src-directory). This separates application code from project configuration files which mostly live in the root of a project.
264+
265+
<Image
266+
alt="An example folder structure with the `src` directory"
267+
srcLight="/docs/light/project-organization-src-directory.png"
268+
srcDark="/docs/dark/project-organization-src-directory.png"
269+
width="1600"
270+
height="687"
271+
/>
272+
273+
### Common strategies
274+
275+
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.
276+
277+
> **Good to know**: In our examples below, we're using `components` and `lib` folders as generalized placeholders, their naming has no special framework significance and your projects might use other folders like `ui`, `utils`, `hooks`, `styles`, etc.
278+
279+
#### Store project files outside of `app`
280+
281+
This strategy stores all application code in shared folders in the **root of your project** and keeps the `app` directory purely for routing purposes.
282+
283+
<Image
284+
alt="An example folder structure with project files outside of app"
285+
srcLight="/docs/light/project-organization-project-root.png"
286+
srcDark="/docs/dark/project-organization-project-root.png"
287+
width="1600"
288+
height="849"
289+
/>
290+
291+
#### Store project files in top-level folders inside of `app`
292+
293+
This strategy stores all application code in shared folders in the **root of the `app` directory**.
294+
295+
<Image
296+
alt="An example folder structure with project files inside app"
297+
srcLight="/docs/light/project-organization-app-root.png"
298+
srcDark="/docs/dark/project-organization-app-root.png"
299+
width="1600"
300+
height="849"
301+
/>
302+
303+
#### Split project files by feature or route
304+
305+
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.
306+
307+
<Image
308+
alt="An example folder structure with project files split by feature or route"
309+
srcLight="/docs/light/project-organization-app-root-split.png"
310+
srcDark="/docs/dark/project-organization-app-root-split.png"
311+
width="1600"
312+
height="1011"
313+
/>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2-
title: Getting Started
3-
description: Learn how to create full-stack web applications with Next.js.
2+
title: Getting Started - App Router
3+
nav_title: Getting Started
4+
description: Learn how to create full-stack web applications with the Next.js App Router.
45
---

0 commit comments

Comments
 (0)