deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

Add Custom 404 Page Support

Open mooxl opened this issue 2 years ago • 3 comments
trafficstars

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:

  1. Check for the existence of a 404.html file in the root directory when a request for a non-existent resource is made.
  2. If present, serve this 404.html file to the client in place of the standard 404 status.
  3. If the 404.html file doesn't exist, continue returning the standard "Not Found" status.

mooxl avatar Aug 11 '23 17:08 mooxl

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.

lino-levan avatar Aug 11 '23 18:08 lino-levan

Sounds good! I'll try to get a PR ready this weekend.

mooxl avatar Aug 11 '23 18:08 mooxl

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.

mooxl avatar Aug 12 '23 07:08 mooxl