Cloudflare Vite Example
This example shows how to deploy a Vite application to Cloudflare Workers with KV Namespace bindings and environment variables.Features
- Vite: Modern frontend build tool with HMR
- KV Namespace: Key-value storage for the application
- Environment Bindings: Secrets and configuration
- Local Development: Vite dev server integration
Project Setup
import alchemy from "alchemy";
import { KVNamespace, Vite, Worker } from "alchemy/cloudflare";
const app = await alchemy("cloudflare-vite");
export const kv = await KVNamespace("kv", {
title: `${app.name}-${app.stage}-kv`,
adopt: true,
});
export const website = await Vite("website", {
entrypoint: "src/index.ts",
noBundle: false,
adopt: true,
bindings: {
KV: kv,
ALCHEMY_TEST_VALUE: alchemy.secret("Hello from Alchemy!"),
VITE_PUBLIC_DOMAIN: Worker.DevDomain,
},
dev: {
command: "vite dev --port 5006",
domain: "localhost:5006",
},
});
console.log({
url: website.url,
});
if (process.env.ALCHEMY_E2E) {
await new Promise((resolve) => setTimeout(resolve, 1000));
const { test } = await import("./test/e2e.js");
await test({
url: website.url,
env: { ALCHEMY_TEST_VALUE: "Hello from Alchemy!" },
});
}
await app.finalize();
import type { website } from "../alchemy.run.ts";
export default {
async fetch(request: Request, env: typeof website.Env) {
// Access KV namespace
await env.KV.put("visit-count", "1");
const count = await env.KV.get("visit-count");
// Access secrets
const message = env.ALCHEMY_TEST_VALUE;
return new Response(
JSON.stringify({
message,
visitCount: count,
domain: env.VITE_PUBLIC_DOMAIN,
}),
{
headers: { "Content-Type": "application/json" },
}
);
},
};
Key Features Explained
KV Namespace Adoption
Theadopt: true flag allows reusing existing KV namespaces:
Environment Variables
Secrets and configuration are passed via bindings:Local Development
Thedev configuration integrates with Vite’s dev server: