跳至主要內容

VercelDeployer

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

安裝
「安裝」的直接連結

若要使用 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:function 執行逾時時間(秒)
  • memory?: number:function 記憶體(MB)
  • regions?: string[]:要部署 function 的區域(例如 ['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 目錄。此目錄包含路由設定與 serverless function bundle,Vercel 會直接部署這些內容並略過框架偵測。

輸出包含:

  • config.json:將要求導向適當 handler 的路由設定
  • functions/:封裝為 serverless function 的 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 時,路由會設定為由 serverless function 處理 /api/* 要求;靜態資產(JS、CSS)由 Vercel Edge CDN 提供,不會叫用 function;所有其他路徑則回復至 index.html,以進行使用者端路由。

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