> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/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-TW/docs/server/mastra-server)。若你使用[伺服器轉接器](https://mastra.zisheng.pro/zh-TW/docs/server/server-adapters)或 [Web 框架](https://mastra.zisheng.pro/zh-TW/docs/deployment/web-framework),請依該框架平常的方式部署。 ## 開始之前 你需要一個 [Mastra 應用程式](https://mastra.zisheng.pro/zh-TW/guides/getting-started/quickstart)和一個 [Netlify](https://www.netlify.com/) 帳號。 > **警告:** Netlify Functions 使用暫時性檔案系統,因此設定的任何儲存空間(包括可觀測性儲存空間)都必須託管於外部。若你使用的 [LibSQLStore](https://mastra.zisheng.pro/zh-TW/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-TW/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-TW/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-TW/models/providers) 的 API 金鑰)。 2. 準備就緒後,按一下 **Deploy** 按鈕,等待第一次部署完成。畫面會顯示已部署一個函式。 3. 前往 `https://.netlify.app/api/agents` 驗證部署;頁面應傳回 Agent 的 JSON 清單。 由於 [Mastra 伺服器](https://mastra.zisheng.pro/zh-TW/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-TW/docs/server/auth)。