Cloudflare Pages
To deploy to Cloudflare Pages, use adapter-cloudflare.
This adapter will be installed by default when you use adapter-auto. If you plan on staying with Cloudflare Pages, you can switch from adapter-auto to using this adapter directly so that values specific to Cloudflare Workers are 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 Pagesadapter-cloudflare-workers– supports all SvelteKit features; builds for Cloudflare Workersadapter-static– only produces client-side static assets; compatible with Cloudflare Pages
Usage
Install with npm i -D @sveltejs/adapter-cloudflare, then add the adapter to your svelte.config.js:
import import adapteradapter from '@sveltejs/adapter-cloudflare';
export default {
kit: {
adapter: any;
}
kit: {
adapter: anyadapter: import adapteradapter({
// See below for an explanation of these options
routes: {
include: string[];
exclude: string[];
}
routes: {
include: string[]include: ['/*'],
exclude: string[]exclude: ['<all>']
},
platformProxy: {
configPath: undefined;
environment: undefined;
persist: undefined;
}
platformProxy: {
configPath: undefinedconfigPath: var undefinedundefined,
environment: undefinedenvironment: var undefinedundefined,
persist: undefinedpersist: var undefinedundefined
}
})
}
};Options
routes
Allows you to customise the _routes.json file generated by adapter-cloudflare.
includedefines routes that will invoke a function, and defaults to['/*']excludedefines routes that will not invoke a function — this is a faster and cheaper way to serve your app’s static assets. This array can include the following special values:<build>contains your app’s build artifacts (the files generated by Vite)<files>contains the contents of yourstaticdirectory<prerendered>contains a list of prerendered pages<all>(the default) contains all of the above
You can have up to 100 include and exclude rules combined. Generally you can omit the routes options, but if (for example) your <prerendered> paths exceed that limit, you may find it helpful to manually create an exclude list that includes '/articles/*' instead of the auto-generated ['/articles/foo', '/articles/bar', '/articles/baz', ...].
platformProxy
Preferences for the emulated platform.env local bindings. See the getPlatformProxy Wrangler API documentation for a full list of options.
Deployment
Please follow the Get Started Guide for Cloudflare Pages to begin.
When configuring your project settings, you must use the following settings:
- Framework preset – SvelteKit
- Build command –
npm run buildorvite build - Build output directory –
.svelte-kit/cloudflare
Runtime 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 context, caches, and cf, meaning that you can access it in hooks and endpoints:
export async function function POST({ request, platform }: {
request: any;
platform: any;
}): Promise<void>
POST({ request, platform }) {
const const x: anyx = platform: anyplatform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('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 3. Once you have built your site, run wrangler pages dev .svelte-kit/cloudflare.
Notes
Functions contained in the /functions directory at the project’s root will not be included in the deployment. Instead, functions should be implemented as server endpoints in your SvelteKit app, which is compiled to a single _worker.js file.
The _headers and _redirects files specific to Cloudflare Pages can be used for static asset responses (like images) by putting them into the /static 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
Further reading
You may wish to refer to Cloudflare’s documentation for deploying a SvelteKit site.
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.