Deploying Your Astro Blog to Cloudflare Workers

16924

Deploying Your Astro Blog to Cloudflare Workers

Cloudflare Workers provides a serverless edge runtime that's perfect for Astro SSR sites. Combined with D1 for the database and R2 for storage, you get a fully managed stack.

Prerequisites

  • A Cloudflare account
  • Wrangler CLI installed
  • An Astro project with @astrojs/cloudflare adapter

Step 1: Install the Adapter

npm install @astrojs/cloudflare

Update astro.config.mjs:

import { defineConfig } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';

export default defineConfig({
  output: 'server',
  adapter: cloudflare(),
});

Step 2: Create D1 Database

wrangler d1 create my-database

Add the binding to wrangler.toml:

[[d1_databases]]
binding = "DB"
database_name = "my-database"
database_id = "<your-database-id>"

Step 3: Deploy

npm run build
wrangler deploy

Your site is now live at https://your-worker.workers.dev!

Comments

Loading comments...

Leave a comment

Your name and email are saved locally in your browser so you don't have to re-enter them.