Cloudflare Workers
adapter-cloudflare-workershas been deprecated in favour ofadapter-cloudflare. We recommend usingadapter-cloudflareto deploy to Cloudflare Workers with Static Assets since Cloudflare Workers Sites will be deprecated in favour of it.
To deploy to Cloudflare Workers with Workers Sites, use adapter-cloudflare-workers.
Usage
Install with npm i -D @sveltejs/adapter-cloudflare-workers, then add the adapter to your svelte.config.js:
import import adapteradapter from '@sveltejs/adapter-cloudflare-workers';
/** @type {import('@sveltejs/kit').Config} */
const const config: {
kit: {
adapter: any;
};
}
config = {
kit: {
adapter: any;
}
kit: {
adapter: anyadapter: import adapteradapter({
// see below for options that can be set here
})
}
};
export default const config: {
kit: {
adapter: any;
};
}
config;Options
config
Path to your Wrangler configuration file. If you would like to use a Wrangler configuration filename other than wrangler.jsonc, wrangler.json, or wrangler.toml 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>",
"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> 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>/homeYou should add the
.cloudflaredirectory (or whichever directories you specified formainandsite.bucket) and the.wranglerdirectory to your.gitignore.
You will need to install Wrangler and log in, if you haven’t already:
npm i -D wrangler
wrangler loginThen, you can build your app and deploy it:
wrangler deployRuntime 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 ctx, caches, and cf, meaning that you can access it in hooks and endpoints:
/** @type {import('./$types').RequestHandler} */
export async function POST({ request: RequestThe original request object.
request, platform: Readonly<App.Platform> | undefinedAdditional data made available through the adapter.
platform }) {
const const x: DurableObjectId | undefinedx = platform: Readonly<App.Platform> | undefinedAdditional data made available through the adapter.
platform?.env: {
YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
}
env.type YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace<undefined>YOUR_DURABLE_OBJECT_NAMESPACE.DurableObjectNamespace<undefined>.idFromName(name: string): DurableObjectIdidFromName('x');
}import type { type RequestHandler = (event: RequestEvent<Record<string, any>, string | null>) => MaybePromise<Response>RequestHandler } from './$types';
export const POST: type RequestHandler = (event: RequestEvent<Record<string, any>, string | null>) => MaybePromise<Response>RequestHandler = async ({ request: RequestThe original request object.
request, platform: Readonly<App.Platform> | undefinedAdditional data made available through the adapter.
platform }) => {
const const x: DurableObjectId | undefinedx = platform: Readonly<App.Platform> | undefinedAdditional data made available through the adapter.
platform?.env: {
YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
}
env.type YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace<undefined>YOUR_DURABLE_OBJECT_NAMESPACE.DurableObjectNamespace<undefined>.idFromName(name: string): DurableObjectIdidFromName('x');
};SvelteKit’s built-in
$envmodule 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.PlatformIf 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;
} | undefinedenv?: {
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 4. Once you have built your site, run wrangler dev.
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.
Edit this page on GitHub llms.txt