Cloudflare Workers
要部署到 Cloudflare Workers,请使用 adapter-cloudflare-workers
。
¥To deploy to Cloudflare Workers, use adapter-cloudflare-workers
.
除非你有使用
adapter-cloudflare-workers
的特定原因,否则建议你改用adapter-cloudflare
。两个适配器都具有同等功能,但 Cloudflare Pages 提供的功能包括 GitHub 集成、自动构建和部署、预览部署、即时回滚等。¥[!NOTE] Unless you have a specific reason to use
adapter-cloudflare-workers
, it’s recommended that you useadapter-cloudflare
instead. Both adapters have equivalent functionality, but Cloudflare Pages offers features like GitHub integration with automatic builds and deploys, preview deployments, instant rollback and so on.
用法(Usage)
¥Usage
使用 npm i -D @sveltejs/adapter-cloudflare-workers
安装,然后将适配器添加到你的 svelte.config.js
:
¥Install with npm i -D @sveltejs/adapter-cloudflare-workers
, then add the adapter to your svelte.config.js
:
import import adapter
adapter from '@sveltejs/adapter-cloudflare-workers';
export default {
kit: {
adapter: any;
}
kit: {
adapter: any
adapter: import adapter
adapter({
// see below for options that can be set here
})
}
};
选项(Options)
¥Options
config
Wrangler 配置文件 的路径。如果你想使用 wrangler.jsonc
以外的 Wrangler 配置文件名,你可以使用此选项指定它。
¥Path to your Wrangler configuration file. If you would like to use a Wrangler configuration filename other than wrangler.jsonc
, you can specify it using this option.
platformProxy
模拟 platform.env
本地绑定的首选项。有关完整选项列表,请参阅 getPlatformProxy Wrangler API 文档。
¥Preferences for the emulated platform.env
local bindings. See the getPlatformProxy Wrangler API documentation for a full list of options.
基本配置(Basic Configuration)
¥Basic Configuration
此适配器希望在项目根目录中找到 Wrangler 配置文件。它看起来应该像这样:
¥This adapter expects to find a Wrangler configuration file in the project root. It should look something like this:
{
"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"
}
<your-service-name>
可以是任何东西。可以通过使用 Wrangler CLI 工具运行 wrangler whoami
或登录 Cloudflare 仪表板 并从 URL 末尾获取它来找到 <your-account-id>
:
¥<your-service-name>
can be anything. <your-account-id>
can be found by running wrangler whoami
using the Wrangler CLI tool or by logging into your Cloudflare dashboard and grabbing it from the end of the URL:
https://dash.cloudflare.com/<your-account-id>/home
你应该将
.cloudflare
目录(或你为main
和site.bucket
指定的任何目录)和.wrangler
目录添加到你的.gitignore
。¥[!NOTE] You should add the
.cloudflare
directory (or whichever directories you specified formain
andsite.bucket
) and the.wrangler
directory to your.gitignore
.
你需要安装 Wrangler 并登录(如果尚未安装):
¥You will need to install Wrangler and log in, if you haven’t already:
npm i -D wrangler
wrangler login
然后,你可以构建应用并部署它:
¥Then, you can build your app and deploy it:
wrangler deploy
运行时 API(Runtime APIs)
¥Runtime APIs
env
对象包含你项目的 bindings,其中包括 KV/DO 命名空间等。它通过 platform
属性传递给 SvelteKit,以及 context
、caches
和 cf
,这意味着你可以在钩子和端点中访问它:
¥The env
object contains your project’s bindings, which consist of KV/DO namespaces, etc. It is passed to SvelteKit via the platform
property, along with context
, caches
, and cf
, meaning that you can access it in hooks and endpoints:
export async function function POST({ request, platform }: {
request: any;
platform: any;
}): Promise<void>
POST({ request, platform }) {
const const x: any
x = platform: any
platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
}
对于环境变量,SvelteKit 的内置
$env
模块 应该是首选。¥[!NOTE] SvelteKit’s built-in
$env
module should be preferred for environment variables.
要使这些类型可用于你的应用,请安装 @cloudflare/workers-types
并在你的 src/app.d.ts
中引用它们:
¥To make these types available to your app, install @cloudflare/workers-types
and reference them in your src/app.d.ts
:
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 {};本地测试(Testing Locally)
¥Testing Locally
在开发和预览模式下,会模拟 platform
属性中的 Cloudflare Workers 特定值。本地 bindings 是基于你的 Wrangler 配置文件 创建的,用于在开发和预览期间填充 platform.env
。使用适配器配置 platformProxy
选项 更改绑定的首选项。
¥Cloudflare Workers specific values in the platform
property are emulated during dev and preview modes. Local bindings are created based on your Wrangler configuration file and are used to populate platform.env
during development and preview. Use the adapter config platformProxy
option to change your preferences for the bindings.
要测试构建,你应该使用 Wrangler 版本 3。构建站点后,运行 wrangler dev
。
¥For testing the build, you should use Wrangler version 3. Once you have built your site, run wrangler dev
.
故障排除(Troubleshooting)
¥Troubleshooting
Node.js 兼容性(Node.js compatibility)
¥Node.js compatibility
如果你想启用 Node.js 兼容性,你可以将 nodejs_compat
兼容性标志添加到你的 Wrangler 配置文件中:
¥If you would like to enable Node.js compatibility, you can add the nodejs_compat
compatibility flag to your Wrangler configuration file:
{
"compatibility_flags": ["nodejs_compat"]
}
Worker 大小限制(Worker size limits)
¥Worker size limits
部署应用时,SvelteKit 生成的服务器将打包到单个文件中。如果在缩小后超过 大小限制,Wrangler 将无法发布你的工作程序。你通常不太可能达到此限制,但一些大型库可能会导致这种情况发生。在这种情况下,你可以尝试通过仅在客户端导入此类库来减小工作器的大小。有关更多信息,请参阅 常见问题解答。
¥When deploying your application, the server generated by SvelteKit is bundled into a single file. Wrangler will fail to publish your worker if it exceeds the size limits after minification. You’re unlikely to hit this limit usually, but some large libraries can cause this to happen. In that case, you can try to reduce the size of your worker by only importing such libraries on the client side. See the FAQ for more information.
访问文件系统(Accessing the file system)
¥Accessing the file system
你不能在 Cloudflare Workers 中使用 fs
— 你必须 prerender 相关路由。
¥You can’t use fs
in Cloudflare Workers — you must prerender the routes in question.