Skip to main content

Cloudflare Workers

[注意] adapter-cloudflare-workers 已弃用,推荐使用 adapter-cloudflare。建议使用 adapter-cloudflare 将静态资源部署到 Cloudflare Workers,因为 Cloudflare Workers 网站将弃用,推荐使用它。

要部署到Cloudflare WorkersWorkers Sites,请使用adapter-cloudflare-workers

使用

安装 npm i -D @sveltejs/adapter-cloudflare-workers 后,将适配器添加到您的svelte.config.js中:

svelte.config
import import adapteradapter from '@sveltejs/adapter-cloudflare-workers';

export default {
	
kit: {
    adapter: any;
}
kit
: {
adapter: anyadapter: import adapteradapter({ // see below for options that can be set here }) } };

选项

配置

路径到您的Wrangler 配置文件。如果您想使用除wrangler.jsoncwrangler.jsonwrangler.toml之外的 Wrangler 配置文件名,可以使用此选项指定。

平台代理

偏好于模拟的 platform.env 本地绑定。请参阅 getPlatformProxy Wrangler API 文档以获取完整选项列表。

基本配置

此适配器期望在项目根目录中找到一个Wrangler 配置文件。它应该看起来像这样:

wrangler
{
	"name": "<your-service-name>",
	"account_id": "<your-account-id>",
	"main": "./.cloudflare/worker.js",
	"site": {
		"bucket": "./.cloudflare/public"
	},
	"build": {
		"command": "npm run build"
	},
	"compatibility_date": "2021-11-12"
}

您的服务名称可以是任何内容。您的账户 ID可以通过运行wrangler whoami使用 Wrangler CLI 工具或通过登录您的Cloudflare 控制台并从 URL 末尾获取。

https://dash.cloudflare.com/<your-account-id>/home

[!注意] 您应将 .cloudflare 目录(或您指定的 mainsite.bucket 目录)以及 .wrangler 目录添加到您的 .gitignore 中。

您需要安装Wrangler并登录,如果您还没有的话:

npm i -D wrangler
wrangler login

然后,您可以构建您的应用程序并部署它:

wrangler 部署

运行时 API

对象 env 包含您项目的 bindings,这些绑定包括 KV/DO 命名空间等。它通过 platform 属性传递给 SvelteKit,同时还包括 ctxcachescf,这意味着您可以在钩子和端点中访问它:

export async function 
function POST({ request, platform }: {
    request: any;
    platform: any;
}): Promise<void>
POST
({ request, platform }) {
const const x: anyx = platform: anyplatform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x'); }

[!注意] 应优先使用 SvelteKit 内置的 $env 模块 来处理环境变量。

为了使这些类型可用于您的应用,安装 @cloudflare/workers-types 并在您的 src/app.d.ts 中引用它们:

src/app.d
import { interface KVNamespace<Key extends string = string>KVNamespace, interface DurableObjectNamespace<T extends Rpc.DurableObjectBranded | undefined = undefined>DurableObjectNamespace } from '@cloudflare/workers-types';

declare global {
	namespace App {
		interface interface App.Platform

If your adapter provides platform-specific context via event.platform, you can specify it here.

Platform
{
App.Platform.env?: {
    YOUR_KV_NAMESPACE: KVNamespace;
    YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
} | undefined
env
?: {
type YOUR_KV_NAMESPACE: KVNamespace<string>YOUR_KV_NAMESPACE: interface KVNamespace<Key extends string = string>KVNamespace; type YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace<undefined>YOUR_DURABLE_OBJECT_NAMESPACE: interface DurableObjectNamespace<T extends Rpc.DurableObjectBranded | undefined = undefined>DurableObjectNamespace; }; } } } export {};

本地测试

Cloudflare Workers 在平台属性中的特定值在开发和预览模式下进行模拟。根据您的Wrangler 配置文件创建本地绑定,并在开发和预览期间用于填充platform.env。使用适配器配置中的platformProxy选项来更改绑定偏好。

测试构建时,您应使用Wrangler版本 4。构建您的网站后,运行wrangler dev

故障排除

Node.js 兼容性

如果您想启用Node.js 兼容性,您可以将nodejs_compat兼容性标志添加到您的 Wrangler 配置文件中:

wrangler
{
	"compatibility_flags": ["nodejs_compat"]
}

工人大小限制

当部署您的应用程序时,由 SvelteKit 生成的服务器被打包成一个单独的文件。如果经过压缩后超过大小限制,Wrangler 将无法发布您的 worker。通常情况下,您不太可能达到这个限制,但一些大型库可能会导致这种情况发生。在这种情况下,您可以尝试通过仅在客户端导入这些库来减小 worker 的大小。有关更多信息,请参阅常见问题解答

访问文件系统

您不能在 Cloudflare Workers 中使用fs——您必须预渲染相关路由。

Edit this page on GitHub llms.txt

previous next