Skip to main content

适配器

在部署 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:

其他平台存在额外的 社区提供的适配器

¥Additional community-provided adapters exist for other platforms.

使用适配器(Using adapters)

¥Using adapters

你的适配器在 svelte.config.js 中指定:

¥Your adapter is specified in svelte.config.js:

svelte.config
import const adapter: (opts: any) => import("@sveltejs/kit").Adapteradapter from 'svelte-adapter-foo';

/** @type {import('@sveltejs/kit').Config} */
const const config: Config
@type{import('@sveltejs/kit').Config}
config
= {
Config.kit?: KitConfig | undefined

SvelteKit options

kit
: {
KitConfig.adapter?: Adapter | undefined

Your adapter is run when executing vite build. It determines how the output is converted for different platforms.

@defaultundefined
adapter
: function adapter(opts: any): import("@sveltejs/kit").Adapteradapter({
// adapter options go here }) } }; export default const config: Config
@type{import('@sveltejs/kit').Config}
config
;

平台特定上下文(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.