| pcx_content_type | how-to | |
|---|---|---|
| title | SvelteKit | |
| description | Learn how to create and deploy a SvelteKit application to Cloudflare Pages using the create-cloudflare CLI | |
| products |
|
import { PagesBuildPreset, Render, PackageManagers } from "~/components";
SvelteKit is the official framework for building modern web applications with Svelte, an increasingly popular open-source tool for creating user interfaces. Unlike most frameworks, SvelteKit uses Svelte, a compiler that transforms your component code into efficient JavaScript, enabling SvelteKit to deliver fast, reactive applications that update the DOM surgically as the application state changes.
In this guide, you will create a new SvelteKit application and deploy it using Cloudflare Pages.
You will use SvelteKit, the official Svelte framework for building web applications of all sizes.
Use the create-cloudflare CLI (C3) to set up a new project. C3 will create a new project directory, initiate SvelteKit's official setup tool, and provide the option to deploy instantly.
To use create-cloudflare to create a new SvelteKit project, run the following command:
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 guide. Choose the options that suit your project.
create-cloudflare will then install dependencies, including the Wrangler CLI and the SvelteKit @sveltejs/adapter-cloudflare adapter, and ask you setup questions.
After you have installed your project dependencies, start your application:
npm run devTo 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-cloudflarein your terminal. - Include the adapter in
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
envobject, 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:
declare 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(context) {
const counter = context.platform.env.COUNTER.idFromName("A");
}:::note
In addition to the Cloudflare adapter, review other adapters you can use in your project:
-
SvelteKit's default adapter automatically chooses the adapter for your current environment. If you use this adapter, no configuration is needed. However, the default adapter introduces a few disadvantages for local development because it has no way of knowing what platform the application is going to be deployed to.
To solve this issue, provide a CF_PAGES variable to SvelteKit so that the adapter can detect the Pages platform. For example, when locally building the application: CF_PAGES=1 vite build.
@sveltejs/adapter-staticOnly produces client-side static assets (no server-side rendering) and is compatible with Cloudflare Pages. Review the official SvelteKit documentation for instructions on how to set up the adapter. Keep in mind that if you decide to use this adapter, the build directory, instead of.svelte-kit/cloudflare, becomesbuild. You must also configure your Cloudflare Pages application's build directory accordingly.
:::
:::caution
If you are using any adapter different from the default SvelteKit adapter, remember to commit and push your adapter setting changes to your GitHub repository before attempting the deployment.
:::
<Render file="deploy-via-c3" product="pages" params={{ name: "Svelte" }} />
<Render file="deploy-to-pages-steps-with-preset" product="pages" params={{ name: "SvelteKit" }} />
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 SvelteKit 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.
:::note
For the complete guide to deploying your first site to Cloudflare Pages, refer to the Get started guide.
:::
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:
import type { RequestHandler } from "./$types";
export const GET = (({ url }) => {
return new Response(String(Math.random()));
}) satisfies RequestHandler;:::note[SvelteKit API Routes]
For more information about SvelteKit API Routes, refer to the SvelteKit documentation. :::
<Render file="framework-guides/learn-more" product="pages" params={{ one: "Svelte" }} />