Deploying to Cloudflare Pages
A step-by-step guide to deploying your Astro site to Cloudflare Pages with edge optimization and global CDN distribution.
Deploying to Cloudflare Pages
Cloudflare Pages offers incredible performance for static sites with their global edge network. This guide walks you through deployment.
Why Cloudflare Pages?
- Global CDN with 300+ locations
- Unlimited bandwidth on free tier
- Instant cache invalidation
- Built-in PR previews
- Custom domains with free SSL
Prerequisites
- A GitHub or GitLab repository
- A Cloudflare account (free tier works)
- Your Astro project ready to deploy
Step 1: Connect Your Repository
- Log in to Cloudflare Dashboard
- Navigate to Pages → Create a project
- Select Connect to Git
- Authorize Cloudflare to access your repository
- Select your Astro project repository
Step 2: Configure Build Settings
| Setting | Value |
|---|---|
| Framework preset | Astro |
| Build command | npm run build |
| Build output directory | dist |
| Node version | 20 (or higher) |
Step 3: Environment Variables
Add your environment variables in the Settings → Environment variables section:
KEYSTATIC_STORAGE_KIND=cloud
KEYSTATIC_PROJECT=your-project-name
KEYSTATIC_CLOUD_ACCESS_TOKEN=your-token
PUBLIC_SITE_URL=https://your-site.pages.dev
Step 4: Deploy
Click Save and Deploy. Cloudflare will:
- Pull your repository
- Install dependencies
- Run the build
- Deploy to their edge network
First deployment typically takes 1-2 minutes.
Custom Domain Setup
- Go to your project → Custom domains
- Click Set up a custom domain
- Enter your domain (e.g.,
www.yoursite.com) - Cloudflare will configure DNS automatically
Performance Tips
Enable Early Hints
Cloudflare supports 103 Early Hints for faster resource loading:
# wrangler.toml
[site]
bucket = "./dist"
Optimize Images
Use Cloudflare Images or ensure your images are optimized:
- WebP format with fallbacks
- Responsive srcset
- Lazy loading for below-fold images
Cache Headers
Static assets get aggressive caching automatically. For dynamic content:
Cache-Control: public, max-age=3600, stale-while-revalidate=86400
Continuous Deployment
Every push to your main branch triggers automatic deployment:
- Push changes to GitHub
- Cloudflare detects the push
- New build starts automatically
- Site updates when build completes
Preview Deployments
Pull requests get unique preview URLs:
https://abc123.your-site.pages.dev
Perfect for testing before merging.
Monitoring
Cloudflare provides:
- Analytics: Page views, bandwidth, visitors
- Web Analytics: Core Web Vitals, device breakdown
- Logs: Real-time request logs (paid plans)
Troubleshooting
Build Fails
Check the build logs. Common issues:
- Missing environment variables
- Node version mismatch
- Dependency resolution errors
404 on Routes
Ensure astro.config.mjs has correct site URL and output mode:
export default defineConfig({
site: 'https://your-site.pages.dev',
output: 'static',
});
Next Steps
Your site is now live on Cloudflare’s global network! Consider:
- Setting up a custom domain
- Configuring Cloudflare Analytics
- Adding Cloudflare Workers for dynamic features
Happy deploying!