跳至內容

Next.js 專案結構 (Next.js Project Structure)

此頁面概述 Next.js 應用程式的專案結構。它涵蓋了頂層檔案和資料夾、設定檔,以及 `app` 和 `pages` 目錄中的路由慣例。(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.)

點擊檔案和資料夾名稱以進一步了解每個慣例。(Click the file and folder names to learn more about each convention.)

頂層資料夾

頂層資料夾用於組織應用程式的程式碼和靜態資源。(Top-level folders are used to organize your application's code and static assets.)

Route segments to path segments
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 middleware, integrate monitoring tools, and define environment variables.)

Next.js (Next.js)
next.config.js (next.config.js)Next.js 的設定檔 (Configuration file for Next.js)
package.json (package.json)專案依賴項和指令碼 (Project dependencies and scripts)
instrumentation.ts (instrumentation.ts)OpenTelemetry 和 Instrumentation 檔案 (OpenTelemetry and Instrumentation file)
middleware.ts (middleware.ts)Next.js 請求中介軟體 (Next.js request middleware)
.env (.env)環境變數 (Environment variables)
.env.local (.env.local)本地環境變數 (Local environment variables)
.env.production (.env.production)生產環境變數 (Production environment variables)
.env.development (.env.development)開發環境變數
.eslintrc.jsonESLint 的設定檔
.gitignore要被 Git 忽略的檔案和資料夾
next-env.d.tsNext.js 的 TypeScript 宣告檔
tsconfig.jsonTypeScript 的設定檔
jsconfig.jsonJavaScript 的設定檔

app 路由慣例

以下檔案慣例用於在 app 路由器 中定義路由和處理詮釋資料。

路由檔案

layout.js .jsx .tsx佈局
page.js .jsx .tsx頁面
loading.js .jsx .tsx載入中 UI
not-found.js .jsx .tsx找不到頁面 UI
error.js .jsx .tsx錯誤 UI
global-error.js .jsx .tsx全域錯誤 UI
route.js .tsAPI 端點
template.js .jsx .tsx重新渲染的佈局
default.js .jsx .tsx平行路由後備頁面

巢狀路由

folder路由區段
folder/folder巢狀路由區段

動態路由
[folder]動態路由區段
[...folder]全符合路由區段
[[...folder]]可選的全符合路由區段

路由群組和私有資料夾

(資料夾)在不影響路由的情況下將路由分組
_資料夾將資料夾及其所有子區段排除在路由之外

平行和攔截路由
@資料夾命名插槽
(.)資料夾攔截同一層級
(..)資料夾攔截上一層級
(..)(..)資料夾攔截上兩層級
(...)資料夾從根目錄攔截

詮釋資料檔案慣例 應用程式圖示
網站圖示.ico網站圖示檔案
圖示.ico .jpg .jpeg .png .svg應用程式圖示檔案
圖示.js .ts .tsx產生的應用程式圖示
apple-icon.jpg .jpeg, .pngApple 應用程式圖示檔案
apple-icon.js .ts .tsx產生的 Apple 應用程式圖示

Open Graph 和 Twitter 圖片
opengraph-image.jpg .jpeg .png .gifOpen Graph 圖片檔案
opengraph-image.js .ts .tsx已產生的 Open Graph 圖片
twitter-image.jpg .jpeg .png .gifTwitter 圖片檔案
twitter-image.js .ts .tsx已產生的 Twitter 圖片

SEO

sitemap.xmlSitemap 檔案
sitemap.js .ts已產生的 Sitemap
robots.txtRobots 檔案
robots.js .ts已產生的 Robots 檔案

pages 路由慣例

以下檔案慣例用於定義 pages 路由器 中的路由。

特殊檔案

_app.js .jsx .tsx自訂應用程式
_document.js .jsx .tsx自訂文件
_error.js .jsx .tsx自訂錯誤頁面
404.js .jsx .tsx404 錯誤頁面
500.js .jsx .tsx500 錯誤頁面

路由

資料夾慣例
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可選的全符合路由區段