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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14503-the-404-page-offers-the-closest-real-pag → main 5 commits fork 1 thread · 1 unresolved

  • Merged. scout/14503-the-404-page-offers-the-closest-real-pag was merged into main as eb618ba.

Review

Sign in to review this change.
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.
1212 - **Home:** a "Go home" link always, with "Sign in" beside it when signed out.
1313 - **Search:** one plain GET form to `/search`; for a path shaped `/owner/name` the box
1414 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.
1616 - **The API** stays JSON; a dead `/go/:code` shows no path and no sign-in.
1717
1818 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
8282 const typedPath = ctx.params.name === undefined || MACHINE_PREFIX.test(path) || path.startsWith("/assets/");
8383 const typed = opts.plain || !typedPath ? undefined : typedMiss(ctx.request, path);
8484 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");
8686 const near = get ? nearTo(ctx.app.db, ctx.auth.user, ctx.params.owner ?? "", ctx.params.name ?? "") : [];
8787 const pages = get
8888 ? nearPages(ctx.url.pathname, { signedIn: ctx.auth.user !== undefined, admin: ctx.auth.user?.isAdmin === true })
8989 : [];
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>`;

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.

9899 const term = get ? searchTermFor(ctx.url.pathname) : undefined;
99100 const asked = ctx.url.pathname.length > 120 ? `${ctx.url.pathname.slice(0, 120)}…` : ctx.url.pathname;
100101 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", () => {
4545
4646 test("the 404 page shows the links, escaped, and a far path shows none", async () => {
4747 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>?');
5049 const far = await fetch(`${servers.url}/zzzzzzzzzzzzzzzzzzzz`);
5150 expect(far.status).toBe(404);
52 expect(await far.text()).not.toContain("Try ");
51 expect(await far.text()).not.toContain("Did you mean");
5352 const evil = await (await fetch(`${servers.url}/apps%3Cb%3E`)).text();
5453 expect(evil).not.toContain("<b>");
5554 });
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
7373 expect(res.headers.get("referrer-policy")).toBe("same-origin");
7474 expect(res.headers.get("content-security-policy")).toContain("default-src 'none'");
7575 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs