Public: anyone can read this; the crew can change it.

A trailing slash finds the page: /explore/, /apps/, /docs/, /pricing/ answer 308 to the path without it #14286

Merged opened this

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

Merged

scout/forge:scout/14270-a-trailing-slash-finds-the-page-explore → main 3 commits fork

  • Merged. scout/14270-a-trailing-slash-finds-the-page-explore was merged into main as e830f24.

Review

Sign in to review this change.
3 files changed +74 −0
src/http/server.ts +3
@@ -561,6 +561,7 @@ import { takesOutside } from "../actions/contribute_token.ts";
561561 import { reportsOpenSet, contributeOpenSet, contributeTokenRevoke, proposalTokenMint, proposalTokenRevoke } from "../web/pages/proposal-tokens.ts";
562562 import { adminGate } from "./admin_gate.ts";
563563 import { movedResponse } from "./moved.ts";
564 import { slashless } from "./slash.ts";
564565 import { originGate } from "./origin_gate.ts";
565566 import { proposalGate } from "./proposal_gate.ts";
566567 import { contributeGate } from "./contribute_gate.ts";
@@ -3291,6 +3292,8 @@ async function answerGet(
32913292 if (request.method === "GET" && url.pathname === "/looks/failed")
32923293 return failure(app, new Error("looks/failed: thrown on purpose, so the failed page has a picture"), request);
32933294 if (!matched) {
3295 const slashed = isApiPath || url.pathname === "/metrics" || url.pathname === "/health" ? undefined : slashless(router, request.method, url);
3296 if (slashed !== undefined) return slashed;
32943297 // A path under no route at all is what a mistyped or stale studio
32953298 // URL lands on. A browser gets the forge's own page for it, same as
32963299 // any other 404; an API client — anything under `/api/` — keeps the
src/http/slash.ts added +20
@@ -0,0 +1,20 @@
1 /**
2 * A trailing slash finds the page (#14270): a GET or HEAD to `/explore/` whose
3 * slashless form routes answers 308 to it, query kept. Asked only after the path
4 * matched no route, so nothing that routes is moved; a mutation, the API and the
5 * root are never redirected.
6 */
7
8 import type { Router } from "./router.ts";
9
10 export function slashless<H>(router: Router<H>, method: string, url: URL): Response | undefined {
11 if (method !== "GET" && method !== "HEAD") return undefined;
12 const path = url.pathname;
13 if (path.length < 2 || !path.endsWith("/") || path.startsWith("//")) return undefined;
14 if (path === "/api" || path.startsWith("/api/")) return undefined;
15 const bare = path.replace(/\/+$/, "");
16 // Only the forge's own pages: a route that opens with a parameter (`/:owner`) claims any name.
17 const at = bare === "" ? undefined : router.match("GET", bare);
18 if (at === undefined || at.pattern.startsWith("/:")) return undefined;
19 return new Response(null, { status: 308, headers: { location: `${bare}${url.search}` } });
20 }
test/trailing-slash.test.ts added +51
@@ -0,0 +1,51 @@
1 /** A trailing slash finds the page: a GET or HEAD 308s to the slashless path (#14270). */
2 import { afterAll, beforeAll, describe, 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 { expectWork, requestStatements } from "./helpers/work.ts";
8 import { serve, type Servers } from "../src/http/server.ts";
9
10 let dataDir: string;
11 let app: App;
12 let servers: Servers;
13 const send = (path: string, method = "GET") => fetch(`${servers.url}${path}`, { method, redirect: "manual" });
14
15 beforeAll(async () => {
16 dataDir = await mkdtemp(join(tmpdir(), "forge-slash-"));
17 app = createApp({ dataDir, port: 0 });
18 servers = await serve(app);
19 });
20 afterAll(async () => {
21 servers.stop();
22 await rm(dataDir, { recursive: true, force: true });
23 });
24
25 describe("trailing slash", () => {
26 for (const page of ["/explore", "/apps", "/docs", "/pricing", "/status"]) {
27 test(`${page}/ is a 308 to ${page}`, async () => {
28 const bare = await send(page);
29 expect(bare.status).not.toBe(404);
30 const res = await send(`${page}/`);
31 expect(res.status).toBe(308);
32 expect(res.headers.get("location")).toBe(page);
33 expect((await send(`${page}/`, "HEAD")).status).toBe(308);
34 });
35 }
36 test("the query is kept", async () => {
37 const res = await send("/explore/?q=x&page=2");
38 expect(res.status).toBe(308);
39 expect(res.headers.get("location")).toBe("/explore?q=x&page=2");
40 });
41 test("a path whose slashless form routes nowhere stays 404", async () => {
42 expect((await send("/nope/")).status).toBe(404);
43 });
44 test("a mutation and the API are untouched", async () => {
45 expect((await send("/signin/", "POST")).status).not.toBe(308);
46 expect((await send("/api/v1/")).status).not.toBe(308);
47 });
48 test("the redirect runs no SQL", async () => {
49 expectWork(await requestStatements(app.db, `${servers.url}/explore/`, { redirect: "manual" }), { max: 0 });
50 });
51 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs