Astro
This guide shows how to initialize and deploy an Astro server-side–rendered site to Cloudflare using Alchemy.
Start by creating a new Astro project with Alchemy:
bunx alchemy create my-astro-app --template=astrocd my-astro-app
npx alchemy create my-astro-app --template=astrocd my-astro-app
pnpm dlx alchemy create my-astro-app --template=astrocd my-astro-app
yarn dlx alchemy create my-astro-app --template=astrocd my-astro-app
Authenticate once with your Cloudflare account:
bun wrangler login
npx wrangler login
pnpm wrangler login
yarn wrangler login
Deploy
Section titled “Deploy”Run the deploy script generated by the template:
bun run deploy
npm run deploy
pnpm run deploy
yarn run deploy
You’ll get the live URL of your Astro site:
{ url: "https://website.<your-account>.workers.dev",}
Local Development
Section titled “Local Development”Work locally using the dev script:
bun run dev
npm run dev
pnpm run dev
yarn run dev
Destroy
Section titled “Destroy”Clean up all Cloudflare resources created by this stack:
bun run destroy
npm run destroy
pnpm run destroy
yarn run destroy
What 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-me
alchemy.run.ts
Section titled “alchemy.run.ts”/// <reference types="@types/node" />
import alchemy from "alchemy";import { Astro } from "alchemy/cloudflare";
const app = await alchemy("my-astro-app");
export const worker = await Astro("website", { command: "bun run build",});
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": "astro/tsconfigs/strict", "include": [".astro/types.d.ts", "**/*", "types/**/*.ts", "alchemy.run.ts"], "exclude": ["dist"], "compilerOptions": { "types": ["@cloudflare/workers-types", "./types/env.d.ts"] }}
astro.config.mjs
Section titled “astro.config.mjs”Use the @astrojs/cloudflare
plugin to build for Cloudflare Workers:
import { defineConfig } from "astro/config";import cloudflare from "@astrojs/cloudflare";
// https://astro.build/configexport default defineConfig({ output: "server", adapter: cloudflare(),});