Deploy a Svelte site
Svelte is an increasingly popular, open-source framework for building user interfaces and web applications. Unlike most frameworks, Svelte is primarily a compiler that converts your component code into efficient JavaScript that surgically updates the DOM when your application state changes.
In this guide, you will create a new Svelte application and deploy it using Cloudflare Pages.
You will use SvelteKit
, the official Svelte framework for building web applications of all sizes.
Setting up a new project
Create a new project by running the npm init
command in your terminal, giving it a title:
$ npm init svelte@latest my-svelte-app
During init
, SvelteKit will prompt you for customization choices. For the template option, choose one of the application/project options. The remaining answers will not affect the rest of this tutorial. Choose the options that better suit your project.
After creating your project, go to your project directory and install its dependencies:
$ cd my-svelte-app
$ npm install
After you have installed your project dependencies, start your application:
$ npm run dev
Before you continue
All of the framework guides assume you already have a fundamental understanding of Git. If you are new to Git, refer to this summarized Git handbook on how to set up Git on your local machine.
If you clone with SSH, you must generate SSH keys on each computer you use to push or pull from GitHub.
Refer to the GitHub documentation and Git documentation for more information.
Create a GitHub repository
Create a new GitHub repository by visiting repo.new. After creating a new repository, prepare and push your local application to GitHub by running the following commands in your terminal:
$ git init
$ git remote add origin https://github.com/<your-gh-username>/<repository-name>
$ git add .
$ git commit -m "Initial commit"
$ git branch -M main
$ git push -u origin main
SvelteKit Cloudflare configuration
To use SvelteKit with Cloudflare Pages, you need to add the Cloudflare adapter to your application:
- Install the Cloudflare Adapter by running
npm i --save-dev @sveltejs/adapter-cloudflare
in your terminal. - Include the adapter in
svelte.config.js
:
svelte.config.js- import adapter from '@sveltejs/adapter-auto;+ import adapter from '@sveltejs/adapter-cloudflare';
/** @type {import('@sveltejs/kit').Config} */const config = { kit: { adapter: adapter(), // ... truncated ... }};
export default config;
- (Needed if you are using Typescript) Include support for environment variables. The
env
object, containing KV namespaces and other storage objects, is passed to SvelteKit via the platform property along with context and caches, meaning you can access it in hooks and endpoints. For example:
src/app.d.tsdeclare namespace App { interface Locals {}
+ interface Platform {+ env: {+ COUNTER: DurableObjectNamespace;+ };+ context: {+ waitUntil(promise: Promise<any>): void;+ };+ caches: CacheStorage & { default: Cache }+ }
interface Session {}
interface Stuff {}}
- Access the added KV or Durable objects (or generally any binding) in your endpoint with
env
:
export async function post({ request, platform }) { const counter = platform.env.COUNTER.idFromName("A");
}
Deploy with Cloudflare Pages
Deploy your site to Pages by logging in to the Cloudflare dashboard > Account Home > Pages and selecting Create a project.
You will be asked to authorize access to your GitHub account if you have not already done so. Cloudflare needs this authorization to deploy your projects from your GitHub account. You may narrow Cloudflare’s access to specific repositories. However, you will have to manually update this list within your GitHub settings when you want to add more repositories to Cloudflare Pages.
Select the new GitHub repository that you created and, in Set up builds and deployments, provide the following information:
Configuration option | Value |
---|---|
Production branch | main |
Build command | npm run build |
Build directory | .svelte-kit/cloudflare |
Environment Variables | NODE_VERSION: 16 |
Optionally, you can customize the Project name field. It defaults to the GitHub repository’s name, but it does not need to match. The Project name value is assigned as your *.pages.dev
subdomain.
After completing configuration, click the Save and Deploy button.
You will see your first deploy pipeline in progress. Pages installs all dependencies and builds the project as specified.
Cloudflare Pages will automatically rebuild your project and deploy it on every new pushed commit.
Additionally, you will have access to preview deployments, which repeat the build-and-deploy process for pull requests. With these, you can preview changes to your project with a real URL before deploying them to production.
Functions setup
In SvelteKit, functions are written as endpoints. Functions contained in the /functions
directory at the project’s root will not be included in the deployment, which compiles to a single _worker.js
file.
To have the functionality equivalent to Pages Functions onRequests
, you need to write standard request handlers in SvelteKit. For example, the following TypeScript file behaves like an onRequestGet
:
src/routes/random/+server.tsimport type { RequestHandler } from './$types';
export const GET = (({ url }) => { return new Response(String(Math.random()));
}) satisfies RequestHandler;
Learn more
By completing this guide, you have successfully deployed your Svelte site to Cloudflare Pages. To get started with other frameworks, refer to the list of Framework guides.