跳到主要內容

建置和部署

撰寫適配器

在 GitHub 上編輯此頁面

如果尚不存在適用於您偏好環境的適配器,您可以自行建置。我們建議查看類似於您平台的適配器原始碼,並將其複製作為起點。

適配器套件實作下列 API,它會建立一個適配器

ts
/** @param {AdapterSpecificOptions} options */
export default function (options) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: 'adapter-package-name',
async adapt(builder) {
// adapter implementation
},
async emulate() {
return {
async platform({ config, prerender }) {
Type '({ config, route }: { config: any; route: { id: string; }; }) => void' is not assignable to type '(details: { config: any; route: { id: string; }; }) => boolean'. Type 'void' is not assignable to type 'boolean'.2322Type '({ config, route }: { config: any; route: { id: string; }; }) => void' is not assignable to type '(details: { config: any; route: { id: string; }; }) => boolean'. Type 'void' is not assignable to type 'boolean'.
// the returned object becomes `event.platform` during dev, build and
// preview. Its shape is that of `App.Platform`
}
}
},
supports: {
read: ({ config, route }) => {
// Return `true` if the route with the given `config` can use `read`
// from `$app/server` in production, return `false` if it can't.
// Or throw a descriptive error describing how to configure the deployment
}
}
};
return adapter;
}

其中,名稱適應是必要的。模擬支援是選用的。

適應方法中,適配器應該執行多項工作

  • 清除建置目錄
  • 使用builder.writeClientbuilder.writeServerbuilder.writePrerendered撰寫 SvelteKit 輸出
  • 輸出程式碼
    • ${builder.getServerDirectory()}/index.js匯入伺服器
    • 使用 builder.generateManifest({ relativePath }) 產生的清單實例化應用程式
    • 偵聽來自平台的請求,必要時將其轉換為標準的請求,呼叫server.respond(request, { getClientAddress })函式以產生回應並以它回應
    • 透過傳遞給server.respondplatform選項,向 SvelteKit 公開任何平台特定的資訊
    • 必要時,在目標平台上全面模擬fetch以使其正常運作。SvelteKit 提供一個@sveltejs/kit/node/polyfills輔助程式,適用於可以使用undici的平台
  • 必要時,將輸出打包以避免需要在目標平台上安裝相依項
  • 將使用者的靜態檔案和產生的 JS/CSS 放置在目標平台的正確位置

如果可行,我們建議將轉接器輸出放在 build/ 目錄下,任何中間輸出則放在 .svelte-kit/[轉接器名稱] 下。

上一個 Vercel
下一個 進階路由