When moving a Pages build output to Cloudflare Workers static assets, what happens to its _headers, _redirects and _worker.js files, and what does .assetsignore do?
answer
- config files, not content
- rules apply to asset responses only
- server code must not become public
- gitignore syntax at the directory root
basics
~20 sWorkers static assets read _headers and _redirects from the assets directory as rules, not files, applying them only to asset responses. A Pages _worker.js would become a public file, so wrangler deploy refuses it while no .assetsignore exists: move it out or list it there.
solid answer
~40 s`_headers` and `_redirects` work in Workers static assets as they did in Pages: placed at the root of `assets.directory`, they are parsed as rules and are not themselves served or uploaded. Their rules apply only to responses served from assets, never to a `Response` the Worker script builds, and redirects run before headers. A Pages advanced-mode `_worker.js` is different: in Workers it is just a file in the directory and would be published as a public asset, exposing server code, so `wrangler deploy` (and `--dry-run`) stops with an error unless an `.assetsignore` exists. Move it out and point `main` at it, or list it in `.assetsignore`, a gitignore-style file at the directory root. Pages also skipped `node_modules`, `.DS_Store` and `.git` automatically; Workers needs them listed.
code
json · 9 lines{
"name": "spa-move",
"compatibility_date": "2026-10-10",
"main": "./worker/index.js",
"assets": {
"directory": "./dist/client/",
"binding": "ASSETS"
}
}go deeper
Recall that _headers and _redirects keep working at the root of the assets directory and that .assetsignore uses gitignore syntax to keep files out of the upload.
Explain that the rules apply only to asset responses, that redirects run before headers, and why a _worker.js left in the directory would become a public file.
Catch the deploy-time traps: wrangler dev serving _worker.js locally, an empty .assetsignore silencing the guard, and headers or redirects missing from Worker-generated responses.
Make the migration repeatable: a checklist and a wrangler deploy --dry-run step in CI that fails on server code in the public directory, rather than relying on reviewers.
## Why these files need attention A Pages project's build output folder often contains more than content. Pages treated some names specially: `_headers` and `_redirects` held rules, and `_worker.js` held the server script of an advanced-mode project. When the same folder becomes `assets.directory` of a Worker, each of those files is handled differently, and one of them is dangerous. ## `_headers` and `_redirects` Both are supported natively by Workers static assets, with the syntax Pages used. - **Placement.** Put them at the root of the assets directory, usually by authoring them in the framework's `public/` folder so the build copies them over. - **Not content.** Wrangler always leaves the top-level `_headers`, `_redirects` and `.assetsignore` out of the upload. A request for `/_headers` is a miss like any other path. - **Scope.** Their rules apply to **responses served from static assets**. A `Response` that the Worker script constructs, such as JSON from an API route or a server-rendered page, gets neither the headers nor the redirects, even when its URL matches a rule. Security headers and redirects for Worker routes belong in the script. - **Order.** Redirects run before headers, so a path matching both is redirected. - **Redirect defaults.** A `_redirects` line is `source destination [code]`, with `302` when the code is omitted; a `200` code proxies to another path on the same site. A rule in `_headers` such as `/assets/*` followed by `Cache-Control: public, max-age=31536000, immutable` is the usual way to cache content-hashed bundles for a year, since the default for assets is `public, max-age=0, must-revalidate`. ## `_worker.js`: server code that would go public In Pages, a `_worker.js` file in the output directory switched the project into advanced mode: Pages ran it as code and never served it. Workers has no such convention. The script is whatever `main` points at, and everything in `assets.directory` is a public file. Left in place, `_worker.js` would be downloadable by anyone, along with any internal URLs or logic in it. Wrangler guards against that. When the assets directory contains a top-level `_worker.js` file or directory and there is **no** `.assetsignore`, `wrangler deploy` stops before uploading: ```plaintext ✘ [ERROR] Uploading a Pages _worker.js file as an asset. This could expose your private server-side code to the public Internet. Is this intended? ``` Since Wrangler 4.78.0, `wrangler deploy --dry-run` runs the same check, so CI can catch it before a real deploy. `wrangler dev` does **not**: locally the file is served like any other asset, which is why the problem can survive until the first deploy. There are two fixes: 1. **Move the script out** of the assets directory (recommended) and set `"main"` to its new path. 2. **Keep it in place**, list `_worker.js` in `.assetsignore`, and point `"main"` at `./dist/client/_worker.js`. A project that used a `functions/` folder instead compiles it first with `wrangler pages functions build --outdir=./dist/worker/` and sets `main` to the output. ## `.assetsignore` `.assetsignore` sits at the root of the assets directory and uses the same pattern format as `.gitignore`. Wrangler does not upload any file matching a line in it, and `wrangler dev` does not serve those files either. | File in the Pages output | In a Worker with static assets | | --- | --- | | `_headers` | parsed as rules for asset responses | | `_redirects` | parsed as rules for asset responses | | `_worker.js` | public file unless ignored; deploy refuses it with no `.assetsignore` | | `node_modules`, `.DS_Store`, `.git` | uploaded unless listed in `.assetsignore` | That last row is the second migration trap: Pages skipped those names automatically, Workers uploads whatever the directory holds. A typical file for a migrated project: ```plaintext _worker.js **/node_modules **/.DS_Store **/.git ``` An *empty* `.assetsignore` also silences the `_worker.js` error. That is only correct when you really do want the file public; otherwise it removes the one guard that would have stopped the leak. ## A short migration checklist - Confirm `_headers` and `_redirects` sit at the root of the new `assets.directory`. - Move any header or redirect that Worker routes need into the script. - Take `_worker.js` out of the directory or list it, and set `main`. - Add `.assetsignore` entries for anything Pages used to skip. - Run `wrangler deploy --dry-run` in CI to surface the `_worker.js` check.
- Why is an empty .assetsignore a risky way to make the _worker.js error go away?Wrangler only raises the error when no `.assetsignore` exists; an empty file tells it you have considered the question and want the file uploaded. The deploy then succeeds and publishes the server script as a public asset. Either list `_worker.js` in the file or move the script out of the assets directory.
- An API route in the Worker needs the same CORS header that _headers sets for /api/*. Why does it not get it?`_headers` rules are applied to responses served from static assets. A JSON `Response` built by the script is not one, so the header never appears, including on `OPTIONS` preflight responses. Set CORS headers in the Worker's own responses.
- _headers rules apply to every response whose URL matches, including Worker responses
- Workers runs a _worker.js in the assets directory as the script, as Pages did
- If wrangler dev serves the site correctly, wrangler deploy will upload the same files without complaint
- An empty .assetsignore safely hides the _worker.js warning
- Workers skips node_modules and .git in the assets directory automatically
Assumes Wrangler 4.149.0; the _worker.js check has also run under wrangler deploy --dry-run since Wrangler 4.78.0.