【Next.js】 部署模式總覽

下面整理 Next.js 的主要部署類型、每種模式的特性與常見應用場景,並特別解釋 standalone 是什麼以及它適合的情境。


1️⃣ Next.js 部署模式總覽

部署類型 next.config.js / 指令 產物 特性與適用場景
1. Node Server (傳統模式) next build && next start .next/ 預設,完整支援 SSR、ISR、API Routes。部署到任何 Node 環境(PM2、Docker、K8s、AWS EC2、GCP、Heroku…)。
2. Standalone output: 'standalone' + next build .next/standalone + .next/static 只輸出最小可執行檔(server.js + 必要檔案),方便 Docker/容器化。功能與 Node Server 相同(SSR/ISR/API Routes 皆可)。
3. Static Export next exportoutput: 'export' out/ 完全靜態 HTML/CSS/JS。無 SSR/ISR/API Routes。適合純內容網站、部落格、文件站。
4. Edge Runtime / Vercel runtime: 'edge' 或自動判斷 Vercel Edge Functions (或 Cloudflare Workers) 以 Web 標準 API 為基礎,無 Node built-ins。啟動極快、全球節點分布,適合 Middleware、即時驗證、A/B 測試。
5. Serverless Functions Vercel / AWS Lambda Lambda zip 每個頁面/Route 轉成獨立 Serverless Function。可自動擴展,適合不維護長駐伺服器的架構。
6. Custom Server 自行建立 server.jsnext() .next/ 自定義 Node 伺服器,整合 Koa/Express/Fastify,做複雜路由或中介層。
註:模式之間可以組合,例如 Node Server + StandaloneVercel Serverless + Edge Middleware

模式 需 Node.js Runtime 常見部署基底 代表場景
Node Server Node image / VM 全功能 SSR/ISR + API
Standalone ✅ (但包體精簡) Node image / VM Docker/K8s 輕量部署
Static Export Nginx、GitHub Pages、S3+CDN 純靜態內容
Edge Runtime ❌ (Web Worker API) Vercel Edge / Cloudflare Workers 全球低延遲運算、Middleware
Serverless Functions ✅ (平台提供) AWS Lambda / Vercel Functions 無伺服器、自動擴展
Custom Server Node image / VM 自訂路由、混合架構

2️⃣ 各模式詳解

Node Server (預設)


Standalone


Static Export


Edge Runtime / Middleware


Serverless Functions (Vercel / AWS Lambda)


Custom Server


3️⃣ 如何選擇

需求 建議模式
企業內部、自管 Docker/K8s Standalone (Node Server):產物小、部署快
純靜態內容、無後端邏輯 Static Export
全球極低延遲、近使用者運算 Edge Runtime / Middleware
彈性擴展、免維運 Serverless Functions (Vercel / AWS Lambda)
需與自訂 Node 架構整合 Custom Server

✅ 總結


📊 Next.js 部署模式功能支援度

部署模式 需要 Node Runtime SSR ISR API Routes 適用場景
Node Server (next build && next start) 傳統部署,功能完整。適合自管伺服器、Docker、K8s。
Standalone (output: 'standalone') 與 Node Server 相同,但打包精簡,適合容器化。
Static Export (next export) 只能輸出純 HTML/JS/CSS。適合靜態網站(部落格、Landing Page)。
Serverless Functions (Vercel / AWS Lambda) ✅(平台提供) 拆分成獨立 Functions,自動擴展。適合流量不穩的專案。
Edge Runtime (runtime: 'edge') ❌(Web API 而非 Node) ⚠️ 部分支援(僅 Server Components,可撈 API) ⚠️ 部分支援(需 Web API,不支援 Node drivers) 全球低延遲運算、Middleware、A/B 測試。
Custom Server (Express/Koa/Fastify + Next) 自訂路由/中介層,整合既有 Node 架構。

📌 補充說明


🎯 總結


 


修訂版本 #3
由 treeman 建立於 17 E 2025 10:27:58
由 treeman 更新於 17 E 2025 10:42:43