Deploying Astro to Cloudflare Pages
I recently moved my portfolio and blog over to Astro, and needed somewhere fast, free, and low-maintenance to host it. Cloudflare Pages ticked every box — especially now that Astro has been acquired by Cloudflare and has first-class support baked in.
Here’s how I set it all up.
Why Cloudflare Pages
I looked at Netlify, Vercel, and self-hosting on my own server. Cloudflare Pages won for a few reasons:
- Unlimited bandwidth on the free tier
- 500 builds per month — plenty for a blog
- Edge network means the site loads fast everywhere
- Astro 6 has native Cloudflare Workers support, so if I ever need server-side rendering it’s already there
- Auto-deploy on every
git push
No credit card required.
Prerequisites
Before deploying, make sure you have:
- Your Astro project pushed to a GitHub (or GitLab) repository
- A free Cloudflare account at dash.cloudflare.com
If you haven’t pushed to GitHub yet:
git init
git add .
git commit -m "initial commit"
git remote add origin https://github.com/yourusername/your-repo.git
git branch -M main
git push -u origin main
Connecting to Cloudflare Pages
The setup takes about two minutes:
- Log into your Cloudflare dashboard
- Go to Workers & Pages in the sidebar
- Click Create then Pages
- Choose Connect to Git and authorise your GitHub account
- Select your Astro repository
When it asks for build settings, use:
| Setting | Value |
|---|---|
| Framework preset | Astro |
| Build command | npm run build |
| Build output directory | dist |
Hit Save and Deploy. Cloudflare will clone your repo, install dependencies, run the build, and deploy it to a .pages.dev subdomain.
The first build usually takes a minute or two. After that, subsequent builds are faster thanks to dependency caching.
Setting Up a Custom Domain
Cloudflare gives you a URL like your-project.pages.dev for free. But if you want your own domain:
- Go to your Pages project in the dashboard
- Click Custom domains
- Add your domain (e.g.
yourdomain.com)
If your domain already uses Cloudflare DNS, it connects within seconds. If not, Cloudflare will provide nameservers for you to point to — this can take a few hours to propagate.
HTTPS is automatic. No messing with SSL certificates.
The Deployment Workflow
This is the part I love. Once connected, every push to main triggers an automatic build and deploy. So publishing a new blog post looks like this:
# Write a new post in src/content/blog/
git add .
git commit -m "new post: deploying astro to cloudflare"
git push
Within 30 seconds, the post is live. No FTP, no SSH, no CI/CD pipeline to configure. Just push and it’s done.
Cloudflare also supports preview deployments for branches other than main. So if you’re working on a new feature or redesign, push it to a separate branch and Cloudflare will give you a unique preview URL to test it before merging.
Environment Variables
If your Astro project uses environment variables (API keys, site URLs, etc.), add them in your Pages project under Settings > Environment variables.
For example, I set SITE_URL to my production domain so the sitemap and RSS feed generate with the correct URLs.
Build Caching
Cloudflare caches your node_modules between builds automatically. This means after the first deploy, builds are noticeably faster since it doesn’t need to reinstall every dependency from scratch.
If you ever run into a stale cache issue, you can clear it from Settings > Builds > Build cache in the dashboard.
What About SSR?
For a static blog like mine, Astro’s default static output mode is perfect. Everything is pre-rendered HTML at build time.
But if you ever need dynamic server-side rendering — maybe for an API route, authentication, or personalised content — Astro 6 integrates natively with Cloudflare Workers. You’d add the Cloudflare adapter:
npx astro add cloudflare
Then any server-rendered routes run on Cloudflare’s edge network. The free tier includes 100,000 Worker requests per day, which is more than enough for most projects.
Final Thoughts
Coming from a PHP and Laravel background where deployment usually means configuring servers, managing Nginx, and handling SSL, this felt refreshingly simple. Push to Git, site is live. The entire hosting setup took less time than writing this blog post.
If you’re running an Astro site and haven’t tried Cloudflare Pages yet, it’s worth the five minutes to set up.