Cloudflare Workers static assets won't serve a 404 page unless you say so twice
Found this while hunting down broken links: the site had no not-found page at all. Unknown URLs returned a blank Workers response. No navigation, no way back. A dead end for the visitor, and a crawl signal that screams neglect to anything indexing the site.
Fixing it on Astro + Cloudflare Workers takes two changes, and the second one is the one everyone misses.
One: create the page. src/pages/404.astro, styled like the rest of the site, links back to the content. Astro builds it to dist/404.html.
Two: tell the assets layer to use it. In wrangler.json:
{
"assets": {
"directory": "./dist",
"binding": "ASSETS",
"not_found_handling": "404-page"
}
}
Without not_found_handling, the assets binding doesn’t know 404.html is special. It’s just another file that never gets requested. The default behavior hands misses to the Worker, and what comes back depends on your adapter’s mood, not your design.
With it, any path that matches no asset gets 404.html with a proper 404 status. Visitor sees your page; crawler sees the correct code; both leave with somewhere to go.
Quick check that it works after deploy:
curl -s -o /dev/null -w "%{http_code}" https://yoursite.com/definitely-not-a-page
# want: 404, not 200 or 500
A custom 404 page is the cheapest page on the site. One file, one config line. And it’s the one every typo’d URL and stale backlink lands on, so it’s worth the ten minutes.