Cloudflare
To deploy to Cloudflare Workers, use adapter-cloudflare.
This adapter will be installed by default when you use adapter-auto. If you plan on staying with Cloudflare, you can switch from adapter-auto to using this adapter directly so that cloudflare:workers is emulated during local development, type declarations are automatically applied, and the ability to set Cloudflare-specific options is provided.
Comparisons
adapter-cloudflare– supports all SvelteKit features; builds for Cloudflare Workers Static Assetsadapter-cloudflare-workers– deprecated. Supports all SvelteKit features; builds for Cloudflare Workers Sitesadapter-static– only produces client-side static assets; compatible with Cloudflare Workers Static Assets and Cloudflare Pages
Usage
Run npx sv add sveltekit-adapter="adapter:cloudflare", or install with npm i -D @sveltejs/adapter-cloudflare and add the adapter to your vite.config.js:
import adapter from '@sveltejs/adapter-cloudflare';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
sveltekit({
adapter: adapter({
// See below for an explanation of these options
config: undefined,
platformProxy: {
configPath: undefined,
environment: undefined,
persist: undefined
},
fallback: 'plaintext'
})
})
]
});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 env local bindings. See the getPlatformProxy Wrangler API documentation for a full list of options.
fallback
Whether to render a plaintext 404.html page or a rendered SPA fallback page for non-matching asset requests.
For Cloudflare Workers, the default behaviour is to return a null-body 404-status response for non-matching assets requests. However, if the assets.not_found_handling Wrangler configuration setting is set to "404-page", this page will be served if a request fails to match an asset. If assets.not_found_handling is set to "single-page-application", the adapter will render a SPA fallback index.html page regardless of the fallback option specified.
Basic configuration
The adapter expects to find a Wrangler configuration file in the project root. It should look something like this:
{
"name": "<any-name-you-want>",
"main": ".svelte-kit/cloudflare/_worker.js",
"compatibility_flags": ["nodejs_als"],
"compatibility_date": "<YYYY-MM-DD>",
"assets": {
"binding": "ASSETS",
"directory": ".svelte-kit/cloudflare",
}
}Deployment
You can use the Wrangler CLI to deploy your application by running npx wrangler deploy or use the Cloudflare Git integration to enable automatic builds and deployments on push.
Runtime APIs
The env object contains your project’s bindings, which consist of KV/DO namespaces, etc. It is available on the cloudflare:workers module.
import { env } from 'cloudflare:workers';
/** @type {import('./$types').RequestHandler} */
export async function POST() {
const x = env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
}import { env } from 'cloudflare:workers';
import type { RequestHandler } from './$types';
export const POST: RequestHandler = async () => {
const x = env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
};SvelteKit’s built-in
$app/env/*modules should be preferred for environment variables.
To make these types available to your app, install wrangler and run wrangler types.
Testing locally
Cloudflare specific values are emulated during dev and preview modes. Local bindings are created based on your Wrangler configuration file and are used to populate env during development and preview. Use the adapter config platformProxy option to change your preferences for the bindings.
Durable Objects and Workflows, which require custom classes to be exported from your worker, are not currently supported.
For testing the build, you should use Wrangler version 4. Once you have built your site, run wrangler dev.
Headers and redirects
The _headers and _redirects files, specific to Cloudflare, can be used for static asset responses (like images) by putting them into the project root folder.
However, they will have no effect on responses dynamically rendered by SvelteKit, which should return custom headers or redirect responses from server endpoints or with the handle hook.
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.
Instead, use the read function from $app/server to access your files. It works by fetching the file from the deployed public assets location.
Alternatively, you can prerender the routes in question.
Migrating from Workers Sites
Cloudflare no longer recommends using Workers Sites and instead recommends using Workers Static Assets. To migrate, replace @sveltejs/adapter-cloudflare-workers with @sveltejs/adapter-cloudflare and remove all site configuration settings from your Wrangler configuration file, then add the assets.directory and assets.binding configuration settings:
import adapter from '@sveltejs/adapter-cloudflare';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
sveltekit({
adapter: adapter()
})
]
});wrangler.toml
site.bucket = ".cloudflare/public"
assets.directory = ".cloudflare/public"
assets.binding = "ASSETS" # Exclude this if you don't have a `main` key configured.wrangler.jsonc
{
"site": {
"bucket": ".cloudflare/public"
},
"assets": {
"directory": ".cloudflare/public",
"binding": "ASSETS" // Exclude this if you don't have a `main` key configured.
}
}Migrating from Cloudflare Pages
To migrate to Cloudflare Workers:
- Remove
pages_build_output_dirfrom your Wrangler configuration and add themainandassetssettings. - Remove the adapter’s
routesoption and any_routes.jsonfiles. - Replace
wrangler pages devandwrangler pages deploywithwrangler devandwrangler deploy. For automatic deployments, use Workers Builds instead of the Pages Git integration.
See Cloudflare’s Workers and Pages compatibility matrix for a comparison of supported features.
Edit this page on GitHub llms.txt