Deploying to Cloudflare Pages¶
Deploy your site to Cloudflare's global edge network.
Why Cloudflare Pages?¶
- Global Edge Network: 300+ locations worldwide
- Zero Configuration: Auto-detects Astro
- Unlimited Bandwidth: No bandwidth limits on free tier
- Preview Deployments: Every branch gets a URL
- Built-in Analytics: Free Web Analytics
- Edge Functions: Serverless functions at the edge
Prerequisites¶
- Cloudflare account (free tier works)
- GitHub repository
- Repository pushed to GitHub
Setup via Dashboard¶
1. Connect Repository¶
- Log in to Cloudflare Dashboard
- Go to Workers & Pages → Create application
- Select Pages → Connect to Git
- Authorize Cloudflare to access your GitHub account
- Select your repository:
chrislyons-dev/home
2. Configure Build¶
Cloudflare auto-detects Astro, but verify these settings:
Build Configuration:
- Framework preset: Astro
- Build command:
npm run build - Build output directory:
dist - Root directory:
/(default) - Node version: 20
Environment Variables:
3. Deploy¶
- Click Save and Deploy
- Wait 2-3 minutes for initial build
- Your site will be live at
https://chrislyons-dev.pages.dev
Branch Deployments¶
Production Branch¶
Set your production branch in project settings:
- Settings → Builds & deployments
- Production branch:
main - Every push to
maindeploys to production
Preview Branches¶
All other branches automatically get preview URLs:
Preview URL: https://[commit-hash].chrislyons-dev.pages.dev
Branch-Specific Deployments¶
Configure which branches trigger deployments:
- Settings → Builds & deployments
- Branch deployments
- Add branches:
main→ Productionstaging→ Staging environmentdevelop→ Development preview
Setup via GitHub Actions¶
For more control, use the GitHub Actions workflow.
1. Get Cloudflare Credentials¶
API Token:
- Cloudflare Dashboard → My Profile → API Tokens
- Click Create Token
- Use template: Edit Cloudflare Workers
- Copy the token
Account ID:
- Cloudflare Dashboard → Workers & Pages
- Copy Account ID from the right sidebar
2. Add GitHub Secrets¶
- GitHub Repository → Settings → Secrets and variables → Actions
- Click New repository secret
- Add:
- Name:
CLOUDFLARE_API_TOKEN - Value: [your API token]
- Add:
- Name:
CLOUDFLARE_ACCOUNT_ID - Value: [your account ID]
3. Workflow Configuration¶
The workflow in .github/workflows/cloudflare.yml handles deployment:
on:
push:
branches:
- main # Production
- staging # Staging environment
- develop # Development preview
Customize branches by editing the workflow file.
4. Deploy¶
GitHub Actions will build and deploy automatically.
Custom Domain¶
Add Custom Domain¶
- Custom domains → Set up a custom domain
- Enter your domain:
chrislyons.dev - Choose setup method:
Option A: Cloudflare-managed DNS
- Domain already on Cloudflare
- Click Activate domain
- DNS records added automatically
Option B: External DNS
- Add CNAME record:
- For apex domain, use CNAME flattening or ALIAS record
SSL/TLS¶
SSL certificates are automatically provisioned:
- Universal SSL (default)
- Auto-renewal every 90 days
- Full (strict) encryption mode recommended
Environment Variables¶
Add Variables¶
- Settings → Environment variables
- Click Add variable
- Set per environment:
- Production:
mainbranch - Preview: All other branches
Example:
Access in Code¶
Build Configuration¶
Custom Build Command¶
Override default build in Settings → Builds & deployments:
# Install dependencies and build
npm ci && npm run build
# Run tests before build
npm ci && npm test && npm run build
Build Watch Paths¶
Trigger builds only on specific file changes:
- Settings → Builds & deployments
- Build watch paths
- Add patterns:
Performance¶
Edge Caching¶
Cloudflare automatically caches:
- Static assets: 1 year
- HTML: Configurable
- Images: Auto-optimized
Headers Configuration¶
Set in wrangler.toml:
Compression¶
Auto-enabled:
- Brotli compression
- Gzip fallback
- Smart compression
Analytics¶
Enable Web Analytics¶
- Analytics → Web Analytics
- Enable for your domain
- View metrics:
- Page views
- Unique visitors
- Geographic distribution
- Performance metrics
Custom Events¶
Add to your site:
<script
defer
src="https://static.cloudflareinsights.com/beacon.min.js"
data-cf-beacon='{"token": "your-token"}'
></script>
Functions (Optional)¶
Add Edge Functions¶
Create functions/ directory:
// functions/api/hello.ts
export const onRequest: PagesFunction = async (context) => {
return new Response('Hello from the edge!');
};
Available at: /api/hello
Rollback¶
Revert to Previous Deployment¶
- Deployments tab
- Find last good deployment
- Click ⋯ → Rollback to this deployment
- Confirm rollback
Via Git¶
Monitoring¶
Deployment Status¶
View in real-time:
- Deployments tab
- Build logs
- Deployment timeline
Error Logs¶
- Functions → Logs (if using functions)
- Real-time error tracking
- Filter by severity
Troubleshooting¶
Build Failures¶
Check build logs:
- Go to failed deployment
- View Build log
- Look for errors
Common issues:
Node version mismatch:
Missing dependencies:
# Ensure package-lock.json is committed
git add package-lock.json
git commit -m "fix: add lock file"
Deployment Not Updating¶
- Clear build cache:
- Settings → Builds & deployments
-
Clear cache and retry
-
Force rebuild:
Custom Domain Issues¶
CNAME not resolving:
- Wait for DNS propagation (up to 48 hours)
- Verify DNS records:
dig chrislyons.dev - Check Cloudflare DNS settings
SSL errors:
- Ensure SSL/TLS mode is Full (strict)
- Wait for certificate provisioning
- Check SSL/TLS → Edge Certificates
CI/CD Integration¶
GitHub Actions + Cloudflare¶
Full workflow in .github/workflows/cloudflare.yml:
- name: Deploy to Cloudflare Pages
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: chrislyons-dev
directory: dist
branch: ${{ github.ref_name }}
Cost¶
Free Tier¶
Includes:
- Unlimited requests
- Unlimited bandwidth
- 500 builds/month
- 20,000 functions requests/day (if using functions)
Pro Tier ($20/month)¶
Adds:
- 5,000 builds/month
- Advanced DDoS protection
- Faster builds
- Priority support
Comparison with Other Platforms¶
| Feature | Cloudflare | Vercel | Netlify |
|---|---|---|---|
| Bandwidth | Unlimited | 100GB | 100GB |
| Build minutes | 500/mo | 6,000/mo | 300/mo |
| Edge locations | 300+ | 100+ | 100+ |
| Functions | Yes (free) | Yes (paid) | Yes (125k/mo) |
| Analytics | Free | Paid | Paid |