From: Ardis Lu Date: Mon, 21 Mar 2022 03:00:45 +0000 (-0700) Subject: Update 404 handling to mirror Cloudflare Pages X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=cdc7c3a057b8e64ccdb9454c8c0af60e6692527c;p=minimal-routing Update 404 handling to mirror Cloudflare Pages --- diff --git a/README.md b/README.md index cf6a9a4..a6c85cd 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@ Demo website for the minimum JavaScript required to implement client-side routin ## Web server configuration -This demo website assumes that the web server has been configured to serve `index.html` on all routes **EXCEPT** for the special path: `/errors/404.html`. For `/errors/404.html`, the web server must respond with a `404 Not Found` error and the file `/errors/404.html`. +This demo website assumes that the web server has been configured to serve `index.html` on all routes **EXCEPT** for the special path: `/errors/NotFound`. For `/errors/NotFound`, the web server must respond with a `404 Not Found` error and the file `/errors/404.html`. -Note: this file path is intentionally nested in a folder because some static site hosts (like [Cloudflare Pages](https://developers.cloudflare.com/pages/platform/serving-pages)) use a top-level `404.html` page as a flag to override the desired behavior of serving `index.html` on all routes. +Note: this file path is intentionally nested in a folder because some static site hosts (like [Cloudflare Pages](https://developers.cloudflare.com/pages/platform/serving-pages)) use a top-level `404.html` page as a flag to override the desired behavior of serving `index.html` on all other routes. The desired behavior can be replicated locally using the npm package [local-web-server](https://www.npmjs.com/package/local-web-server): ``` -ws --spa index.html +ws --spa index.html --rewrite '/errors/(.*) -> /errors/404.html' ``` diff --git a/index.html b/index.html index 450957a..c59c257 100644 --- a/index.html +++ b/index.html @@ -56,7 +56,7 @@ if (!match) { // MUST redirect to a page where the web server serves an actual 404 error (i.e. not just // index.html again) otherwise this triggers an infinite loop - window.location.href = `${window.location.protocol}//${window.location.host}/errors/404.html`; + window.location.href = `${window.location.protocol}//${window.location.host}/errors/NotFound`; } }