Integration Guides · 6 MIN
How to deploy a static site to Cloudflare Workers with Wrangler
Deploy a static site on Cloudflare Workers static assets with Wrangler, add a Worker only where you need code, and use .assetsignore so private files never go live.
To deploy a static site to Cloudflare Workers, point the assets.directory setting in wrangler.jsonc at your built files, test with wrangler dev, then run wrangler deploy with that config file named explicitly. If the assets directory is your repo root, add an .assetsignore file first, because Wrangler uploads everything else in it. This guide shows the setup we run and the tests that prove it.
- Workers static assets serves plain files directly, and your Worker code runs only for the routes you choose.
-
.assetsignoreuses.gitignoresyntax and lives in the root of the assets directory. - Without it, our local test served
/.env,/.git/configand the Worker source with status 200. -
_headersand_redirectsfiles work on Workers static assets the same way they do on Pages. - Always name the config file and dry-run before a deploy, then check the output names the Worker you expected.
- Nactore is a technology and growth company that builds, ships, ranks and grows products, and our own site is deployed this way.
How do you host a static site on Cloudflare Workers?
Cloudflare Workers hosts a static site through static assets. Wrangler uploads a folder of HTML, CSS and images, and Cloudflare serves any request that matches a file without running code. You add a Worker script only when a route needs logic, such as a contact form or an API. One project then covers both.
| Question | Pages | Workers static assets |
|---|---|---|
| Plain static files | Yes | Yes |
_headers and _redirects | Yes | Yes, natively supported |
| Server code on some routes | Pages Functions | The Worker, with run_worker_first |
| Wider platform features | Fewer | Cloudflare calls the Workers feature set "distinctly broader" |
Cloudflare's static assets docs cover the full option list.
Step 1: Write the Wrangler config
A small site needs four files plus your pages. This is the layout we tested.
my-site/
index.html
404.html
blog/first-post.html
_headers
.assetsignore
worker.js
wrangler.jsonc{
"name": "my-site",
"main": "worker.js",
"compatibility_date": "2026-11-01",
"assets": {
"directory": ".",
"binding": "ASSETS",
"html_handling": "auto-trailing-slash",
"not_found_handling": "404-page",
"run_worker_first": ["/api/*"]
}
}Set compatibility_date to the day you create the project. not_found_handling: "404-page" serves your 404.html with a real 404 status. With html_handling set, /blog/first-post serves blog/first-post.html.
Step 2: Run code only where you need it
By default a request that matches a file never reaches your Worker. run_worker_first sends the listed paths to the Worker first, so /api/* gets code while every page stays a plain file.
// worker.js
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname === "/api/health") {
return Response.json({ ok: true });
}
return env.ASSETS.fetch(request); // everything else is a static file
},
};Security headers go in _headers, which applies to every matching path.
/*
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-originStep 3: Write .assetsignore before the first deploy
An .assetsignore file tells Wrangler which files in the assets directory never to upload. The binding docs say it uses the same format as .gitignore and sits in the root of the assets directory. They list no default exclusions, so if your site lives in the repo root, list your secrets, tooling and Git data yourself.
# never upload these, the assets directory is the repo root
.git
.env*
.wrangler
node_modules
wrangler.jsonc
worker.js
private-notesWe ran the test site with wrangler dev (Wrangler 4.163.0) and requested each path, once with this file and once without it.
| Path | With .assetsignore | Without it |
|---|---|---|
/ and /blog/first-post | 200 | 200 |
/api/health | 200 from the Worker | 200 from the Worker |
/.env | 404 | 200 |
/.git/config | 404 | 200 |
/worker.js and /wrangler.jsonc | 404 | 200 |
/private-notes/plan.md | 404 | 200 |
The safer layout is a public/ folder as the assets directory, so nothing else is in reach. We publish from the repo root, so our .assetsignore is the line between public and private.
Step 4: Test locally, dry-run, then deploy
- Log in once. Run
npx wrangler login. - Run it locally.
npx wrangler dev --config wrangler.jsoncand request the private paths. They should 404. - Dry-run.
npx wrangler deploy --config wrangler.jsonc --dry-runbundles everything without publishing. - Deploy.
npx wrangler deploy --config wrangler.jsonc, then confirm the output namesmy-site. - Add your domain. Attach it under the Worker's domains settings, per Cloudflare's custom domains guide.
The dry-run line "Read N files from the assets directory" is not proof of what goes live. In our test it counted ignored files too. The local requests in step 2 are the real check.
Name the config file on every deploy. A deploy that finds no config file can fall back to defaults, and that is how a wrong folder gets published.
What went wrong for us
| Problem | What happened | Fix |
|---|---|---|
| Wrong folder published | A deploy run without a config file published another project's folder, .git included, to a new workers.dev Worker. We deleted it within minutes | --config and --dry-run every time |
| Duplicate URLs indexed | Always Use HTTPS was off, so http:// pages served 200 and Google indexed both | Turn on Always Use HTTPS |
wrangler dev refused to start | An old global Wrangler did not support a newer compatibility date | Use npx wrangler@latest |
| AI answer engines blocked | A firewall rule returned 403 to AI crawlers | See our Cloudflare field notes |
Frequently asked questions
Should I use Cloudflare Pages or Workers for a new static site?
Both serve static files well. Workers static assets keeps static files and server code in one project and gives you the wider Workers feature set, so we use it for new sites. Cloudflare publishes a migration guide for moving from Pages.
Does Wrangler skip .git automatically?
Cloudflare's docs list no default exclusions for .assetsignore, and our local test served /.git/config when the file was missing. List .git yourself.
Do _headers and _redirects work on Workers?
Yes. Cloudflare's migration guide says both are supported natively in Workers static assets. Keep them in the assets directory.
Can I test a Workers site without deploying?
Yes. wrangler dev serves the same assets and Worker locally, and wrangler deploy --dry-run builds without publishing.
When to get help
If your site is slow to ship, leaks files it should not, or will not rank, tell us what you see or see what we build. Nactore is one partner that builds, ships, ranks and grows, measured on your growth.
Want to apply this to your business?
Book a free 30-minute call. We will tell you what we would do first.