404.html Files in Subdirectories Not Used
I have a multilingual Hugo website on Cloudflare Pages. I have a 404.html file at the root and a localized version under each lang subdirectory (ie. /en/404.html, /de/404.html and so forth). The documentation says:
Pages will then attempt to find the closest 404 page. If one is not found in the same directory as the route you are currently requesting, it will continue to look up the directory tree for a matching 404.html file, ending in /404.html.This implies that if the URL is something like
example.com/de/foobar
, it should use the 404.html under the /de/
directory. However, it looks like the 404.html at the root is always used.
Have I misunderstood something, or is this functionality broken?5 Replies
No guesses what might be going on?
Functionality looks fine to me, here's an example project with custom 404s stating which directory they are under:
https://4o4.pages.dev/doesntexist
https://4o4.pages.dev/lang/doesntexist
https://4o4.pages.dev/lang/inner/doesntexist
the asset handler for Pages is open source as well, can see exactly what it does: https://github.com/cloudflare/workers-sdk/blob/5936282bfbda848b465396a70f6334988d1a57a0/packages/pages-shared/asset-server/handler.ts#L707C1-L707C16
GitHub
workers-sdk/packages/pages-shared/asset-server/handler.ts at 593628...
⛅️ Home to Wrangler, the CLI for Cloudflare Workers® - cloudflare/workers-sdk
If you have an advanced mode function (_worker.js), could be messing with it, although Hugo shouldn't have that out of the box. Otherwise would be helpful to show your assets uploaded/pages.dev
Thanks. Looks like the 404 pages are missing in Assets uploaded, but they are generated on my local dev environment when I run Hugo. Curious.
I have them in the layouts folder, and Hugo should (and does on my dev machine) generate the final 404.html files inside the lang folders. So
layouts/404.en.html
should end up in /en/404.html
, but for some reason this doesn't happen when the site is build on Cloudflare.
Okay, problem solved. I added an environment variable HUGO_VERSION
and gave it the value 0.134.1
(latest Hugo version). Now the 404 pages are generated and applied as they should.
@Chaika Thank you for pointing me to the Assets uploaded tab, I hadn't noticed it before.