> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # 將 Mastra 部署至 Netlify 使用 `@mastra/deployer-netlify` 將 Mastra 伺服器部署至 Netlify。deployer 會捆綁你的程式碼,並產生符合 Netlify [Frameworks API](https://docs.netlify.com/build/frameworks/frameworks-api/) 的 `.netlify` 目錄,供你直接部署。你可以部署為無伺服器函數(預設),亦可部署為延遲較低、執行時間較長的[邊緣函數](https://docs.netlify.com/build/edge-functions/overview/)。 > **資訊:** 本指南涵蓋如何部署 [Mastra 伺服器](https://mastra.zisheng.pro/zh-HK/docs/server/mastra-server)。如你使用[伺服器配接器](https://mastra.zisheng.pro/zh-HK/docs/server/server-adapters)或[網絡框架](https://mastra.zisheng.pro/zh-HK/docs/deployment/web-framework),請按該框架的一般方式部署。 ## 開始之前 你需要一個 [Mastra 應用程式](https://mastra.zisheng.pro/zh-HK/guides/getting-started/quickstart)和一個 [Netlify](https://www.netlify.com/) 帳戶。 > **注意:** Netlify Functions 使用暫時性檔案系統,因此你設定的任何儲存空間(包括可觀測性儲存空間)都必須託管於外部。如你使用的 [LibSQLStore](https://mastra.zisheng.pro/zh-HK/reference/storage/libsql) 採用檔案 URL,請改用遙距託管的資料庫。 ## 安裝 將 `@mastra/deployer-netlify` 依賴套件加入項目: **npm**: ```bash npm install @mastra/deployer-netlify@latest ``` **pnpm**: ```bash pnpm add @mastra/deployer-netlify@latest ``` **Yarn**: ```bash yarn add @mastra/deployer-netlify@latest ``` **Bun**: ```bash bun add @mastra/deployer-netlify@latest ``` 匯入 [`NetlifyDeployer`](https://mastra.zisheng.pro/zh-HK/reference/deployer/netlify),並在 Mastra 設定中將它設為 deployer: ```typescript import { Mastra } from '@mastra/core' import { NetlifyDeployer } from '@mastra/deployer-netlify' export const mastra = new Mastra({ deployer: new NetlifyDeployer(), }) ``` 如要改為部署成邊緣函數,請傳入 `{ target: 'edge' }`: ```typescript import { Mastra } from '@mastra/core' import { NetlifyDeployer } from '@mastra/deployer-netlify' export const mastra = new Mastra({ deployer: new NetlifyDeployer({ target: 'edge', }), }) ``` 邊緣函數在最接近使用者的邊緣位置透過 Deno 執行。它們沒有固定的執行逾時限制(只有 CPU 時間限制),因此更適合執行時間較長的 AI Workflow。詳情請參閱[建構函數選項](https://mastra.zisheng.pro/zh-HK/reference/deployer/netlify)。 在項目根目錄建立包含以下內容的 `netlify.toml` 檔案: ```toml [build] command = "mastra build" ``` ## 部署 設定好項目後,將它推送至你選用的遙距 Git Provider(例如 GitHub)。 1. 將你的程式碼儲存庫連接至 Netlify。在設定過程中,Netlify 會偵測你的 `netlify.toml` 檔案,並將建置命令設為 `mastra build`。 > **備註:** 請記得設定執行應用程式所需的環境變數(例如你的[模型 Provider](https://mastra.zisheng.pro/zh-HK/models/providers) API 金鑰)。 2. 準備好後,按一下 **Deploy** 按鈕,並等待首次部署完成。系統會通知你已有一個 函數完成部署。 3. 在 `https://.netlify.app/api/agents` 驗證部署;該網址應傳回你的 Agent JSON 清單。 由於 [Mastra 伺服器](https://mastra.zisheng.pro/zh-HK/docs/server/mastra-server)會在每個 API 端點前加上 `/api`,因此發出請求時必須將它加入 URL。 > **資訊:** Netlify 函數通常部署於 `https://.netlify.app/.netlify/functions/api`。`NetlifyDeployer` 會將所有來自 `/*` 的請求重新導向至 `/.netlify/functions/api/:splat`,因此 URL 無需包含 `.netlify/functions` 前綴。 4. 現在你可以透過 HTTP 呼叫 Mastra 端點。 > **注意:** 對外公開端點前,請先設定[驗證](https://mastra.zisheng.pro/zh-HK/docs/server/auth)。