SvelteKit is a powerful framework for building web applications, and Cloudflare Pages provides an excellent platform for hosting them. This guide will walk you through the process of deploying your SvelteKit application to Cloudflare Pages, ensuring optimal performance and reliability.
Prerequisites#
Before we begin, make sure you have:
- Node.js installed on your machine
- A GitHub account
- A Cloudflare account
- Basic familiarity with SvelteKit
Setting Up Your SvelteKit Project#
1. Create a New SvelteKit Project#
If you haven’t already created your SvelteKit project, you can do so using the following commands:
npx sv create my-sveltekit-app
cd my-sveltekit-app
npm install2. Configure SvelteKit for Cloudflare#
To deploy to Cloudflare Pages, you’ll need to make some adjustments to your SvelteKit configuration:
- Install the Cloudflare adapter:
npm install -D @sveltejs/adapter-cloudflare- Update your
svelte.config.js:
import adapter from '@sveltejs/adapter-cloudflare';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter()
}
};
export default config;Preparing for Deployment#
1. Version Control Setup#
Make sure your project is in a Git repository and pushed to GitHub:
git init
git add .
git commit -m "Initial commit"
git remote add origin your-github-repo-url
git push -u origin main2. Build Configuration#
Create a wrangler.toml file in your project root (optional but recommended):
name = "your-project-name"
compatibility_date = "2023-01-01"Deploying to Cloudflare Pages#
1. Connect to Cloudflare Pages#
- Log in to your Cloudflare dashboard
- Navigate to Pages
- Click “Create a project”
- Choose “Connect to Git”
- Select your GitHub repository
2. Configure Build Settings#
Set the following build configurations in Cloudflare Pages:
- Build command:
npm run build - Build output directory:
.svelte-kit/cloudflare - Node.js version: 18 (or your preferred version)
Environment variables (if needed):
NODE_VERSION=183. Deploy#
Click “Save and Deploy” to start the deployment process. Cloudflare Pages will:
- Clone your repository
- Install dependencies
- Build your application
- Deploy to their global network
What you get out of the box#
On the first successful deploy, Cloudflare Pages gives you:
- A
*.pages.devpreview URL on every commit to the connected branch. - Automatic preview deploys on every pull request.
- Global CDN distribution — no region to pick.
- SSR running on Cloudflare Workers, which is what
adapter-cloudflaretargets.
If you need D1 database access, KV, R2, or cron triggers from the same app, those are bindings you attach to the Pages project in Settings → Functions → Bindings. The adapter surfaces them as platform.env.<BINDING> inside your SvelteKit load and server routes — see Using Cloudflare KV with SvelteKit and Setting up D1 with Drizzle in Hono for the two I reach for most.
References#
Related posts
- Using Cloudflare KV with SvelteKitHow to wire a Cloudflare KV namespace into a SvelteKit app deployed on Cloudflare — creating the namespace, binding it in wrangler, exposing it through hooks, and the basic read/write patterns in server routes.sveltekitsveltecloudflare workerscloudflare kv
- Cloudflare Cron Jobs in SvelteKitHow to add cron job support to a SvelteKit app deployed on Cloudflare Workers, since the adapter doesn't natively support the scheduled event handler.sveltekitcloudflare workerscron
- Using Nx with SvelteKit — caching without the monorepoNx is usually pitched as a monorepo tool for Angular or React, but since v18's "project crystal" it works cleanly on a single SvelteKit project too — and the build cache alone is worth the install.sveltekitsveltenx