deno_std
deno_std copied to clipboard
Add Custom 404 Page Support
Feature Description
Currently, the file server responds with a standard "Not Found" status (404) when a requested resource isn't available. To improve the user experience and provide more informative error handling, I propose supporting custom 404 pages.
Proposed Solution
Add error handling logic to the createServeDirResponse function in the server. The new logic should:
- Check for the existence of a
404.htmlfile in the root directory when a request for a non-existent resource is made. - If present, serve this
404.htmlfile to the client in place of the standard 404 status. - If the
404.htmlfile doesn't exist, continue returning the standard "Not Found" status.
I think we could solve https://github.com/denoland/deno_std/issues/3420 with the same thing. By default, the fallback will be 404.html. We should introduce a --fallback argument. --fallback "index.html" would solve #3420 and --fallback false would disable it.
Sounds good! I'll try to get a PR ready this weekend.
In line with the deno lint command structure, my recommendation is to divide --fallback into -f, --fallback <FILE> and --no-fallback. This is consistent with the arrangement of -c, --config <FILE> and --no-config in deno lint.
Typically the fallback will be called 404.html and is automatically detected; in that case this flag -f, --fallback <FILE> is not necessary.