Deploying a Portfolio on Cloudflare Pages: Custom Domain, HTTPS, and www Redirect
Build a Developer Portfolio · Part 7 of 9
- #Portfolio
- #personal-website
- #cloudflare
- #deployment
- #devops
Getting a static site onto the internet takes about ten minutes. Getting it
onto your domain, over HTTPS, with www redirecting to the right place,
unknown URLs returning a real 404 and a way back when a deploy goes wrong,
takes longer. Most of that time goes to steps that look done before they
actually are. This post walks through how to deploy a portfolio on
Cloudflare Pages with all of that working, using the setup that serves
this site as the example.
Part 3 said to deploy an empty page on day one and to test the domain, HTTPS and redirect before building anything else. This is the detailed version of that step. It assumes you already have a build that produces a folder of static files (if you're still choosing how to produce it, Part 5 compares the options).
Nothing here needs a paid plan. A portfolio is nowhere near the free tier's limits, which I'll come back to near the end.
The moving parts
There are fewer pieces than the Cloudflare dashboard makes it look:
- A Pages project. It holds your deployments and gives you a
<project>.pages.devhostname. - Something that uploads a build to it. Either Cloudflare builds your repository itself, or your CI builds it and uploads the result.
- A domain whose DNS is on Cloudflare, with a custom domain attached to the project.
- A redirect so the hostname you didn't choose sends people to the one you did.
The rest of the post goes through them in that order.
Git integration or Direct Upload
When you create a Pages project, Cloudflare asks how deployments will arrive, and the choice is more permanent than it looks.
- Git integration. You connect the GitHub or GitLab repository, give Pages a build command and an output folder, and Cloudflare builds and deploys every push. Pull requests get preview deployments automatically. It's the least work by far.
- Direct Upload. Cloudflare never sees your repository. You build
wherever you like and upload the finished folder with
wrangler pages deploy, usually from GitHub Actions.
Cloudflare's docs are explicit that a Direct Upload project can't be switched to Git integration later: you'd create a new project and move the domain over. So pick deliberately.
Git integration is the right default if your build is ordinary and you
want the fewest moving parts. Direct Upload is worth the extra YAML when
you want the deploy to run after the same checks your pull requests run,
in a build environment you control (same Node version from .nvmrc, same
lockfile, same post-build scripts), with one pipeline you can read top to
bottom. This site uses Direct Upload for that reason: its build prerenders
every page and then verifies the output, and a deploy that skipped that
verification would be a deploy nobody should trust. If you want the
general version of that argument,
Building CI/CD Pipelines Your Team Actually Trusts
makes it.
The deploy workflow
This is the workflow that deploys this site, lightly trimmed. It runs on
every push to main:
name: Deploy
on:
push:
branches: [main]
workflow_dispatch:
concurrency:
group: deploy-production
cancel-in-progress: true
permissions:
contents: read
deployments: write
jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: production
url: https://josealencar.dev
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version-file: .nvmrc
cache: npm
- run: npm ci
- run: npm run build
- name: Deploy to Cloudflare Pages
uses: cloudflare/wrangler-action@v4
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: >-
pages deploy dist/client
--project-name=${{ vars.CLOUDFLARE_PROJECT_NAME }}
--branch=main
A few lines in there are doing more than they seem to.
concurrency with cancel-in-progress. If you merge two pull requests
a minute apart, the first deploy is cancelled and only the second one
ships. Without it, two runs race and whichever finishes last wins, which
isn't always the newer commit.
The token's scope. Create the API token in the Cloudflare dashboard with a single permission, Account → Cloudflare Pages → Edit, limited to your account. Don't use the Global API Key, and don't give the token DNS or zone permissions it doesn't need. If this token leaks, the worst case should be "someone deployed to my portfolio", not "someone changed my DNS".
Secrets vs. variables. The token and the account ID go in GitHub secrets, so they're masked in logs and never in the repository. The project name isn't sensitive, so it's a plain Actions variable. Keep the account ID secret anyway: it isn't a credential, but there's no reason to publish it alongside your token-shaped YAML.
--branch=main. For a Direct Upload project, the branch you pass is
what decides whether an upload is production or a preview. Uploads tagged
with the project's production branch replace what your domain serves;
anything else becomes a preview. Pass it explicitly, so the decision is in
the workflow and not inferred from wherever the runner happened to check
out.
The environment block. It's optional, but it gives you a deployment
history in the GitHub UI with a link to the live URL, and it's where you'd
add a required reviewer if you ever wanted a human to approve production
deploys.
The output folder (dist/client here) is whatever your build produces.
Upload exactly the folder you want served, and nothing else: anything in
it is public.
Preview deployments
Run the same wrangler pages deploy with --branch=some-feature and you
get a preview deployment: it doesn't touch production, and it's reachable
at a unique hash URL (<hash>.<project>.pages.dev) plus a stable alias
for the branch (some-feature.<project>.pages.dev). With Git integration
you get these for free on every pull request. With Direct Upload you add a
job that runs on pull_request and deploys with the PR's branch name.
Two things to know before you start handing out preview links:
- Previews are public by default. Anyone with the URL can open them. For a portfolio that's usually fine. If a preview might show a draft you don't want seen yet, Cloudflare Access can put a login in front of preview hostnames (Settings → General → enable the access policy).
- They don't hurt your SEO. Preview URLs are sent with an
X-Robots-Tag: noindexheader, so a crawler that finds one won't index a second copy of your site.
Putting it on your own domain
The pages.dev hostname works, but a portfolio should live on a domain
you own. How easy this is depends on where the domain's DNS is.
For an apex domain (example.dev, no subdomain), the domain has to be
a zone on Cloudflare: you change the nameservers at your registrar to the
two Cloudflare gives you. Apex records can't be a normal CNAME at most DNS
providers, and Cloudflare solves that by being the DNS provider. For a
subdomain (me.example.com) you can keep DNS elsewhere and point a
CNAME at <project>.pages.dev.
Either way, add the domain in the project's Custom domains tab first.
This is the step people skip. A CNAME to <project>.pages.dev that you
create by hand, without telling the Pages project about the domain, gets
you an HTTP 522: the request reaches Cloudflare, and Cloudflare has no
idea which project should answer it. When the zone is on the same
Cloudflare account, adding the custom domain creates the DNS record for
you, so you don't have to write it at all.
Then wait for the certificate. Cloudflare issues it automatically; until it does, the domain shows as pending and HTTPS fails. It's usually minutes.
HTTPS, and why .dev makes you get it right early
If your domain ends in .dev (or .app, and several other Google-run
TLDs), the whole TLD is on the browsers' HSTS preload list. Browsers will
only ever connect to it over HTTPS, even if someone types http://. There
is no "I'll add the certificate later" phase: until HTTPS works, the site
doesn't load at all.
That's mostly a good thing. It means you find out on day one, not after a
recruiter opens a "Not secure" warning. Whatever your TLD, also turn on
Always Use HTTPS for the zone, so plain http:// requests get a 301 to
https:// at the edge.
The www redirect, the step people get half-right
You have two hostnames that people will type, example.dev and
www.example.dev, and you want exactly one of them to be the real site.
Pick one (this site uses the apex), put it in every canonical URL, and
make the other one redirect to it. Two hostnames serving the same pages
splits your links and confuses search engines about which one is real.
If your zone is on Cloudflare and you want www to redirect rather than
serve, it takes two separate pieces, and each one alone fails in a
different way:
- A proxied DNS record for
www. A CNAME fromwwwto the apex, with the orange cloud on (proxied). This doesn't serve anything by itself. It makeswwwresolve and makes the request land on Cloudflare's edge, where rules can run. - A Redirect Rule. Under Rules → Redirect Rules, Cloudflare has a
template called "Redirect from WWW to Root domain". It matches the
wwwhostname and returns a 301 to the apex, keeping the path and the query string.
The failure modes are worth memorising, because they tell you which half is missing:
- Rule but no record → NXDOMAIN. The browser can't even find
www. The rule exists, but no request ever reaches the place where it would run. - Record but no rule → HTTP 522.
wwwresolves, the request reaches Cloudflare, and nothing there is set up to answer for that hostname. - Both → 301 to the apex. Done.
There is a shortcut: add www as a second custom domain on the Pages
project and it will serve the site on www too. That works, but then
you have two hostnames returning the same pages, and you're back to
relying on canonical tags to sort it out. A redirect is the cleaner
answer, and it's what this site does.
Test it with curl, which shows you the redirect instead of following it.
These are the real responses from this site:
curl -sI "https://www.josealencar.dev/blog?x=1"
# HTTP/2 301
# location: https://josealencar.dev/blog?x=1
curl -sI http://josealencar.dev/
# HTTP/1.1 301 Moved Permanently
# Location: https://josealencar.dev/
The first check matters more than it looks: the path and the query
string survived. A redirect that sends every www URL to the home page
technically works and quietly breaks every deep link anyone ever shared.
How Pages decides what each URL serves
Once the domain works, the remaining surprises come from how Pages maps a URL to a file. These rules are documented, but they only start to matter when your build writes more than one HTML file.
Trailing slashes follow your file layout. about.html is served at
/about. about/index.html is served at /about/, with a slash, and
/about.html redirects to /about. If your canonical URLs have no
trailing slash but your build writes about/index.html, every page except
the home page answers with a redirect to a URL you never link to. On this
site that's why a post-build script renames each prerendered
<path>/index.html to <path>.html. Check yours:
curl -sI https://josealencar.dev/about # HTTP/2 200
curl -sI https://josealencar.dev/about/ # HTTP/2 308, location: /about
A 404.html changes what unknown URLs get. If your output has no
404.html, Pages assumes you're deploying a single-page app and serves
your root index.html for any path that doesn't match a file. That's what
a client-side React or Vue app wants, and it's also why so many SPAs
return a 200 with the home page for URLs that don't exist (search engines
call that a soft 404). If your output has a 404.html, Pages serves it,
with a real 404 status. A prerendered site should ship one. An SPA can
ship one too, as long as the page loads the app and lets the router
decide.
Don't add a catch-all to _redirects. The classic SPA recipe on other
hosts is a rule like /* /index.html 200. On Pages you don't need it (the
SPA fallback above already does that job), and it can hurt: the rule also
matches files that exist, so your JavaScript and your sitemap can end up
answering with the HTML shell or a redirect. This site's first
prerendering change added exactly that kind of rule, pointing at a
fallback page, and on Pages it made every URL redirect in a loop. It was
caught in review by running the build under wrangler pages dev,
Cloudflare's local emulator, which serves your output folder with Pages'
rules and publishes nothing. Use it before you ship any change to how
your files are laid out:
npx wrangler pages dev dist/client
curl -sI http://localhost:8788/about # expect 200, not a redirect
Keep _redirects for real redirects, like an old URL that moved.
Rolling back
Sooner or later a deploy breaks something. You have two ways back.
- Roll back in the dashboard. Under the project's Deployments, every successful production deployment has a "Rollback to this deployment" option in its menu. It's instant, because Pages keeps every deployment and just points the domain at an older one. Only production deployments are valid targets; you can't roll back to a preview.
- Revert in git.
git revertthe bad commit and push. The workflow builds and deploys the fixed state. It takes a few minutes instead of a few seconds, butmainand production agree again afterwards.
Use the first to stop the bleeding and the second to make it permanent.
If you only roll back in the dashboard, the next push to main redeploys
the broken code, because it's still there.
If your site is more than a portfolio and you want gradual rollouts rather than "all at once, with an undo button", Deployment Strategies: Blue-Green, Canary, and Rolling covers the options. For a portfolio, instant rollback is plenty.
Limits, for the record
On the free plan, as of Cloudflare's limits page at the time of writing:
- 20,000 files per site and 25 MiB per file. A portfolio with a blog is a few hundred files. The per-file limit only bites if you ship a video or a large PDF; put those somewhere else.
- 500 builds a month, which only counts builds Cloudflare runs for you. With Direct Upload, your CI does the building.
- Unlimited preview deployments and up to 100 custom domains per project.
Check the current page before relying on any of these; limits change.
A checklist before you call it done
Run these against your own domain, and don't trust the browser for them: it caches redirects and HSTS, so it can show you a working site that a first-time visitor won't get.
curl -sI https://example.dev/ # 200
curl -sI http://example.dev/ # 301 to https://
curl -sI "https://www.example.dev/a?b=1" # 301 to https://example.dev/a?b=1
curl -sI https://example.dev/about # 200, not a redirect
curl -sI https://example.dev/does-not-exist # 404, not 200
And outside the terminal:
- The API token has only the Pages permission, and it lives in GitHub secrets.
- A pull request's checks and the deploy run the same build.
- You know where the rollback button is before you need it.
What's next
The site is live, on your domain, and every push to main ships it.
Part 8 is about making sure people can find it: metadata, sitemaps
and structured data for a developer portfolio, and what search engines and
link previews actually read from your pages. Part 9 covers making the
site bilingual.
If you deploy something other than a static site, from a Spring Boot jar to a container, the same habits carry over (one pipeline, a clear production trigger, a fast way back), and Packaging and Running Your App is the place to start.