跳到主要内容

NetlifyDeployer

NetlifyDeployer 类通过调整 Mastra 的输出,创建服务器的优化版本,以处理打包、配置和部署。它扩展了基础 Deployer 类,增加了 Netlify 专用功能。借助该类,你可以在 Netlify serverless function 或 edge function 中运行 Mastra。

安装
安装的直接链接

要使用 NetlifyDeployer,需要安装 @mastra/deployer-netlify 包:

npm install @mastra/deployer-netlify@latest

使用示例
使用示例的直接链接

导入 NetlifyDeployer,并在 Mastra 配置中将其设为 deployer:

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

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

构造函数选项
构造函数选项的直接链接

  • target?: 'serverless' | 'edge':Netlify 的部署目标。默认为 'serverless'
    • 'serverless':标准 Netlify Functions(Node.js 运行时,默认超时 60 秒)。
    • 'edge'Netlify Edge Functions(基于 Deno 的运行时,在离用户最近的边缘位置运行,没有固定超时时间)。

Edge function 示例
Edge function 示例的直接链接

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

export const mastra = new Mastra({
deployer: new NetlifyDeployer({
target: 'edge',
}),
})

输出
输出的直接链接

运行 mastra build 后,deployer 会生成 .netlify 文件夹。构建输出包含项目中的所有 Agent、Tool 和 Workflow,以及用于配置 Netlify Frameworks APIconfig.json 文件。

Serverless 输出(默认)
Serverless 输出(默认)的直接链接

your-project/
└── .netlify/
└── v1/
├── config.json
└── functions/
└── api/
├── index.mjs
├── package.json
└── node_modules/

config.json 文件包含:

{
"functions": {
"directory": ".netlify/v1/functions",
"node_bundler": "none",
"included_files": [".netlify/v1/functions/**"]
},
"redirects": [
{
"force": true,
"from": "/*",
"to": "/.netlify/functions/api/:splat",
"status": 200
}
]
}

Edge 输出
Edge 输出的直接链接

your-project/
└── .netlify/
└── v1/
├── config.json
└── edge-functions/
├── index.mjs
├── package.json
└── node_modules/

config.json 文件包含:

{
"edge_functions": [
{
"function": "index",
"path": "/*"
}
]
}