Cloudflare Worker redirect never runs, because static assets are served first
I wrote a www → apex redirect in my Worker's fetch handler. It was dead code from the moment I deployed it, and I didn't notice for weeks. Matches if: your Cloudflare Worker isn't running; the fetch handler never fires; a redirect in Worker code doesn't work; console.log in a Worker produces nothing on real pages. The symptom The redirect logic is right there in the Worker. It's deployed. You can…
A Cloudflare Worker redirect never runs because static assets are served first. The redirect logic is present in the Worker and deployed, but it is not called. This is because requests matching a file in the assets directory are served directly at the edge, and the Worker script is not invoked. The redirect only runs for requests that do not resolve to a file, resulting in 404 errors for real pages.
To confirm this, send a request to a non-existent URL and check if the redirect works. The solution is to use host-level redirects in Cloudflare's Redirect Rules, which run earlier in Cloudflare's traffic sequence than Workers. Adding a console.log to the Worker handler doesn't work because it is not being called. This issue is more prevalent when using a wildcard Worker route, which can silently swallow R2 custom domains.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.

