Workers Assets 404 Behavior

I was wondering if anyone here has played with with workers assets 404 behaviors? I have a super simple setup with
not_found_handling = "404-page"
not_found_handling = "404-page"
but it won't serve the 404.html i have in the specified directory in assets and instead throws an exception when a page doesn't exist. any ideas?
6 Replies
Chaika
Chaika2w ago
What does your worker look like, are you falling back to the assets binding (return env.ASSETS.fetch(request)), if your worker doesn't match anything? Or rather, what's the exception? Another confusing aspect here is your compat date. If it's greater or equal to April 1st (2025-04-01), there's some new behavior with not found handling. If you're older, then that doesn't apply, and you should follow handing it to the binding
If a Worker is configured and a request does not match a static asset, the Worker will handle the request. The Worker can choose to pass the request to the asset binding (through env.ASSETS.fetch()), following the not_found_handling rules.
https://developers.cloudflare.com/workers/static-assets/
andrew_nyr
andrew_nyrOP2w ago
yeah the worker in this case is
export default {
async fetch(request, env) {
return env.ASSETS.fetch(request);
},
};
export default {
async fetch(request, env) {
return env.ASSETS.fetch(request);
},
};
andrew_nyr
andrew_nyrOP2w ago
and the log shows
No description
andrew_nyr
andrew_nyrOP2w ago
Error 1101 interesting idea, i have it set to compatibility_date = "2025-03-28"
Chaika
Chaika2w ago
hmm, what's your wrangler toml look like? Do you have binding = "ASSETS"?, ex:
[assets]
directory = "./public"
binding= "ASSETS"
not_found_handling = "404-page"
[assets]
directory = "./public"
binding= "ASSETS"
not_found_handling = "404-page"
If you bump it into April, it'll probably just start working because of the new behavior but it's a bit thorny With April compat date: Requests from browsers, identified by req header sec-fetch-mode: navigate, hit the asset worker instead of the user worker even on misses and invoke not found behavior. If your user worker is just falling back, shouldn't matter for your use case and would even save you some worker invocations, but it can break some setups...
andrew_nyr
andrew_nyrOP2w ago
that was my issue :facepalm: didn't have binding= "ASSETS" ty!

Did you find this page helpful?