项目结构和组织
本页概述了 Next.js 中的文件夹和文件约定,以及组织项目的技巧。
文件夹和文件约定
顶层文件夹
顶层文件夹用于组织应用程序的代码和静态资源。


顶层文件
顶层文件用于配置应用程序、管理依赖项、运行中间件、集成监控工具以及定义环境变量。
next.config.js | |
next.config.js | Next.js 的配置文件 |
package.json | 项目依赖项和脚本 |
instrumentation.ts | OpenTelemetry 和检测文件 |
middleware.ts | Next.js 请求中间件 |
.env | 环境变量 |
.env.local | 本地环境变量 |
.env.production | 生产环境环境变量 |
.env.development | 开发环境环境变量 |
.eslintrc.json | ESLint 的配置文件 |
.gitignore | 要忽略的 Git 文件和文件夹 |
next-env.d.ts | Next.js 的 TypeScript 声明文件 |
tsconfig.json | TypeScript 的配置文件 |
jsconfig.json | JavaScript 的配置文件 |
文件约定
_app | .js .jsx .tsx | 自定义 App |
_document | .js .jsx .tsx | 自定义 Document |
_error | .js .jsx .tsx | 自定义错误页面 |
404 | .js .jsx .tsx | 404 错误页面 |
500 | .js .jsx .tsx | 500 错误页面 |
路由
文件夹约定 | ||
index | .js .jsx .tsx | 首页 |
folder/index | .js .jsx .tsx | 嵌套页面 |
文件约定 | ||
index | .js .jsx .tsx | 首页 |
file | .js .jsx .tsx | 嵌套页面 |
动态路由
文件夹约定 | ||
[folder]/index | .js .jsx .tsx | 动态路由段 |
[...folder]/index | .js .jsx .tsx | 捕获所有路由段 |
[[...folder]]/index | .js .jsx .tsx | 可选的捕获所有路由段 |
文件约定 | ||
[file] | .js .jsx .tsx | 动态路由段 |
[...file] | .js .jsx .tsx | 捕获所有路由段 |
[[...file]] | .js .jsx .tsx | 可选的捕获所有路由段 |
这是否有帮助?