Deploy web projects to Cloudflare Pages/Workers using wrangler CLI
Deploy static sites to Cloudflare Pages and serverless APIs to Cloudflare Workers using the wrangler CLI.
wrangler CLI installed: npm install -g wrangler (or npx wrangler)wrangler login or set CLOUDFLARE_API_TOKEN env varCLOUDFLARE_ACCOUNT_ID env var (or wrangler whoami to find it)The recommended flow:
wrangler for manual/CLI deploysConnect a GitHub repo to Pages — auto-deploys on every push:
# Create via dashboard or API. Once connected:
# - Push to main → production deploy
# - Push to branches → preview deploys
# - URL: https://<project>.pages.dev
# Deploy a directory of static files
wrangler pages deploy ./dist --project-name=my-site
# First deploy creates the project automatically
# Subsequent deploys update it
name = "my-site"
pages_build_output_dir = "./dist"
# If using Pages Functions (server-side)
# Place functions in ./functions/ directory
| Framework | Build Command | Output Dir |
|---|---|---|
| Vite/React | npm run build | dist |
| Next.js (static) | npx next build && npx next export | out |
| Astro | npx astro build | dist |
| Hugo | hugo | public |
| Plain HTML | (none) | . |
# Scaffold a new Worker project
npm create cloudflare@latest my-api
# Or manually create wrangler.toml + src/index.ts
name = "my-api"
main = "src/index.ts" # or index.js
compatibility_date = "2024-01-01"
# Optional bindings
# [vars]
# API_KEY = "..."
# KV namespace
# [[kv_namespaces]]
# binding = "MY_KV"
# id = "abc123"
# R2 bucket
# [[r2_buckets]]
# binding = "MY_BUCKET"
# bucket_name = "my-bucket"
# D1 database
# [[d1_databases]]
# binding = "DB"
# database_name = "my-db"
# database_id = "xxx"
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/api/hello") {
return Response.json({ message: "Hello from Workers!" });
}
return new Response("Not Found", { status: 404 });
},
};
# Deploy to production
wrangler deploy
# Deploy to staging (route based)
wrangler deploy --env staging
# Dev mode (local)
wrangler dev
https://<project>.pages.devmy-project/
├── site/ → Cloudflare Pages (static)
│ ├── index.html
│ └── assets/
├── api/ → Cloudflare Workers (dynamic)
│ ├── src/index.ts
│ └── wrangler.toml
└── .github/
└── workflows/
└── deploy.yml → GitHub Actions builds both
# .github/workflows/deploy.yml