Adapter for building SvelteKit applications on Cloudflare Pages with Workers integration.
If you're using adapter-auto, you don't need to install this — it's already included.
Comparisons
adapter-cloudflare– supports all SvelteKit features; builds for Cloudflare Pagesadapter-cloudflare-workers– supports all SvelteKit features; builds for Cloudflare Workersadapter-static– only produces client-side static assets; compatible with Cloudflare Pages
Note: Cloudflare Pages' new Workers integration is currently in beta.
Compared toadapter-cloudflare-workers, this adapter will be the preferred approach for most users since building on top of Pages unlocks automatic builds and deploys, preview deployments, instant rollbacks, etc.
From SvelteKit's perspective, there is no difference and no functionality loss when migrating to/from the Workers and the Pages adapters.
$ npm i --save-dev @sveltejs/adapter-cloudflare@nextYou can include these changes in your svelte.config.js configuration file:
import adapter from '@sveltejs/adapter-cloudflare';
export default {
kit: {
adapter: adapter()
}
};Please follow the Get Started Guide for Cloudflare Pages to begin.
When configuring your project settings, you must use the following settings:
- Framework preset – None
- Build command –
npm run buildorsvelte-kit build - Build output directory –
.svelte-kit/cloudflare - Environment variables
NODE_VERSION:16or14
Important: You need to add a
NODE_VERSIONenvironment variable to both the "production" and "preview" environments. You can add this during project setup or later in the Pages project settings. SvelteKit requires Node14.13or later, so you should use14or16as theNODE_VERSIONvalue.
The env object, containing KV namespaces etc, is passed to SvelteKit via the platform property, meaning you can access it in hooks and endpoints:
// src/app.d.ts
declare namespace App {
interface Locals {}
+ interface Platform {
+ env: {
+ COUNTER: DurableObjectNamespace;
+ };
+ }
interface Session {}
interface Stuff {}
}export async function post({ request, platform }) {
const counter = platform.env.COUNTER.idFromName('A');
}
platform.envis only available in the production build. Use wrangler to test it locally