SvelteKit
Quick guide to initializing and deploying a SvelteKit application to Cloudflare Workers using Alchemy.
This guide shows how to initialize and deploy a SvelteKit application to Cloudflare using Alchemy.
Start by creating a new SvelteKit project with Alchemy:
bunx alchemy create my-sveltekit-app --template=sveltekitcd my-sveltekit-appnpx alchemy create my-sveltekit-app --template=sveltekitcd my-sveltekit-apppnpm dlx alchemy create my-sveltekit-app --template=sveltekitcd my-sveltekit-appyarn dlx alchemy create my-sveltekit-app --template=sveltekitcd my-sveltekit-appLog in to Cloudflare
Section titled “Log in to Cloudflare”Authenticate once with your Cloudflare account:
bun alchemy loginnpx alchemy loginpnpm alchemy loginyarn alchemy loginDeploy
Section titled “Deploy”Run the deploy script generated by the template:
bun run deploynpm run deploypnpm run deployyarn run deployYou’ll get the live URL of your SvelteKit site:
{ url: "https://website.<your-account>.workers.dev",}Local Development
Section titled “Local Development”Work locally using the dev script:
bun run devnpm run devpnpm run devyarn run devDestroy
Section titled “Destroy”Clean up all Cloudflare resources created by this stack:
bun run destroynpm run destroypnpm run destroyyarn run destroyWhat files are created
Section titled “What files are created”Alchemy requires a locally set password to encrypt Secrets that are stored in state. Be sure to change this.
ALCHEMY_PASSWORD=change-mealchemy.run.ts
Section titled “alchemy.run.ts”/// <reference types="@types/node" />
import alchemy from "alchemy";import { SvelteKit } from "alchemy/cloudflare";
const app = await alchemy("my-sveltekit-app");
export const worker = await SvelteKit("website");
console.log({ url: worker.url,});
await app.finalize();types/env.d.ts
Section titled “types/env.d.ts”// Auto-generated Cloudflare binding types.// @see https://alchemy.run/concepts/bindings/#type-safe-bindings
import type { worker } from "../alchemy.run.ts";
export type CloudflareEnv = typeof worker.Env;
declare global { type Env = CloudflareEnv;}
declare module "cloudflare:workers" { namespace Cloudflare { export interface Env extends CloudflareEnv {} }}tsconfig.json
Section titled “tsconfig.json”The CLI updated the tsconfig.json to include alchemy.run.ts and register @cloudflare/workers-types + types/env.d.ts globally
{ "extends": "./.svelte-kit/tsconfig.json", "include": ["alchemy.run.ts", "types/**/*.ts"], "compilerOptions": { // (omitted for brevity ..) "types": ["@cloudflare/workers-types", "./types/env.d.ts"] }}svelte.config.js
Section titled “svelte.config.js”Use the alchemy() adapter to configure SvelteKit for local development and deployment to Cloudflare Workers:
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';import alchemy from 'alchemy/cloudflare/sveltekit';
/** @type {import('@sveltejs/kit').Config} */const config = { // Consult https://svelte.dev/docs/kit/integrations // for more information about preprocessors preprocess: vitePreprocess(),
kit: { // Using Cloudflare adapter for deployment to Cloudflare Workers // See https://svelte.dev/docs/kit/adapters for more information about adapters. adapter: alchemy() }};
export default config;