适配器
在部署 SvelteKit 应用之前,你需要根据部署目标对其进行调整。适配器是将构建的应用作为输入并生成输出以供部署的小插件。
¥Before you can deploy your SvelteKit app, you need to adapt it for your deployment target. Adapters are small plugins that take the built app as input and generate output for deployment.
各种平台都有官方适配器 - 这些适配器记录在以下页面上:
¥Official adapters exist for a variety of platforms — these are documented on the following pages:
适用于 Cloudflare Workers 和 Cloudflare Pages 的
@sveltejs/adapter-cloudflare¥
@sveltejs/adapter-cloudflarefor Cloudflare Workers and Cloudflare Pages@sveltejs/adapter-netlify用于 Netlify¥
@sveltejs/adapter-netlifyfor Netlify@sveltejs/adapter-node用于 Node 服务器¥
@sveltejs/adapter-nodefor Node servers@sveltejs/adapter-static用于静态站点生成 (SSG)¥
@sveltejs/adapter-staticfor static site generation (SSG)@sveltejs/adapter-vercel用于 Vercel¥
@sveltejs/adapter-vercelfor Vercel
其他平台存在额外的 社区提供的适配器。
¥Additional community-provided adapters exist for other platforms.
使用适配器(Using adapters)
¥Using adapters
你的适配器在 svelte.config.js 中指定:
¥Your adapter is specified in svelte.config.js:
import const adapter: (opts: any) => import("@sveltejs/kit").Adapteradapter from 'svelte-adapter-foo';
/** @type {import('@sveltejs/kit').Config} */
const const config: Configconfig = {
Config.kit?: KitConfig | undefinedSvelteKit options
kit: {
KitConfig.adapter?: Adapter | undefinedYour adapter is run when executing vite build. It determines how the output is converted for different platforms.
adapter: function adapter(opts: any): import("@sveltejs/kit").Adapteradapter({
// adapter options go here
})
}
};
export default const config: Configconfig;平台特定上下文(Platform-specific context)
¥Platform-specific context
某些适配器可能有权访问有关请求的其他信息。例如,Cloudflare Workers 可以访问包含 KV 命名空间等的 env 对象。这可以作为 platform 属性传递给 hooks 和 服务器路由 中使用的 RequestEvent — 请参阅每个适配器的文档以了解更多信息。
¥Some adapters may have access to additional information about the request. For example, Cloudflare Workers can access an env object containing KV namespaces etc. This can be passed to the RequestEvent used in hooks and server routes as the platform property — consult each adapter’s documentation to learn more.