This week I moved this site from a small server to Cloudflare. Before, a script built the site on my laptop and copied the files with rsync. The server ran nginx behind a Cloudflare Tunnel (I wrote about that setup in nginx behind a Cloudflare Tunnel). Now I push to Git, and Cloudflare builds and publishes the site.
Astro joined Cloudflare in January, but I want this site to stay free of platform-specific code, and it still is. Cloudflare only serves the same dist/ folder of plain files, and the config below is the only Cloudflare-specific file. If I want to leave, I can copy dist/ to any web server again.
What you get
- Automatic build and deploy. Push to
main, and Cloudflare runs your build command and publishes the result. This is a real CI/CD pipeline, and I did not have to write one. - A preview for every branch. Push any other branch and you get a preview address. The address for one branch stays the same, and it shows the latest commit of that branch. You can send it to someone and fix things before you merge.
- Access control for previews. You can put a login in front of the previews, so only the people you list can open them. Production stays public.
- One-click rollback. Recent deploys are kept. To go back, pick an older one in the dashboard and press Rollback. No rebuild is needed.
- No server to look after. No nginx config, no certificates, no open ports, no disk space to watch. The site does not depend on my server being online.
- Free for this kind of site. More on the limits below.
What you need in the repo
A static site only needs two small things in the repo:
- The build output folder is declared in a Wrangler config file,
wrangler.jsonc. - Optional headers go into a
_headersfile inpublic/.
You do not type the output folder in the dashboard. The dashboard asks for a build command and a deploy command, and the deploy command reads the folder from the config file. The .jsonc format allows comments. This is the file I use:
{
// must match the Worker name in the dashboard, or the build fails
"name": "my-site",
"compatibility_date": "2026-10-12",
"workers_dev": false,
"preview_urls": true,
// required by `wrangler preview`, the command used for non-main branches
"previews": {},
"assets": {
"directory": "./dist",
"not_found_handling": "404-page",
"html_handling": "auto-trailing-slash"
}
}
In the dashboard I kept the defaults: build command npm run build, deploy command npx wrangler deploy, and preview builds turned on.
I also keep Wrangler as a dev dependency (npm install --save-dev wrangler) and commit the lockfile. Then npx wrangler in the build uses a known version, not whatever is newest that day. This matters because wrangler preview needs a recent Wrangler.
Steps
-
Add the config file above and a
public/_headersfile. For a long cache on the hashed Astro files:/_astro/* Cache-Control: public, max-age=31536000, immutable -
Push these files to GitHub and merge them before you press Deploy in the dashboard. Without a config file, Cloudflare tries to configure the project by itself and opens a pull request. It may add things a static site does not need. If a config file already exists, this automatic step is skipped.
-
In the dashboard, create a project from the Git repository. Use the same name as in the config file.
-
Push a branch, open its preview address, and check the site: home page, a post, tag pages, RSS, sitemap, a missing page (it must show your 404 page and return status 404).
-
Add your own domain under the Worker’s domain settings.
-
Redirect
wwwto the root domain. -
Watch the site for a few days. Then stop the old server.
Traps I hit
The domain is already in use. Adding a custom domain failed because the old Tunnel had created a DNS record for it. Delete that record first (or remove the hostname from the Tunnel), then add the domain right away. The gap is short.
www needs a proxied DNS record. A redirect rule only runs if the traffic goes through Cloudflare. I added an A record for www with a placeholder address 192.0.2.1 and the orange cloud on. Then I made a redirect rule from www.example.com to https://example.com plus the path, status 301, with the query string kept. Right after I saved the rule, a request returned error 522. It worked a minute later. This is only what I saw, not a general fix for 522, but wait a little before you decide something is wrong.
wrangler preview needs a previews block. My first preview build on a branch failed with a message that the config has no previews block. An empty "previews": {} is enough. This is easy to miss: builds on main still succeed, and only the check on the pull request fails.
“View logs” on a failed preview build can show “page not found”. The link in the GitHub check pointed to .../production/builds/<id>. Links for preview builds that worked contain .../previews/<branch>/builds/<id>. Open the Worker in the dashboard and find the build in its deployment history instead.
Preview URLs must stay on. I first set preview_urls to false because I did not want public preview addresses. The preview addresses then could not be opened. I turned it back on and used Access to protect them instead. I also do not set it to false in the file, because a deploy may apply that value to production.
Access policy: use “Emails” in a reusable policy. In the Worker’s Access settings, the only email choice is “Email domain”. It matches the end of the address, like @example.com. A full address typed there matches nobody, and the one-time code is never sent. Cloudflare does not send a code to an address that no policy allows, so “no code arrives” means the policy does not match. To allow single addresses, go to Zero Trust → Access controls → Reusable policies. There you can choose “Emails ending in” (the same as “Email domain”) or Emails (full addresses). Create a policy with Emails, list the full addresses, and use it for the previews.
Do not protect the whole Worker. The Worker-level Access setting also covers the custom domain, so it would lock your public site. Use the scope called “Previews only”, then open a preview in a private window and check that it asks for a login. Also check that the real site still returns 200.
The first version number can look wrong. I show the branch and commit in the page footer. The very first build, started by hand from the dashboard, reported the branch name as the commit. Later builds started by a push show the real commit. If you see something odd on your first deploy, push once more before you debug.
Build watch paths do not stop preview builds. Every push starts a build, also a push that only changes documentation. “Build watch paths” in the build settings are meant to skip such builds. On a second site I kept the include path * and excluded the docs folder, the deploy scripts and the README. Then I pushed test branches. Each push changed only excluded files: a file in the docs folder, a script, README.md. Every push still built and published a preview.
The cause is an open bug, cloudflare/developer-platform#75. For each branch, Cloudflare creates its own build trigger. That trigger runs wrangler preview, watches the path * and does not read your watch paths. The settings in the dashboard belong to the production branch. I have not tested if they skip builds on main.
The issue describes how to turn off preview builds through the API (previews_enabled: false), but then you lose the previews. I keep them. A preview build takes about a minute, and the free plan has enough build minutes. If you test watch paths yourself, change a real file: a push with no file changes, such as an empty commit, always builds.
Limits on the free plan
These are the numbers I checked in the docs. Plans change, so look at the current pages before you rely on them.
- Static files: up to 20,000 files per version, and each file up to 25 MiB.
- Builds: 3,000 build minutes per month, one build at a time, 20 minutes per build.
- Dynamic code (Worker scripts): 100,000 requests per day and 10 ms of CPU time per request.
- Previews are public by default. Anyone who has the link can open them. Protect them if the content is private.
- No long-running process and no writable local disk. It is not a small virtual machine.
Requests for static files are free and unlimited. They do not count against the daily request limit. Only requests that run Worker code count.
Which sites fit
A good fit:
- Blogs, documentation, portfolio and company sites that are built to static files.
- Sites where several people review changes before they go live.
- Sites that need a few small dynamic parts: a form, a counter, a small API.
Not a good fit:
- Programs that must keep running, such as a Spring Boot service, a queue worker or a game server.
- Software that expects a normal server with a writable disk and a MySQL database, such as WordPress.
- Pages that need more CPU time per request than the free plan gives.
I will keep my future Java service on my own server. It will use its own subdomain through the Tunnel. The Cloudflare site calls it over HTTPS, and the service must allow that origin with CORS. A plain static site cannot proxy a path like /api/ on the main domain to that service. If I want that later, I need a small Worker script that forwards /api/* requests.
Where it can grow
Today this is a plain static site. The same project can add a Worker script and connect to Cloudflare storage later: D1 (SQL), KV (key-value), R2 (files). You add an entry file and the bindings to the same config file. Then some paths run code, and the rest stay static files. I have not done this yet, so I will write about it when I do.
Summary
For a static Astro site, this move removed my deploy script, my nginx config, and the server from the request path. I gained previews, access control and rollback. The things to remember: put the config file in the repo before the first deploy, keep preview URLs on, use the “Emails” selector in Access, and test the preview and the production site after every change to access rules. Do not count on build watch paths to skip branch previews.