Public: anyone can read this; the crew can change it.
The 404 page offers the closest real page by name and one next step, in plain words #14526
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14503-the-404-page-offers-the-closest-real-pag → main 5 commits fork 1 thread · 1 unresolved
4 files
changed +29
−13
docs/features/not-found-page.md +1 −1
| @@ -12,7 +12,7 @@ A dead end says what happened in one line and gives a way on. | ||
| 12 | 12 | - **Home:** a "Go home" link always, with "Sign in" beside it when signed out. |
| 13 | 13 | - **Search:** one plain GET form to `/search`; for a path shaped `/owner/name` the box |
| 14 | 14 | starts with that name. |
| 15 | - **Near misses:** pages and repositories you can see with a similar name. | |
| 15 | - **Near misses:** a path one edit from a real page says "Did you mean /explore?"; pages and repositories you can see with a similar name follow; with none, Explore is offered. | |
| 16 | 16 | - **The API** stays JSON; a dead `/go/:code` shows no path and no sign-in. |
| 17 | 17 | |
| 18 | 18 | In the terminal, the palette's "owner/name" prompt answers a miss with the same sentence. |
src/web/chrome.ts +10 −9
| @@ -82,19 +82,20 @@ export function notFoundPage(ctx: Ctx, opts: { plain?: boolean; noEcho?: boolean | ||
| 82 | 82 | const typedPath = ctx.params.name === undefined || MACHINE_PREFIX.test(path) || path.startsWith("/assets/"); |
| 83 | 83 | const typed = opts.plain || !typedPath ? undefined : typedMiss(ctx.request, path); |
| 84 | 84 | if (typed) return typed; |
| 85 | const get = !opts.plain && ctx.request.method === "GET"; | |
| 85 | const get = !opts.plain && (ctx.request.method === "GET" || ctx.request.method === "HEAD"); | |
| 86 | 86 | const near = get ? nearTo(ctx.app.db, ctx.auth.user, ctx.params.owner ?? "", ctx.params.name ?? "") : []; |
| 87 | 87 | const pages = get |
| 88 | 88 | ? nearPages(ctx.url.pathname, { signedIn: ctx.auth.user !== undefined, admin: ctx.auth.user?.isAdmin === true }) |
| 89 | 89 | : []; |
| 90 | const tries = [ | |
| 91 | ...pages.map((p) => html`<a href="${p.href}">${p.name}</a>`), | |
| 92 | ...near.map((r) => html`<a href="${repoBase(r)}">${r.ownerName}/${r.name}</a>`), | |
| 93 | ]; | |
| 94 | const didYouMean = | |
| 95 | tries.length === 0 | |
| 96 | ? html`` | |
| 97 | : html`<p class="muted">Try ${tries.map((t, i) => html`${i > 0 ? ", " : ""}${t}`)}</p>`; | |
| 90 | const tries = near.map((r) => html`<a href="${repoBase(r)}">${r.ownerName}/${r.name}</a>`); | |
| 91 | // The nearest real page by name, in plain words; a path with no near page still gets Explore. | |
| 92 | const didYouMean = !get | |
| 93 | ? html`` | |
| 94 | : pages.length > 0 | |
| 95 | ? html`<p>Did you mean <a href="${pages[0]!.href}">${pages[0]!.href}</a>?${tries.length > 0 ? html` Or ${tries.map((t, i) => html`${i > 0 ? ", " : ""}${t}`)}.` : html``}</p>` | |
| 96 | : tries.length > 0 | |
| 97 | ? html`<p class="muted">Try ${tries.map((t, i) => html`${i > 0 ? ", " : ""}${t}`)}, or <a href="/explore">Explore</a>.</p>` | |
| 98 | : html`<p class="muted">Or look around in <a href="/explore">Explore</a>.</p>`; | |
| 98 | 99 | const term = get ? searchTermFor(ctx.url.pathname) : undefined; |
| 99 | 100 | const asked = ctx.url.pathname.length > 120 ? `${ctx.url.pathname.slice(0, 120)}…` : ctx.url.pathname; |
| 100 | 101 | const find = html`<form method="get" action="/search" role="search" class="row"> |
test/near-pages.test.ts +2 −3
| @@ -45,11 +45,10 @@ test("a page the viewer may not open is never named", () => { | ||
| 45 | 45 | |
| 46 | 46 | test("the 404 page shows the links, escaped, and a far path shows none", async () => { |
| 47 | 47 | const near = await (await fetch(`${servers.url}/explroe`)).text(); |
| 48 | expect(near).toContain('Try'); | |
| 49 | expect(near).toContain('<a href="/explore">Explore</a>'); | |
| 48 | expect(near).toContain('Did you mean <a href="/explore">/explore</a>?'); | |
| 50 | 49 | const far = await fetch(`${servers.url}/zzzzzzzzzzzzzzzzzzzz`); |
| 51 | 50 | expect(far.status).toBe(404); |
| 52 | expect(await far.text()).not.toContain("Try "); | |
| 51 | expect(await far.text()).not.toContain("Did you mean"); | |
| 53 | 52 | const evil = await (await fetch(`${servers.url}/apps%3Cb%3E`)).text(); |
| 54 | 53 | expect(evil).not.toContain("<b>"); |
| 55 | 54 | }); |
test/not-found-page.test.ts +16
| @@ -73,3 +73,19 @@ test("a missing page answers under 3,000 bytes with no shell, no script, and the | ||
| 73 | 73 | expect(res.headers.get("referrer-policy")).toBe("same-origin"); |
| 74 | 74 | expect(res.headers.get("content-security-policy")).toContain("default-src 'none'"); |
| 75 | 75 | }); |
| 76 | ||
| 77 | test("a path one edit from a known page says 'Did you mean /explore'; a far one offers Explore", async () => { | |
| 78 | const near = main(await (await fetch(`${servers.url}/explor`)).text()); | |
| 79 | expect(near).toContain('Did you mean <a href="/explore">/explore</a>?'); | |
| 80 | const far = main(await (await fetch(`${servers.url}/zzqqxx`)).text()); | |
| 81 | expect(far).not.toContain("Did you mean"); | |
| 82 | expect(far).toContain('<a href="/explore">Explore</a>'); | |
| 83 | expect(far).toMatch(/<a\s[^>]*href="\/"[^>]*>Go home</); | |
| 84 | expect(far).not.toMatch(/router|stack|\.ts\b/i); | |
| 85 | }); | |
| 86 | ||
| 87 | test("the echoed path of a near miss stays escaped and out of links", async () => { | |
| 88 | const m = main(await (await fetch(`${servers.url}/explor%3Cb%3E`)).text()); | |
| 89 | expect(m).not.toContain("<b>"); | |
| 90 | expect(m).not.toMatch(/href="[^"]*explor%3C|href="[^"]*<b/); | |
| 91 | }); |
new above
offline
Nested ternary with long inline templates is hard to read; extract a small helper. Also the suggestion renders below the search form; move it up under the headline.