Cloudflare Pages
Edit this page on GitHubTo deploy to Cloudflare Pages, use adapter-cloudflare.
This adapter will be installed by default when you use adapter-auto, but adding it to your project is recommended so that event.platform is automatically typed.
Comparisons permalink
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
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.
Usage permalink
Install with npm i -D @sveltejs/adapter-cloudflare, then add the adapter to your svelte.config.js:
tsimportCannot find module '@sveltejs/adapter-cloudflare' or its corresponding type declarations.2307Cannot find module '@sveltejs/adapter-cloudflare' or its corresponding type declarations.adapter from'@sveltejs/adapter-cloudflare' ;export default {kit : {adapter :adapter ({// See below for an explanation of these optionsroutes : {include : ['/*'],exclude : ['<all>']}})}};
Options permalink
The routes option 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', ...].
Deployment permalink
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 buildorvite build - Build output directory –
.svelte-kit/cloudflare - Environment variables
NODE_VERSION:16
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 Node16.14or later, so you should use16as theNODE_VERSIONvalue.
Bindings permalink
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 and caches, 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 make these types available to your app, 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 {};
platform.envis only available in the production build. Use wrangler to test it locally.
Notes permalink
Functions contained in the /functions directory at the project's root will not be included in the deployment, which is compiled to a single _worker.js.ts file. Functions should be implemented as server endpoints in your SvelteKit app.
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 permalink
Further reading permalink
You may wish to refer to Cloudflare's documentation for deploying a SvelteKit site.
Accessing the file system permalink
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.