网页 综合 更新时间 2026-07-21 00:00:00

Project structure and organization

自动蜘蛛 nextjs.org 正文约 5499 字
Project structure and organization
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.
Top-level files are used to configure your application, manage dependencies, run proxy, integrate monitoring tools, and define environment variables.
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.
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.
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.
Organize code without changing URLs with route groups (group), and colocate non-routable files with private folders _folder.
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.
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:
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.
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.
And, even when a route is made publicly accessible, only the content returned by page.js or route.js is sent to the client.
This means that project files can be safely colocated inside route segments in the app directory without accidentally being routable.
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.
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.
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:
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.
Route groups are useful for:
Next.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.
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.
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.
This strategy stores all application code in shared folders in the root of your project and keeps the app directory purely for routing purposes.
This strategy stores all application code in shared folders in the root of the app directory.
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.
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)).
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. These layouts nest within the existing app layout.
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).
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.
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.
In the example above, both (marketing) and (shop) have their own root layout.
Last updated July 21, 2026
Subscribe to our newsletter
Stay updated on new releases and features, guides, and case studies.
访问原文 返回搜索 https://nextjs.org/docs/app/getting-started/project-structure
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。