Next.js 支持多个部署目标,由 output 配置部分控制。正确的选择取决于你是否需要 Node 服务器、容器或纯静态托管。
构建输出模式
js
// next.config.js
module.exports = {
// output: undefined → default: needs a Node.js server (full features)
};
运行 Node.js 服务器,所以你获得所有功能:SSR、ISR、Server Actions、Route Handlers、图像优化、中间件。这是 Vercel(Next.js 的制造者,零配置)和 Node 托管平台使用的方式。
# Next traces only the needed files into .next/standalone → a tiny, self-contained server
COPY --from=builder /app/.next/standalone ./
CMD ["node", "server.js"]
standalone 生成一个最小的文件夹,仅包含实际使用的依赖项,非常适合在 AWS、GCP、你自己的服务器等上构建小型 Docker 镜像。你保留所有服务器功能。
next build → out/ (plain HTML/CSS/JS, deployable to any static host: S3, GitHub Pages, Netlify CDN)
静态导出提供最便宜、最简单的托管,但禁用服务器功能:无 SSR、无 ISR、无 Route Handlers、无 Server Actions、无即时图像优化。仅适用于完全静态的网站。
Need SSR/ISR/Server Actions, want zero-config → Vercel (or a Node host) — default output
Want containers / self-host with full features → output: "standalone" + Docker
Purely static content (docs, marketing) → output: "export" → any static CDN
- Runtime choice (Edge vs Node) affects where functions run
- Self-hosting ISR/caching needs persistent storage or a shared cache
- Image optimization on non-Vercel hosts may need a custom loader
部署并非一刀切:output 模式决定了哪些 Next.js 功能能够保留。
了解到 default/Node 输出保留所有服务器能力,standalone 为 Docker/自托管打包它们,而 export 用纯静态托管替换所有服务器功能,让你能够将构建与你的基础设施相匹配 — 并避免 output: "export" 以静默方式破坏 SSR/ISR/Server Actions 的陷阱。