Build and deploy
Cloudflare Workers
Edit this page on GitHubTo deploy to Cloudflare Workers, use adapter-cloudflare-workers.
Unless you have a specific reason to use
adapter-cloudflare-workers, it's recommended that you useadapter-cloudflareinstead. 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.
Usagepermalink
Install with npm i -D @sveltejs/adapter-cloudflare-workers, then add the adapter to your svelte.config.js:
tsimportCannot find module '@sveltejs/adapter-cloudflare-workers' or its corresponding type declarations.2307Cannot find module '@sveltejs/adapter-cloudflare-workers' or its corresponding type declarations.adapter from'@sveltejs/adapter-cloudflare-workers' ;export default {kit : {adapter :adapter ()}};
Basic Configurationpermalink
This adapter expects to find a wrangler.toml 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"
workers_dev = true<your-service-name> can be anything. <your-account-id> can be found by logging into your Cloudflare dashboard and grabbing it from the end of the URL:
https://dash.cloudflare.com/<your-account-id>You should add the
.cloudflaredirectory (or whichever directories you specified formainandsite.bucket) to your.gitignore.
You will need to install wrangler and log in, if you haven't already:
npm i -g wrangler
wrangler loginThen, you can build your app and deploy it:
wrangler deployCustom configpermalink
If you would like to use a config file other than wrangler.toml, you can do like so:
tsimportCannot find module '@sveltejs/adapter-cloudflare-workers' or its corresponding type declarations.2307Cannot find module '@sveltejs/adapter-cloudflare-workers' or its corresponding type declarations.adapter from'@sveltejs/adapter-cloudflare-workers' ;export default {kit : {adapter :adapter ({config : '<your-wrangler-name>.toml' })}};
If you would like to enable Node.js compatibility, you can add "nodejs_compat" flag to wrangler.toml:
compatibility_flags = [ "nodejs_compat" ]Runtime APIspermalink
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:
tsexport async functionBinding element 'request' implicitly has an 'any' type.Binding element 'platform' implicitly has an 'any' type.7031POST ({, request }) { platform
7031Binding element 'request' implicitly has an 'any' type.Binding element 'platform' implicitly has an 'any' type.constx =platform .env .YOUR_DURABLE_OBJECT_NAMESPACE .idFromName ('x');}
SvelteKit's built-in
$envmodule should be preferred for environment variables.
To include type declarations for your bindings, reference them in your src/app.d.ts:
declare global {
namespace App {
interface Platform {
env?: {
YOUR_KV_NAMESPACE: KVNamespace;
YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
};
}
}
}
export {};
Testing Locallypermalink
Cloudflare Workers specific values in the platform property are emulated during dev and preview modes. The bindings in your wrangler.toml will be used to populate platform.env during local development.
For testing the build, you should use wrangler version 3. Once you have built your site, run wrangler dev.
Troubleshootingpermalink
Worker size limitspermalink
When deploying to workers, 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 systempermalink
You can't use fs in Cloudflare Workers — you must prerender the routes in question.