跳至主要内容

[NextJS] Deployment

內容已過時

next export 指令已在 Next.js 13.3 中棄用,並在 Next.js 14 中正式移除。現在要產生靜態檔案,請在 next.config.js 中設定 output: 'export',然後執行 next build 即可。

打包靜態檔案前的動作

參考資料

Static Exports @ Next.js Docs

如果想要將 Next.js 的專案部署到靜態網站上,在 next.config.js 中設定 output: 'export',然後執行 next build 即可:

// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'export',
// 可選:指定輸出目錄(預設為 'out')
// distDir: 'dist',
};

module.exports = nextConfig;
# 執行 build,靜態檔案會產生在 out/ 目錄
$ npx next build

# 在本地執行靜態檔
$ npx serve out
舊版做法(Next.js 13.3 以前)
# export 之前要先 build
$ npx next build

# 執行 export(此指令已在 Next.js 14 中移除)
$ npx next export -o dist

Vercel

可以說是最簡單的方式。Vercel 可以直接整合在 Github 或 Gitlab 上的專案,選擇專案後,只要按下「Deploy」,就可以完成部署。

Netlify

和 Vercel 類似,Netlify 也可以直接整合在 Github 或 Gitlab 上的專案,選擇專案後,只要按下「Deploy」,就可以完成部署。如果是靜態站的話,也使用 Manual Deploy,將檔案(例如 /dist)用拖曳上傳的方式亦可。

Nginx on DigitalOcean