适配器
在部署 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:
@sveltejs/adapter-cloudflare
用于 Cloudflare Pages¥
@sveltejs/adapter-cloudflare
for Cloudflare Pages@sveltejs/adapter-cloudflare-workers
用于 Cloudflare Workers¥
@sveltejs/adapter-cloudflare-workers
for Cloudflare Workers@sveltejs/adapter-netlify
用于 Netlify¥
@sveltejs/adapter-netlify
for Netlify@sveltejs/adapter-node
用于 Node 服务器¥
@sveltejs/adapter-node
for Node servers@sveltejs/adapter-static
用于静态站点生成 (SSG)¥
@sveltejs/adapter-static
for static site generation (SSG)@sveltejs/adapter-vercel
用于 Vercel¥
@sveltejs/adapter-vercel
for 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").Adapter
adapter from 'svelte-adapter-foo';
/** @type {import('@sveltejs/kit').Config} */
const const config: 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.
adapter: function adapter(opts: any): import("@sveltejs/kit").Adapter
adapter({
// adapter options go here
})
}
};
export default const config: 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.