Cloudflare Pages & Workers
Deploy to the world's largest edge network — unlimited bandwidth, Workers for backend logic, and R2 for storage
Skills taught: deployment.cloudflare-pages, deployment.cloudflare-workers, deployment.edge-computing, deployment.r2, deployment.wrangler
Cloudflare Pages — the challenger
Cloudflare Pages is newer than Vercel and Netlify but has a compelling advantage: it runs on Cloudflare's network — 300+ cities worldwide, the same CDN that handles 20% of internet traffic. Free tier has unlimited bandwidth (Vercel and Netlify cap free tier). Cloudflare Workers (serverless functions at the edge) run in V8 isolates, not Node.js — cold starts are ~0ms. If you already use Cloudflare for DNS, adding Pages is seamless.
Deploying to Cloudflare Pages
# Install Wrangler (Cloudflare's CLI)
npm install -g wrangler
# Login
wrangler login
# Deploy a Pages project directly
wrangler pages deploy ./dist \
--project-name my-react-app
# Or via GitHub integration:
# Cloudflare Dashboard → Pages → Create project → Connect to Git
# Same auto-build workflow as Vercel/Netlify
# Framework build settings (auto-detected or manual):
# React/Vite: Build command: npm run build, Output: dist
# Next.js: Build command: npm run build, Output: .vercel/output (with @cloudflare/next-on-pages)
# Astro: Build command: npm run build, Output: dist
# Check deployment status
wrangler pages deployment list --project-name my-react-app
Cloudflare Workers — edge functions
// Cloudflare Workers run IN the network, at the edge
// ~0ms cold start, runs in every Cloudflare datacenter simultaneously
// Powered by V8 isolates (not Node.js)
export interface Env {
// KV namespace binding for key-value storage
KV: KVNamespace;
// R2 bucket binding for object storage
BUCKET: R2Bucket;
// Secret environment variable
API_KEY: string;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
// Route: GET /api/user/:id
if (url.pathname.startsWith('/api/user/')) {
const userId = url.pathname.split('/').pop()!;
// KV Store: ultra-fast global key-value
const cached = await env.KV.get(`user:${userId}`);
if (cached) {
return new Response(cached, {
headers: { 'Content-Type': 'application/json', 'X-Cache': 'HIT' },
});
}
// Fetch from origin and cache
const user = await fetch(`https://api.myapp.com/users/${userId}`);
const data = await user.text();
await env.KV.put(`user:${userId}`, data, { expirationTtl: 300 }); // cache 5 min
return new Response(data, {
headers: { 'Content-Type': 'application/json', 'X-Cache': 'MISS' },
});
}
return new Response('Not found', { status: 404 });
},
};
wrangler.toml — configuration
name = "my-worker"
main = "src/index.ts"
compatibility_date = "2024-01-01"
# KV Namespace binding
[[kv_namespaces]]
binding = "KV"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# R2 Bucket (like S3, but free egress)
[[r2_buckets]]
binding = "BUCKET"
bucket_name = "my-app-storage"
# D1 Database (SQLite at the edge — free tier)
[[d1_databases]]
binding = "DB"
database_name = "myapp-db"
database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# Secrets (set with 'wrangler secret put API_KEY')
# They appear as env.API_KEY in the worker
[vars]
NODE_ENV = "production"
PAGES:
✓ Unlimited bandwidth
✓ Unlimited requests
✓ 500 builds/month
✓ Preview deployments
WORKERS:
✓ 100,000 requests/day (free)
✓ 10ms CPU time per request
✓ 128MB memory
✓ ~0ms cold starts
KV (Key-Value):
✓ 100,000 reads/day free
✓ 1,000 writes/day free
✓ 1GB storage free
R2 (Object Storage):
✓ 10GB storage free
✓ 10M Class-A ops free
✓ FREE EGRESS (unlike S3 which charges for data out!)
D1 (SQLite Database):
✓ 5GB storage free
✓ 25M rows read/day free
✓ 100k rows written/day free
Tip: R2 is S3 with zero egress cost. AWS charges $0.09/GB to download data from S3. Cloudflare R2 charges nothing for data downloads. For media-heavy apps (images, video, downloads), R2 can save significant money. R2 is S3-compatible — you can use the AWS SDK pointing at R2's endpoint, or use the native R2 bindings in Workers.
Frequently Asked Questions
What are the limitations of Cloudflare Workers vs Node.js?
Workers run in V8 isolates, not Node.js. This means: no Node.js built-in modules (no 'fs', 'path', 'crypto' — use Web Crypto API instead), limited npm package compatibility (packages using Node.js internals don't work), 128MB memory limit, and 50ms CPU time on the free plan (paid: 30s). Workers use Web APIs (fetch, Request, Response, URL) — the same APIs as modern browser JavaScript. Most npm packages designed for the web (not Node.js) work fine.