【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 export 或 output: '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.js 並 next() |
.next/ |
自定義 Node 伺服器,整合 Koa/Express/Fastify,做複雜路由或中介層。 |
註:模式之間可以組合,例如 Node Server + Standalone、Vercel Serverless + Edge Middleware。
| 模式 |
|---|
2️⃣ 各模式詳解
① Node Server (預設)
-
啟動:
next build && next start -
產物:
.next/目錄 -
特性:完整支援 SSR、ISR、API Routes。
-
適用:自行管理伺服器(EC2、Kubernetes、Docker Swarm、Heroku…)。
-
優缺:最彈性,但部署包體積較大,需要完整 node_modules。
② Standalone ⭐
-
設定:
// next.config.js module.exports = { output: 'standalone', } -
產物:
.next/standalone # 精簡 Node 專案,可直接 node server.js .next/static # 靜態資源 -
原理:
-
next build會分析依賴,只把執行需要的檔案、node_modules 打包進standalone。 -
不再需要完整專案原始碼,也不必把整個 node_modules 打進 Docker。
-
-
適用場景:
-
Docker / 容器化:
只複製.next/standalone+.next/static,image 更小、build 更快。 -
Kubernetes / ECS:
配合多階段 Docker build,把RUN npm ci階段與最終執行環境分離。
-
-
功能:與 Node Server 相同(SSR / ISR / API Routes 全支援)。
③ Static Export
-
指令:
next build && next export -
產物:
out/完全靜態 -
限制:無 SSR、無 API Routes、無 ISR。
-
適用:
-
部落格、說明文件、Landing Page
-
部署到任何靜態空間(S3+CloudFront、GitHub Pages、Netlify)。
-
④ Edge Runtime / Middleware
-
用法:在頁面或 route handler 中
export const runtime = 'edge' -
部署:Vercel Edge Functions、Cloudflare Workers、Deno Deploy。
-
特性:全球節點極低延遲;不能用 Node built-ins。
-
適用:
-
驗證/權限檢查
-
A/B 測試、URL 轉向
-
即時 Header/Cookie 操作
-
⑤ Serverless Functions (Vercel / AWS Lambda)
-
部署:Vercel 自動化,或
serverless-http將 Next 整包轉為 Lambda。 -
特性:
-
每個頁面/路由轉成獨立 Lambda。
-
自動擴展、免維運。
-
-
適用:
-
高併發但流量不穩定。
-
只需 Node 短期計算、不需長連線。
-
⑥ Custom Server
-
方式:建立
server.jsconst express = require('express') const next = require('next') const app = next({ dev: false }) const handle = app.getRequestHandler() app.prepare().then(() => { const server = express() server.get('/health', (req, res) => res.send('ok')) server.all('*', (req, res) => handle(req, res)) server.listen(3000) }) -
適用:
-
需要與既有 Node 架構整合。
-
自訂路由、中介層或 WebSocket。
-
3️⃣ 如何選擇
| 需求 | 建議模式 |
|---|---|
| 企業內部、自管 Docker/K8s | Standalone (Node Server):產物小、部署快 |
| 純靜態內容、無後端邏輯 | Static Export |
| 全球極低延遲、近使用者運算 | Edge Runtime / Middleware |
| 彈性擴展、免維運 | Serverless Functions (Vercel / AWS Lambda) |
| 需與自訂 Node 架構整合 | Custom Server |
✅ 總結
-
Standalone 是 Node Server 模式的輕量化打包,非常適合容器化部署。
-
如果要保留完整 SSR、ISR、API Routes,又希望 映像檔小、啟動快,選 Standalone 幾乎是最佳解。
-
其他模式(Static Export、Edge、Serverless)則依你的應用是否需要即時運算、全球分佈或純靜態內容來決定。