Astro + Cloudflare
This guide will walk you through deploying an Astro project to Cloudflare Workers from scratch. Perfect for beginners!
Prerequisites
Section titled “Prerequisites”Before starting, make sure you have:
- Node.js (v18 or higher) or Bun installed
- A Cloudflare account (free tier works fine)
- Basic terminal/command line knowledge
Project Folder Structure
Section titled “Project Folder Structure”Understanding where files go is crucial! Here’s the complete folder structure of an Astro + Cloudflare project:
Directorymy-astro-site/ Your project folder (root)
Directorysrc/ Source code (you work here)
Directorypages/ Your website pages
- index.astro Homepage (/)
Directorycomponents/ Reusable components
- …
Directorylayouts/ Page layouts
- …
Directorystyles/ CSS files
- …
Directorypublic/ Static files (images, fonts, etc.)
- favicon.svg Site icon
Directorydist/ Built files (auto-generated)
- _worker.js Cloudflare Worker script
- .assetsignore Ignore file for Wrangler
Directorynode_modules/ Dependencies (auto-installed)
- …
- astro.config.mjs Root Astro configuration
- wrangler.jsonc Root Cloudflare config
- package.json Root Project info & scripts
- tsconfig.json TypeScript config
- .gitignore Git ignore file
Important Locations
Section titled “Important Locations”| File/Folder | Location | Purpose |
|---|---|---|
astro.config.mjs | Root | Astro + Cloudflare adapter config |
wrangler.jsonc | Root | Cloudflare deployment settings |
package.json | Root | Dependencies and scripts |
.assetsignore | dist/ | Tells Wrangler what to ignore |
.dev.vars | Root | Local environment variables |
| Your pages | src/pages/ | Website pages you create |
| Static assets | public/ | Images, fonts (copied as-is) |
Example: Where to Create Files
Section titled “Example: Where to Create Files”When the guide says “create in project root”, it means:
Directorymy-astro-site/
- astro.config.mjs Root
- wrangler.jsonc Root
- package.json Root
Part 1: Creating an Astro Project
Section titled “Part 1: Creating an Astro Project”Step 1: Install Astro
Section titled “Step 1: Install Astro”Open your terminal and run:
bun create astro@latestnpm create astro@latestStep 2: Follow the Setup Wizard
Section titled “Step 2: Follow the Setup Wizard”You’ll be asked several questions:
Where should we create your new project?→ my-astro-site
How would you like to start your new project?→ Choose a starter template (or "Empty" for blank project)
Install dependencies?→ Yes
Do you plan to write TypeScript?→ Yes (recommended)
Initialize a git repository?→ Yes (recommended)Step 3: Navigate to Your Project
Section titled “Step 3: Navigate to Your Project”cd my-astro-sitePart 2: Installing Cloudflare Adapter
Section titled “Part 2: Installing Cloudflare Adapter”Astro needs an adapter to work with Cloudflare Workers.
-
Install Cloudflare Adapter
Terminal window bun add @astrojs/cloudflareTerminal window npm install @astrojs/cloudflare -
Install Wrangler (Cloudflare CLI)
Wrangler is Cloudflare’s command-line tool for deployment.
Terminal window bun add wrangler --devTerminal window npm install wrangler --save-dev
Part 3: Configure Your Project
Section titled “Part 3: Configure Your Project”-
Update Astro Config
Open
astro.config.mjsand add the Cloudflare adapter:astro.config.mjs // astro.config.mjsimport { defineConfig } from 'astro/config';import cloudflare from '@astrojs/cloudflare';export default defineConfig({output: 'server', // Enable SSRadapter: cloudflare(),}); -
Create Wrangler Configuration
Create a file named
wrangler.jsoncin your project root (same level aspackage.json):File location:
my-astro-site/├── package.json├── wrangler.jsonc ← Create here└── astro.config.mjswrangler.jsonc {"$schema": "node_modules/wrangler/config-schema.json","name": "my-astro-site","compatibility_date": "2025-10-13","workers_dev": true,"assets": {"directory": "./dist","not_found_handling": "404-page"}}wrangler.toml name = "my-astro-site"main = "./dist/_worker.js"compatibility_date = "2025-10-13"workers_dev = true[site]bucket = "./dist"How to create:
Terminal window # Make sure you're in project rootcd my-astro-site# Create the filetouch wrangler.jsonc# Then open it in your editor and paste the JSON aboveCreate manually in your code editor: File → New → Save as
wrangler.jsoncin root. -
Understanding .assetsignore File
File location:
my-astro-site/ ← Project root├── package.json├── wrangler.jsonc└── dist/ ← Build output folder├── _worker.js└── .assetsignore ← Create hereThe Problem:
- The
dist/folder is auto-generated when you runbun run build - It gets deleted and recreated every time you build
- So
.assetsignoreneeds to be recreated after each build
Solution 1: Auto-create with deploy script
Add this to your
package.jsonscripts:package.json {"scripts": {"deploy": "npm run build && echo '_worker.js' > dist/.assetsignore && npx wrangler deploy"}}Now run:
npm run deploy(orbun run deploy)Solution 2: Manual creation after build
Terminal window # 1. Build firstbun run build# 2. Create .assetsignore in dist folderecho "_worker.js" > dist/.assetsignore# 3. Deploynpx wrangler deployFile content:
_worker.js - The
Part 4: Set Up Cloudflare Account
Section titled “Part 4: Set Up Cloudflare Account”-
Create Cloudflare Account
- Go to cloudflare.com
- Click Sign Up
- Enter your email and password
- Verify your email address
-
Get Your Account ID Optional
- Log in to Cloudflare Dashboard
- Click on Workers & Pages in the sidebar
- Your Account ID is displayed on the right side
- Copy it for later (you might need it)
Part 5: Authentication with Wrangler
Section titled “Part 5: Authentication with Wrangler”-
Login to Cloudflare via Wrangler
Run this command in your terminal:
Terminal window npx wrangler loginThis will:
- Open your browser automatically
- Ask you to authorize Wrangler
- Click Allow to grant permissions
- Return to terminal - you’ll see “Successfully logged in”
-
Verify Login
Check if you’re logged in:
Terminal window npx wrangler whoamiYou should see your account email and Account ID.
Part 6: Build and Deploy
Section titled “Part 6: Build and Deploy”-
Test Locally First
Before deploying, test your site locally:
Terminal window bun run devTerminal window npm run devVisit
http://localhost:4321to see your site. -
Build Your Project
Build your Astro site for production:
Terminal window bun run buildTerminal window npm run buildThis creates a
distfolder with your built site. -
Deploy to Cloudflare
Deploy your site:
Terminal window npx wrangler deployWhat happens:
- Uploads your built files to Cloudflare
- Creates a Worker
- Gives you a live URL like:
https://my-astro-site.YOUR-SUBDOMAIN.workers.dev
-
Visit Your Live Site
After deployment completes, you’ll see:
Deployed my-astro-site triggers (5.00 sec)https://my-astro-site.YOUR-SUBDOMAIN.workers.devCurrent Version ID: abc123...Click the URL or copy it to your browser!
Part 7: Update Your Site
Section titled “Part 7: Update Your Site”Step 17: Making Changes
Section titled “Step 17: Making Changes”Whenever you update your site:
-
Make your changes in code
-
Test locally
Terminal window bun run devTerminal window npm run dev -
Build for production
Terminal window bun run buildTerminal window npm run build -
Deploy
Terminal window npx wrangler deploy
Quick command (build + deploy in one line):
bun run build && npx wrangler deploynpm run build && npx wrangler deployConfiguration Files Reference
Section titled “Configuration Files Reference”File Locations Overview
Section titled “File Locations Overview”Here’s where each config file should be:
Directorymy-astro-site/ PROJECT ROOT
- astro.config.mjs 1. Astro config
- wrangler.jsonc 2. Cloudflare config
- package.json 3. Scripts
- .dev.vars 4. Local env vars (optional)
Directorysrc/
- …
Directorydist/ Generated on build
- .assetsignore 5. Wrangler ignore
Required Files
Section titled “Required Files”Your project needs these configuration files:
1. astro.config.mjs Location: Root
Section titled “1. astro.config.mjs ”import { defineConfig } from 'astro/config';import cloudflare from '@astrojs/cloudflare';
export default defineConfig({ output: 'server', adapter: cloudflare(),});2. wrangler.jsonc
Section titled “2. wrangler.jsonc”{ "$schema": "node_modules/wrangler/config-schema.json", "name": "my-astro-site", "compatibility_date": "2025-10-13", "workers_dev": true, "assets": { "directory": "./dist", "not_found_handling": "404-page" }}3. .assetsignore
Section titled “3. .assetsignore”_worker.js4. package.json Scripts
Section titled “4. package.json Scripts”{ "scripts": { "dev": "astro dev", "build": "astro build", "preview": "astro preview", "deploy": "bun run build && echo '_worker.js' > dist/.assetsignore && npx wrangler deploy" }}Command Reference
Section titled “Command Reference”Development Commands
Section titled “Development Commands”| Command | Description |
|---|---|
bun run dev | Start local development server |
bun run build | Build project for production |
bun run preview | Preview production build locally |
bun run deploy | Build and deploy in one command |
| Command | Description |
|---|---|
npm run dev | Start local development server |
npm run build | Build project for production |
npm run preview | Preview production build locally |
npm run deploy | Build and deploy in one command |
Wrangler Commands
Section titled “Wrangler Commands”| Command | Description |
|---|---|
npx wrangler login | Login to Cloudflare |
npx wrangler logout | Logout from Cloudflare |
npx wrangler whoami | Check current login status |
npx wrangler deploy | Deploy to Cloudflare Workers |
npx wrangler tail | View live logs from your worker |
npx wrangler delete | Delete your worker |
Deployment Commands
Section titled “Deployment Commands”Quick Deployment:
bun run deployWhat it does:
- Builds your Astro site
- Creates
.assetsignorefile (prevents_worker.jserror) - Deploys to Cloudflare Workers
Manual Steps (if needed):
# 1. Buildbun run build
# 2. Create .assetsignoreecho '_worker.js' > dist/.assetsignore
# 3. Deploynpx wrangler deployQuick Deployment:
npm run deployWhat it does:
- Builds your Astro site
- Creates
.assetsignorefile (prevents_worker.jserror) - Deploys to Cloudflare Workers
Manual Steps (if needed):
# 1. Buildnpm run build
# 2. Create .assetsignoreecho '_worker.js' > dist/.assetsignore
# 3. Deploynpx wrangler deployTroubleshooting
Section titled “Troubleshooting”Common Issues and Solutions
Section titled “Common Issues and Solutions”Issue 1: “API Token Required”
Section titled “Issue 1: “API Token Required””Error:
In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKENIssue 2: “_worker.js Upload Error”
Section titled “Issue 2: “_worker.js Upload Error””Error:
Uploading a Pages _worker.js directory as an assetIssue 3: “Port Already in Use”
Section titled “Issue 3: “Port Already in Use””Error:
Port 4321 is in useIssue 4: Build Fails
Section titled “Issue 4: Build Fails”Issue 5: Worker Not Working After Deploy
Section titled “Issue 5: Worker Not Working After Deploy”Environment Variables (Advanced)
Section titled “Environment Variables (Advanced)”Setting Environment Variables
Section titled “Setting Environment Variables”If your app needs environment variables:
-
Create
.dev.varsFileFor local development:
.dev.vars DATABASE_URL=your-database-urlAPI_KEY=your-api-key -
Add to Cloudflare
For production, add secrets via Wrangler:
Terminal window npx wrangler secret put DATABASE_URL# Enter your value when promptedOr add in
wrangler.jsonc:wrangler.jsonc {"vars": {"ENVIRONMENT": "production"}}
Custom Domain (Optional)
Section titled “Custom Domain (Optional)”Adding Your Own Domain
Section titled “Adding Your Own Domain”-
Add Domain to Cloudflare
- Go to Cloudflare Dashboard
- Click Add Site
- Enter your domain
- Follow DNS setup instructions
-
Add Route to Worker
In
wrangler.jsonc, add:wrangler.jsonc {"routes": [{"pattern": "yourdomain.com/*","zone_name": "yourdomain.com"}]} -
Deploy
Terminal window npx wrangler deployYour site will be available at your custom domain!
Best Practices
Section titled “Best Practices”Next Steps
Section titled “Next Steps”Now that your site is deployed:
- Add More Pages - Create new
.astrofiles insrc/pages/ - Customize Design - Edit your components and styles
- Add Features - Install Astro integrations
- Set Up CI/CD - Automate deployments with GitHub Actions
- Monitor Performance - Use Cloudflare Analytics
Useful Links
Section titled “Useful Links”Summary
Section titled “Summary”You’ve learned how to:
- Install and set up Astro
- Add Cloudflare adapter
- Configure Wrangler
- Authenticate with Cloudflare
- Build and deploy your site
- Update and redeploy
- Troubleshoot common issues
Happy deploying!