跳至主要內容

VercelDeployer

VercelDeployer 會封裝你的 Mastra 伺服器,並產生符合 Vercel Build Output API 的輸出。Vercel 會直接將此輸出部署為無伺服器函式。

安裝
安裝 的直接連結

要使用 VercelDeployer,請安裝 @mastra/deployer-vercel 套件:

npm install @mastra/deployer-vercel@latest

使用範例
使用範例 的直接連結

匯入 VercelDeployer,並在 Mastra 設定中將其設為 deployer:

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { VercelDeployer } from '@mastra/deployer-vercel'

export const mastra = new Mastra({
deployer: new VercelDeployer(),
})

建構函式選項
建構函式選項 的直接連結

Deployer 接受以下選項:

  • studio?: boolean:將 Studio 與 API 一併部署為由 Vercel Edge CDN 提供的靜態資源。預設為 false
  • maxDuration?: number:函式執行逾時時間(秒)
  • memory?: number:函式記憶體(MB)
  • regions?: string[]:部署函式的區域(例如 ['sfo1','iad1']

maxDurationmemoryregions 選項會合併至 .vercel/output/functions/index.func/.vc-config.json,同時保留預設欄位(handlerlauncherTyperuntimeshouldAddHelpers)。

覆寫選項範例
覆寫選項範例 的直接連結

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { VercelDeployer } from '@mastra/deployer-vercel'

export const mastra = new Mastra({
deployer: new VercelDeployer({
studio: true,
maxDuration: 600,
memory: 1536,
regions: ['sfo1', 'iad1'],
}),
})

建置輸出
建置輸出 的直接連結

執行 mastra build 後,deployer 會產生符合 Vercel Build Output API (v3).vercel/output 目錄。該目錄包含路由設定及無伺服器函式套件,Vercel 會略過框架偵測並直接部署這些內容。

輸出包含:

  • config.json:將請求導向適當 handler 的路由設定
  • functions/:封裝為無伺服器函式的 Mastra 伺服器
  • static/:Studio SPA 資源(僅限 studio: true

不含 Studio:

.vercel/
└── output/
├── config.json
└── functions/
└── index.func/
├── .vc-config.json
├── index.mjs
└── node_modules/

使用 studio: true

.vercel/
└── output/
├── config.json
├── static/
│ ├── index.html
│ └── assets/
│ ├── *.js
│ └── *.css
└── functions/
└── index.func/
├── .vc-config.json
├── index.mjs
└── node_modules/

啟用 Studio 後,路由會設定為由無伺服器函式處理 /api/* 請求;靜態資源(JS、CSS)則由 Vercel Edge CDN 提供,不會調用函式;所有其他路徑都會回退至 index.html,以便在客戶端處理路由。

此資料夾會在建置期間產生,不應提交至版本控制系統。