Redwood
Quick guide to initializing and deploying a RedwoodJS application with D1 database to Cloudflare Workers using Alchemy.
This guide shows how to initialize and deploy a RedwoodJS application to Cloudflare using Alchemy.
Start by creating a new Redwood project with Alchemy:
bunx alchemy create my-redwood-app --template=rwsdkcd my-redwood-appnpx alchemy create my-redwood-app --template=rwsdkcd my-redwood-apppnpm dlx alchemy create my-redwood-app --template=rwsdkcd my-redwood-appyarn dlx alchemy create my-redwood-app --template=rwsdkcd my-redwood-appAuthenticate 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 Redwood application:
{ 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 { D1Database, DurableObjectNamespace, Redwood,} from "alchemy/cloudflare";
const app = await alchemy("my-redwood-app");
const database = await D1Database("database", { name: "my-redwood-app-db", migrationsDir: "drizzle",});
export const worker = await Redwood("website", { name: "my-redwood-app-website", bindings: { AUTH_SECRET_KEY: alchemy.secret(process.env.AUTH_SECRET_KEY), DB: database, SESSION_DURABLE_OBJECT: DurableObjectNamespace("session", { className: "SessionDurableObject", }), },});
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
{ "compilerOptions": { "target": "ES2022", "lib": ["ES2022"], "module": "ES2022", "moduleResolution": "bundler", "strict": true, "skipLibCheck": true, "types": ["@cloudflare/workers-types", "./types/env.d.ts"] }, "include": ["types/**/*.ts", "alchemy.run.ts", "src/**/*.ts"]}vite.config.mts
Section titled “vite.config.mts”Use the alchemy() plugin to configure Redwood with Alchemy:
import alchemy from "alchemy/cloudflare/redwood";import { defineConfig } from "vite";
export default defineConfig({ plugins: [alchemy()],});