To deploy to Cloudflare Workers, use adapter-cloudflare-workers
.
Unless you have a specific reason to use this adapter, we recommend using adapter-cloudflare
instead.
Requires Wrangler v2.
Install with npm i -D @sveltejs/adapter-cloudflare-workers
, then add the adapter to your svelte.config.js
:
// @errors: 2307
/// file: svelte.config.js
import adapter from '@sveltejs/adapter-cloudflare-workers';
export default {
kit: {
adapter: adapter()
}
};
This adapter expects to find a wrangler.toml file in the project root. It should look something like this:
/// file: wrangler.toml
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
.cloudflare
directory (or whichever directories you specified formain
andsite.bucket
) to your.gitignore
.
You will need to install wrangler and log in, if you haven't already:
npm i -g wrangler
wrangler login
Then, you can build your app and deploy it:
wrangler publish
If you would like to use a config file other than wrangler.toml
, you can do like so:
// @errors: 2307
/// file: svelte.config.js
import adapter from '@sveltejs/adapter-cloudflare-workers';
export default {
kit: {
adapter: adapter({ config: '<your-wrangler-name>.toml' })
}
};
The env
object, containing KV/DO namespaces etc, is passed to SvelteKit via the platform
property along with context
and caches
, meaning you can access it in hooks and endpoints:
// @errors: 7031
export async function POST({ request, platform }) {
const x = platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
}
To make these types available to your app, reference them in your src/app.d.ts
:
/// file: src/app.d.ts
declare global {
namespace App {
interface Platform {
+ env?: {
+ YOUR_KV_NAMESPACE: KVNamespace;
+ YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
+ };
}
}
}
export {};
platform.env
is only available in the production build. Use wrangler to test it locally
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.
You can't access the file system through methods like fs.readFileSync
in Serverless/Edge environments. If you need to access files that way, do that during building the app through prerendering. If you have a blog for example and don't want to manage your content through a CMS, then you need to prerender the content (or prerender the endpoint from which you get it) and redeploy your blog everytime you add new content.