diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index af3d688..d772af7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -132,5 +132,11 @@ jobs: curl -fsS http://localhost:8350/ | grep -q 'Play your Stems back into the Mixer' curl -fsSI http://localhost:8350/ | grep -qi 'content-security-policy' test "$(curl -s -o /dev/null -w '%{http_code}' http://localhost:8350/nope)" = 404 + curl -s http://localhost:8350/nope | grep -q 'Page not found' + # The 404 page asked for by name, and a folder, are a 404 too: not a page, and not a redirect. + for path in /404 /404.html /_not-found /_not-found.html /images /_next; do + test "$(curl -s -o /dev/null -w '%{http_code}' "http://localhost:8350$path")" = 404 + done + test "$(curl -s -o /dev/null -w '%{http_code}' http://localhost:8350/favicon.ico)" = 200 test "$(curl -s -o /dev/null -w '%{http_code}' -H 'Host: www.soundcheckplayer.app' http://localhost:8350/)" = 301 docker rm -f site diff --git a/website/README.md b/website/README.md index b5fb834..b293ce0 100644 --- a/website/README.md +++ b/website/README.md @@ -46,6 +46,7 @@ changes. | What | Where | | --- | --- | | The page | `app/page.tsx` | +| The 404 page (exported as `404.html`; nginx serves it with a 404 status) | `app/not-found.tsx` | | Title, description, theme color | `app/layout.tsx` | | Share image (1200 x 630) and its alt text | `public/og-image.png`, `lib/metadata.ts` | | Styles and tokens (Dark first, light when the system is light) | `app/globals.css` | diff --git a/website/app/favicon.ico b/website/app/favicon.ico new file mode 100644 index 0000000..5448279 Binary files /dev/null and b/website/app/favicon.ico differ diff --git a/website/app/not-found.tsx b/website/app/not-found.tsx new file mode 100644 index 0000000..d40f03e --- /dev/null +++ b/website/app/not-found.tsx @@ -0,0 +1,31 @@ +import type { Metadata } from "next"; +import { SITE_NAME } from "@/lib/site"; + +// Exported as 404.html, which nginx serves with a 404 status for any address that is not a page. Next adds +// itself. No canonical address and no share card: the layout's describe the +// home page, and this page is served at whatever address was asked for. +export const metadata: Metadata = { + title: { absolute: `Page not found - ${SITE_NAME}` }, + description: "There is no page at this address.", + alternates: { canonical: null }, + openGraph: null, + twitter: null, +}; + +export default function NotFound() { + return ( +
+
+
+

Page not found

+

There is no page at this address.

+

+ + Go to the home page + +

+
+
+
+ ); +} diff --git a/website/nginx.conf b/website/nginx.conf index 817e872..ce8382e 100644 --- a/website/nginx.conf +++ b/website/nginx.conf @@ -57,7 +57,21 @@ server { return 301 /$html_query; } + # The 404 page is only ever the answer to a missing address: asked for by name it is a 404 too, not a page. + # "internal" still lets error_page serve it. + location = /404.html { internal; } + location = /404 { return 404; } + location = /_not-found { return 404; } + location = /_not-found.html { return 404; } + + # The home page. It has its own rule because the catch-all below no longer tries folders. + location = / { + try_files /index.html =404; + } + location / { - try_files $uri $uri.html $uri/ =404; + # No "$uri/": a folder such as /images or /_next is not a page. With it, nginx redirected /images to + # /images/, which answered 403. + try_files $uri $uri.html =404; } } diff --git a/website/tests/notFound.test.tsx b/website/tests/notFound.test.tsx new file mode 100644 index 0000000..c3751f1 --- /dev/null +++ b/website/tests/notFound.test.tsx @@ -0,0 +1,55 @@ +import { existsSync, readFileSync } from "node:fs"; +import path from "node:path"; +import { renderToStaticMarkup } from "react-dom/server"; +import NotFound, { metadata } from "@/app/not-found"; + +const ROOT = path.resolve(__dirname, ".."); +const html = renderToStaticMarkup(); +const nginx = readFileSync(path.join(ROOT, "nginx.conf"), "utf8"); + +describe("the not-found page", () => { + it("says the page is missing, in one heading, and links to the home page", () => { + expect(html.match(/

]*>Page not found<\/h1>/); + expect(html).toMatch(/]*href="\/"[^>]*>Go to the home page<\/a>/); + expect(html).toMatch(/]*id="main"/); + }); + + it("has its own title, and no canonical address or share card of the home page", () => { + expect(metadata.title).toEqual({ absolute: "Page not found - SoundcheckPlayer" }); + expect(metadata.alternates).toEqual({ canonical: null }); + expect(metadata.openGraph).toBeNull(); + expect(metadata.twitter).toBeNull(); + }); + + it("uses plain sentences: no exclamation points or em dashes", () => { + expect(html).not.toMatch(/!|—/); + }); +}); + +describe("nginx, for addresses that are not pages", () => { + it("answers /404 and /_not-found with a 404 status, not the 404 page as a normal page", () => { + expect(nginx).toMatch(/location = \/404\s*\{\s*return 404;\s*\}/); + expect(nginx).toMatch(/location = \/_not-found\s*\{\s*return 404;\s*\}/); + expect(nginx).toMatch(/location = \/404\.html\s*\{\s*internal;\s*\}/); + }); + + it("does not try a folder, so /images and /_next are a 404 and not a redirect to a 403", () => { + const catchAll = nginx.match(/location \/ \{[\s\S]*?\}/)?.[0] ?? ""; + expect(catchAll).toMatch(/try_files \$uri \$uri\.html =404;/); + }); + + // Without "$uri/" the catch-all no longer finds the home page (CI's Docker step caught it), so "/" has its own rule. + it("serves the home page from index.html with a rule of its own", () => { + expect(nginx).toMatch(/location = \/ \{\s*try_files \/index\.html =404;\s*\}/); + }); +}); + +describe("the favicon", () => { + it("is also served at /favicon.ico, where crawlers ask for it", () => { + const ico = readFileSync(path.join(ROOT, "app", "favicon.ico")); + expect(existsSync(path.join(ROOT, "app", "favicon.ico"))).toBe(true); + // An .ico file starts 00 00 01 00. + expect([...ico.subarray(0, 4)]).toEqual([0, 0, 1, 0]); + }); +});