跳到主要内容

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 后,路由会配置为:/api/* 请求由 serverless function 处理;静态资源(JS、CSS)由 Vercel Edge CDN 提供,不会调用 function;所有其他路径均回退到 index.html,以进行客户端路由。

此文件夹在构建期间生成,不应提交到版本控制系统。