Public: anyone can read this; the crew can change it.
A page that is not found or not yours says where you are and offers home, search and sign-in-and-come-back #12336
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12323-a-page-that-is-not-found-or-not-yours-sa → main 8 commits fork 3 threads · 3 unresolved
2 files
changed +69
−2
src/web/chrome.ts +9 −2
| @@ -75,7 +75,9 @@ export function notFoundPage(ctx: Ctx, opts: { plain?: boolean } = {}): Response | ||
| 75 | 75 | const asked = get ? askedFor(ctx.url.pathname) : ""; |
| 76 | 76 | const search = asked |
| 77 | 77 | ? btn(`Search for ‘${asked}’`, { href: `/search?q=${encodeURIComponent(asked)}` }) |
| 78 | : html``; | |
| 78 | : get | |
| 79 | ? btn("Search", { href: "/search" }) | |
| 80 | : html``; | |
| 79 | 81 | const near = get ? nearTo(ctx.app.db, ctx.auth.user, ctx.params.owner ?? "", ctx.params.name ?? "") : []; |
| 80 | 82 | const didYouMean = |
| 81 | 83 | near.length === 0 |
| @@ -95,7 +97,7 @@ export function notFoundPage(ctx: Ctx, opts: { plain?: boolean } = {}): Response | ||
| 95 | 97 | action: opts.plain |
| 96 | 98 | ? btn("Go home", { href: "/", variant: "primary" }) |
| 97 | 99 | : offerSignIn |
| 98 | ? html`${btn("Sign in", { href: signInHref(ctx), variant: "primary" })} | |
| 100 | ? html`${btn("Sign in", { href: notFoundSignIn(ctx), variant: "primary" })} | |
| 99 | 101 | ${btn("Create an account", { href: withReturn("/signup", returnTo(ctx)) })} ${search} |
| 100 | 102 | ${btn("Go home", { href: "/" })} ${btn("Explore", { href: "/explore" })}` |
| 101 | 103 | : html`${btn("Go home", { href: "/", variant: "primary" })} ${search} |
| @@ -117,6 +119,11 @@ function askedFor(pathname: string): string { | ||
| 117 | 119 | } |
| 118 | 120 | } |
| 119 | 121 | |
Principle 1: the path is already in the URL bar and the page says 'The page you asked for'. Drop the echo (and shownPath) and revert the five test edits that strip it. |
||
| 122 | /** The sign-in door from a 404: back here through `returnTo`; behind an edge, the edge's. */ | |
| 123 | function notFoundSignIn(ctx: Ctx): string { | |
| 124 | return activeEdgeToken(ctx.app) === undefined ? withReturn("/signin", returnTo(ctx)) : signInHref(ctx); | |
| 125 | } | |
| 126 | ||
| 120 | 127 | export const repoNotFound = notFoundPage; |
| 121 | 128 | |
| 122 | 129 | /** Why a refusal happened, in the words a person reads on the 403 page. */ |
test/not-found-way-on.test.ts added +60
| @@ -0,0 +1,60 @@ | ||
| 1 | /** A page that is not found or not yours offers the way on and says nothing about which it is. */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/core/repos.ts"; | |
| 8 | import { createUser } from "../src/core/users.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | ||
| 11 | let dir = ""; | |
| 12 | let app: App, servers: Servers; | |
| 13 | ||
| 14 | beforeAll(async () => { | |
| 15 | dir = await mkdtemp(join(tmpdir(), "forge-404-way-")); | |
| 16 | app = createApp({ dataDir: dir, port: 0 }); | |
| 17 | servers = await serve(app); | |
| 18 | const u = await createUser(app.db, { | |
| 19 | origin: "seed", trusted: true, username: "alice", email: "[email protected]", | |
| 20 | password: "a-good-passphrase", adminIfFirst: true, | |
| 21 | }); | |
| 22 | if (!u.ok) throw new Error("user"); | |
| 23 | const r = await createRepo(app.db, app.reposRoot, { ownerId: u.value.id, name: "secret", visibility: "private" }); | |
| 24 | if (!r.ok) throw new Error("repo"); | |
| 25 | }); | |
| 26 | afterAll(async () => { | |
| 27 | servers.stop(); | |
| 28 | app.close(); | |
| 29 | await rm(dir, { recursive: true, force: true }); | |
| 30 | }); | |
| 31 | ||
| 32 | const get = async (path: string) => { | |
| 33 | const res = await fetch(servers.url + path); | |
| 34 | return { status: res.status, body: await res.text() }; | |
| 35 | }; | |
| 36 | const main = (b: string) => b.slice(b.indexOf("<main"), b.indexOf("</main>")); | |
| 37 | ||
| 38 | test("a private repo and a missing one read the same signed out", async () => { | |
| 39 | const priv = await get("/alice/secret"); | |
| 40 | const miss = await get("/alice/nothere"); | |
| 41 | expect(priv.status).toBe(404); | |
| 42 | expect(miss.status).toBe(404); | |
Replacing the last segment with LAST can hide a real difference between the private and missing bodies; replace only the full path and its encoded form. |
||
| 43 | // Only the full path, its encoded form and the Search button that names the last segment differ. | |
| 44 | const norm = (b: string, p: string) => | |
| 45 | main(b) | |
| 46 | .replaceAll(encodeURIComponent(p), "PATHQ") | |
| 47 | .replaceAll(p, "PATH") | |
| 48 | .replace(/Search for ‘[^’]*’/, "Search for") | |
| 49 | .replace(/\/search\?q=[^"]*/, "/search?q="); | |
| 50 | expect(norm(priv.body, "/alice/secret")).toBe(norm(miss.body, "/alice/nothere")); | |
| 51 | }); | |
| 52 | ||
| 53 | test("the 404 offers home, search and sign-in that comes back", async () => { | |
| 54 | const { body } = await get("/alice/secret"); | |
| 55 | const m = main(body); | |
| 56 | expect(m).toContain('href="/"'); | |
| 57 | expect(m).toContain('href="/search'); | |
| 58 | expect(m).toContain('href="/signin?next=%2Falice%2Fsecret"'); | |
| 59 | expect(m).not.toContain("You asked for"); | |
| 60 | }); |
new above
offline
Principles 1 and 7: a hand-rolled <p class=muted><code> next to the empty state instead of inside it.