A team has a marketing site or docs portal in a Git repo — Astro, Next.js static export, Hugo, Eleventy, plain HTML, doesn’t matter — and wants every push to main to produce a live URL, every PR to produce a unique preview URL, and the whole thing to ride on Cloudflare’s edge with HTTPS, caching, and DDoS protection already attached. This article walks the connect-the-repo flow end to end, calls out the framework defaults Cloudflare ships, shows the CLI for the workflows where Git is not the trigger, and lists the gotchas that bite first-time projects.
How to verify
Before connecting, confirm the repo builds locally with the exact command you intend to give Cloudflare. The framework’s own dev/build commands are the source of truth — Pages does not run a sandbox of your code, it runs the literal command you provide in a Linux container with Node.js available.
# 1. Confirm the build works locally
cd my-site
npm ci
npm run build
ls -lah dist/ # or .output/public/, build/, public/ — depends on framework
# 2. Confirm Wrangler is installed for CLI deploys / log tail
npx wrangler --version
# 3. Once a Pages project exists, list deployments
npx wrangler pages deployment list --project-name=my-site
If the local npm run build produces an output directory with index.html at the root, Cloudflare will serve it. If the build fails locally, it will fail on Pages with the same error — the build container is a Linux runner, not a magic remediator.
What’s happening
Cloudflare Pages is a Git-driven static hosting product layered on the same edge network that serves the rest of Cloudflare’s traffic. When you connect a repo, Cloudflare installs a GitHub or GitLab app, registers webhooks on push and pull request events, and provisions a build container per event. The build runs your build command inside that container, captures the contents of your build output directory, uploads the static assets to Cloudflare’s storage, and exposes them through a <project>.pages.dev hostname plus any custom domains you’ve attached. Every successful build produces a unique immutable URL (<hash>.<project>.pages.dev); the main/production branch alias points at the latest successful production deployment.
The mental model that helps: a Pages project is a (repo, branch) pair plus a build recipe. The recipe is build command + output directory + environment variables + Node version. Production deployments come from the production branch; preview deployments come from every other branch (including PR branches). Previews are a real, isolated environment — they get their own URL, their own bindings if configured, their own analytics — and they never share state with production.
The procedure
1. Connect the repository
In the Cloudflare dashboard, go to Workers & Pages → Create application → Pages → Connect to Git. The first time you do this, Cloudflare prompts you to install its app on the GitHub or GitLab account/organization that owns the repo. Grant it access to the specific repos you want — granting org-wide is unnecessary and audit-noisy. Pick the repo, pick the production branch (almost always main).
2. Set the build recipe
Pages auto-detects most common frameworks and pre-fills the build command and output directory. Verify the autofill matches your project — the detection reads package.json/lockfiles but can guess wrong on monorepos or polyglot repos. The fields you set:
- Framework preset —
Noneis fine if your build is justnpm run build. The preset only changes the defaults below. - Build command — e.g.
npm run build,pnpm build,hugo --minify. Run this exact command locally first. - Build output directory — the folder relative to the repo root that contains
index.htmlafter build. Common values:dist/,.output/public/(Nuxt),build/(CRA),out/(Next.js static export),public/(Hugo, Gatsby). - Root directory — leave blank unless the project lives in a subfolder of a monorepo.
- Environment variables — anything your build references via
process.env.X. Split between Production and Preview. Mark secrets as encrypted; encrypted values are write-only in the dashboard after creation.
Save and trigger the first deploy. The build log streams live. A typical first failure is the wrong output directory — Cloudflare reports “no files uploaded” rather than a build error.
3. Pin the Node version
Pages picks a Node version based on the project’s signals (.nvmrc, package.json#engines.node, an env var). To pin it explicitly, set the NODE_VERSION environment variable to e.g. 20.11.0. Pinning matters because a default Node bump can break a build months later with no code change on your side.
4. Attach a custom domain
In the project settings, Custom domains → Set up a custom domain. Enter www.example.com or the apex example.com. If the domain’s DNS is on Cloudflare in the same account, Pages auto-creates a CNAME (or CNAME-flattened apex). If the domain is hosted elsewhere, Cloudflare shows you the CNAME target to add at your DNS provider. TLS is issued via the Universal certificate flow; expect a minute or two for the cert to attach.
For apex domains, prefer keeping DNS at Cloudflare — CNAME flattening at the apex is one of the things Cloudflare does that most other DNS providers do not.
5. Use Wrangler when Git is not the trigger
Some workflows produce build artifacts outside CI — a content team running a one-off prebuild, a generated changelog from a different repo, a release pipeline that needs to push artifacts on a schedule. The CLI handles these cases without going through Git:
# Authenticate once
npx wrangler login
# Deploy a directory as a new build in the named project
npx wrangler pages deploy ./dist \
--project-name=my-site \
--branch=main \
--commit-message="manual deploy 2026-06-02"
# Or deploy to a specific preview branch
npx wrangler pages deploy ./dist \
--project-name=my-site \
--branch=hotfix-banner
The CLI accepts the same --branch semantics as Git: main (or whatever you named the production branch) goes to production; anything else gets a preview alias. The commit message shows up in the deployment list and is the easiest way to label CLI-driven deploys.
6. Wire previews into PR review
The GitHub/GitLab app posts a deployment status on each PR with the preview URL. Anyone reviewing the PR clicks the URL and sees the actual built site for that branch — no checkout, no local build. For pre-prod testing, add a Pages-level Preview deployment access policy in Settings → Access policy so the preview URL requires SSO. This is the cheapest way to keep work-in-progress out of search engines and away from random visitors who guess the hash.
Common pitfalls
- Build output directory mismatch —
npm run buildsucceeds locally but Pages says “no files uploaded”. The build wrote toout/and you typeddist/; correct the field and redeploy. - Node version drift — a build that worked last quarter fails today because Pages bumped its default Node and your dependencies don’t like the new version. Set
NODE_VERSIONexplicitly. - Monorepo root not set — the build runs from the repo root, can’t find
package.json, and bails. Set Root directory to the subfolder. - Secrets logged at build time —
echo $API_KEYin a build script writes the secret to the build log, which is visible to anyone with project access. Never echo secrets. - Custom domain stuck on “Verifying” — usually a DNS propagation lag if the domain isn’t on Cloudflare; sometimes a stale CNAME pointing at a different Pages project. Confirm
dig CNAME www.example.comresolves to the project hostname Pages is asking for.
For teams running Pages alongside Workers, R2, and the rest of the edge stack, Stack Harbor handles the connect-the-repo setup, build-recipe hardening, custom domain attachment, and the alerting that catches a silent build regression — see how we run managed operations for client environments. The companion article on Pages Functions covers adding server-side routes when the static-only model isn’t enough.