Cloudflare Pages: Zero to Production

January 2025

Cloudflare Pages transforms static site deployment from a complex DevOps challenge into a simple git push workflow. Here's everything you need to know about deploying your projects with zero configuration and global edge distribution.

Why Cloudflare Pages?

Traditional hosting solutions often require manual server management, complex deployment pipelines, and expensive CDN configurations. Cloudflare Pages eliminates this complexity:

Setting Up Your First Project

Prerequisites

Step 1: Connect Your Repository

  1. Navigate to the Cloudflare Dashboard
  2. Select "Pages" from the sidebar
  3. Click "Create a project"
  4. Choose "Connect to Git"
  5. Authorize Cloudflare to access your GitHub account
  6. Select your repository from the list

Step 2: Configure Build Settings

For static HTML sites like this one:

Framework preset: None
Build command: (leave empty)
Build output directory: /
Root directory: (leave empty)

For popular frameworks, Cloudflare auto-detects settings:

React: npm run build → build/
Vue.js: npm run build → dist/
Next.js: npm run build → out/
Gatsby: npm run build → public/

Step 3: Deploy

Click "Save and Deploy" - your site deploys automatically to a *.pages.dev subdomain.

Custom Domain Configuration

Adding your own domain is straightforward when using Cloudflare DNS:

For Cloudflare-Managed Domains

  1. Go to your Pages project settings
  2. Navigate to "Custom domains"
  3. Click "Set up a custom domain"
  4. Enter your domain (e.g., splarf.com)
  5. Cloudflare automatically creates the necessary DNS records

For External DNS Providers

Type: CNAME
Name: www (or @)
Target: your-project.pages.dev

Advanced Configuration

Environment Variables

Set build-time variables for different environments:

Production:
  API_URL=https://api.mysite.com
  ANALYTICS_ID=GA_PROD_123

Preview:
  API_URL=https://staging-api.mysite.com
  ANALYTICS_ID=GA_DEV_456

Redirects and Rewrites

Create a _redirects file in your project root:

# Redirect old blog URLs
/blog/* /posts/:splat 301

# SPA fallback
/* /index.html 200

# API proxy
/api/* https://backend.example.com/api/:splat 200

Headers Configuration

Add security headers with _headers:

/*
  X-Frame-Options: DENY
  X-Content-Type-Options: nosniff
  Referrer-Policy: strict-origin-when-cross-origin
  Content-Security-Policy: default-src 'self'

/api/*
  Access-Control-Allow-Origin: *
  Cache-Control: no-cache

Performance Optimization

Caching Strategy

Cloudflare automatically caches static assets with optimal headers:

Image Optimization

Enable automatic image optimization:

<img src="/hero.jpg" 
     alt="Hero image"
     loading="lazy"
     width="800" 
     height="400">

Cloudflare automatically serves WebP/AVIF to supporting browsers.

Monitoring and Analytics

Built-in Analytics

Pages provides detailed metrics:

Real User Monitoring

Enable RUM for performance insights:

<script defer src='https://static.cloudflareinsights.com/beacon.min.js' 
        data-cf-beacon='{"token": "your-token"}'></script>

CI/CD Integration

Automatic Deployments

Every git push triggers a new deployment:

Build Hooks

Trigger deployments from external systems:

curl -X POST "https://api.cloudflare.com/client/v4/pages/webhooks/deploy_hooks/YOUR_HOOK_ID"

Security Features

DDoS Protection

Automatic protection against attacks with Cloudflare's global network.

Access Control

Restrict access to preview deployments:

Cost Analysis

Free Tier Limits

Pro Features ($20/month)

Common Troubleshooting

Build Failures

# Check build logs in dashboard
# Verify package.json scripts
# Ensure dependencies are in package.json, not devDependencies

DNS Issues

# Check DNS propagation
dig yourdomain.com

# Verify CNAME records
dig www.yourdomain.com CNAME

Cache Issues

# Purge cache via dashboard
# Add cache-busting query parameters
# Check cache headers in browser dev tools

Migration from Other Platforms

From Netlify

From Vercel

Best Practices

Cloudflare Pages represents the evolution of static site hosting - combining the simplicity of traditional hosting with the power of modern edge computing. For projects like this blog, it provides enterprise-grade infrastructure with zero operational overhead.

The combination of git-based workflows, global distribution, and automatic optimizations makes it an ideal choice for developers who want to focus on building rather than managing infrastructure.