專案結構與組織
本頁概述 Next.js 中的資料夾和檔案慣例,以及組織專案的技巧。
資料夾和檔案慣例
頂層資料夾
頂層資料夾用於組織應用程式的程式碼和靜態資源。


頂層檔案
頂層檔案用於設定應用程式、管理依賴套件、執行中介軟體、整合監控工具以及定義環境變數。
Next.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 | Custom App |
_document | .js .jsx .tsx | Custom 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 | 選用的捕捉所有路由區段 |
這有幫助嗎?