Skip to main content

Cloudflare Worker Redirect Never Runs Because Static Assets Are Served First

A developer shares why a redirect script inside a Worker sat dead for weeks due to Cloudflare serving static assets at the edge first.

AI-written
Inewgen
05 Aug 2026Source: Dev.to3 min read (0 views)
Share
Cloudflare Worker Redirect Never Runs Because Static Assets Are Served First

Stock photo for illustration only, not from the actual event

Font size
  • Redirect logic inside a Worker fetch handler failed to run because Static Assets were served directly at the edge.
  • Real page requests matched underlying files, completely bypassing the Worker script except for 404s.
  • The proper fix is utilizing Cloudflare Redirect Rules instead of handling host-level redirects in Worker code.
  • Forcing Workers to run before asset lookups for simple redirects creates unnecessary performance overhead.

A software developer recently highlighted a frustrating architectural pitfall after writing a www-to-apex redirect inside a Cloudflare Worker's fetch handler. The code sat as dead code from the moment it was deployed, remaining unnoticed for weeks. Even though the redirect logic was clearly visible in the dashboard, browsing to the site's pages served the content directly instead of triggering a redirect.

Adding a console.log statement revealed that the handler was never being called. The script was neither slow nor failing—it was simply being bypassed entirely. Under Cloudflare Workers static assets, requests matching a file in the assets directory are served directly at the edge, meaning the Worker script is never invoked at all.

This default behavior is optimized for speed and cost-efficiency, ensuring static requests are blazing fast and do not incur Worker invocation fees. However, it fundamentally changes how developers should view their code: your Worker acts as a fallback rather than the front door. Consequently, fetch handlers only execute for requests that fail to resolve to an actual file, meaning the redirect only caught traffic heading to non-existent 404 pages.

server architecture network routing diagram

Stock photo for illustration only, not from the actual event

Never miss the latest news?

Subscribe to get news summaries by email - not often enough to be annoying.

โฆษณา

Developers can confirm this behavior using curl. Running curl -sI against the root domain returns a 200 status code instead of a 301 or 302 redirect. Testing a nonexistent path that triggers a redirect confirms that custom code only executes when an asset lookup misses.

Host-level redirects do not belong inside Worker code. Instead, developers should deploy a Redirect Rule via the Cloudflare dashboard (Rules -> Redirect Rules), which executes earlier in the traffic sequence than Workers. Setting a rule where the hostname equals the www variant and applying a dynamic 301 redirect preserves query strings while running for free on every plan.

While an alternative configuration exists to force Workers to run before asset lookups on every request—useful for auth checks or header rewriting—it is the wrong trade for a simple redirect. Forcing a Worker invocation on every single request just to route a small fraction of visitors introduces unnecessary overhead when a native Redirect Rule handles it instantly.

Source: Dev.to

Comments

Leave a Comment
0/2000

Found something wrong in this article? Report an issue with this article