Cloudflare Workers
To deploy to Cloudflare Workers, use adapter-cloudflare-workers
.
Comparisons
adapter-cloudflare-workers
– supports all SvelteKit features; builds for Cloudflare Workersadapter-cloudflare
– supports all SvelteKit features; builds for Cloudflare Pagesadapter-static
– only produces client-side static assets; compatible with Cloudflare Pages and Cloudflare Workers
Usage
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
config
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
Preferences for the emulated platform.env
local bindings. See the getPlatformProxy Wrangler API documentation for a full list of options.
Basic configuration
This adapter expects to find a Wrangler configuration file in the project root. It should look something like this:
{
"name": "<your-service-name>",
"main": ".svelte-kit/cloudflare/_worker.js",
"compatibility_date": "2025-01-01",
"assets": {
"binding": "ASSETS",
"directory": ".svelte-kit/cloudflare",
}
}
Deployment
Please follow the framework guide for Cloudflare Workers to begin.
Runtime APIs
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’s built-in
$env
module should be preferred for environment variables.
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
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.
For testing the build, you should use Wrangler version 3. Once you have built your site, run wrangler dev
.
Notes
The _headers
and _redirects
files specific to Cloudflare Pages can be used for static asset responses (like images) by putting them into the project root folder.
Troubleshooting
Node.js compatibility
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 size limits
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
You can’t use fs
in Cloudflare Workers — you must prerender the routes in question.
Migrating from Workers Sites to Workers Static Assets
Cloudflare no longer recommends using Workers Sites and instead recommends using Workers Static Assets. To migrate, remove all site
configuration settings from your Wrangler configuration file and add the assets.directory
and assets.binding
configuration settings:
wrangler.toml
site.bucket = ".cloudflare/public"
assets.directory = ".cloudflare/public"
assets.binding = "ASSETS"
wrangler.jsonc
{
"site": {
"bucket": ".cloudflare/public"
},
"assets": {
"directory": ".cloudflare/public",
"binding": "ASSETS"
}
}