Public: anyone can read this; the crew can change it.
Asking /explore for a page past the last one goes to the last page, and the address says which page is shown #13861
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13847-asking-explore-for-a-page-past-the-last → main 2 commits fork
2 files
changed +76
−1
src/web/studio/explore.ts +11 −1
| @@ -31,7 +31,7 @@ import { activeEdgeToken, edgeLoginUrl } from "../../http/edge.ts"; | ||
| 31 | 31 | import { offersSignup, pageHead } from "../chrome.ts"; |
| 32 | 32 | import { html, type Html } from "../html.ts"; |
| 33 | 33 | import { compact } from "../compact.ts"; |
| 34 | import { page } from "../layout.ts"; | |
| 34 | import { page, redirect } from "../layout.ts"; | |
| 35 | 35 | import { btn, card, empty, nameMark, row as rowOf, seg, tabs, time } from "../ui.ts"; |
| 36 | 36 | |
| 37 | 37 | const href = (query: ExploreQuery, page = 1): string => { |
| @@ -186,6 +186,16 @@ export async function explorePage(ctx: Ctx): Promise<Response> { | ||
| 186 | 186 | const suggestion = exploreSuggestion(line); |
| 187 | 187 | const ranking = performance.now(); |
| 188 | 188 | const shown = await explorePageAt(ctx.app, ctx.auth.user, query, explorePageFrom((name) => ctx.url.searchParams.get(name))); |
| 189 | // The address names the page shown: a `page` that is not it (past the last, zero, junk, or a | |
| 190 | // redundant 1) answers 303 to the same address with the real page, none for page 1. | |
| 191 | const asked = ctx.url.searchParams.get("page"); | |
| 192 | if (asked !== null && !(shown.page > 1 && asked.trim() === String(shown.page))) { | |
| 193 | const to = new URL(ctx.url); | |
| 194 | if (shown.page > 1) to.searchParams.set("page", String(shown.page)); | |
| 195 | else to.searchParams.delete("page"); | |
| 196 | const text = to.searchParams.toString(); | |
| 197 | return redirect(text === "" ? "/explore" : `/explore?${text}`); | |
| 198 | } | |
| 189 | 199 | const rows = shown.items; |
| 190 | 200 | // Old `?cursor=` links were published; they cannot name a page now, so say so. |
| 191 | 201 | const oldLink = ctx.url.searchParams.has("cursor"); |
test/explore-page-redirect.test.ts added +65
| @@ -0,0 +1,65 @@ | ||
| 1 | /** | |
| 2 | * /explore names the page it shows: a page past the last, zero or junk is a 303 to the real one | |
| 3 | * (#13847). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import { promote } from "./helpers/members.ts"; | |
| 15 | ||
| 16 | let dataDir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | const get = (qs: string) => fetch(`${servers.url}/explore${qs}`, { redirect: "manual" }); | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | dataDir = await mkdtemp(join(tmpdir(), "forge-explore-redirect-")); | |
| 23 | app = createApp({ dataDir, port: 0 }); | |
| 24 | servers = await serve(app); | |
| 25 | const r = await createUser(app.db, { origin: "seed", trusted: true, allowReserved: true, username: "root", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 26 | if (!r.ok) throw new Error(r.error); | |
| 27 | promote(app.db, r.value); | |
| 28 | for (let i = 0; i < 65; i++) { | |
| 29 | const repo = await createRepo(app, r.value, { name: `pg-${i}`, visibility: "public" }); | |
| 30 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 31 | const rec = insertApp(app.db, { repoId: repo.value.id, slug: `pg-${i}`, access: "public" }); | |
| 32 | if (!rec.ok) throw new Error(rec.error); | |
| 33 | } | |
| 34 | }); | |
| 35 | afterAll(async () => { | |
| 36 | servers.stop(); | |
| 37 | await rm(dataDir, { recursive: true, force: true }); | |
| 38 | }); | |
| 39 | ||
| 40 | describe("explore page address", () => { | |
| 41 | test("a real page past the first is still a 200", async () => { | |
| 42 | expect((await get("?page=2")).status).toBe(200); | |
| 43 | }); | |
| 44 | test("a page past the last of two redirects to the last page", async () => { | |
| 45 | const res = await get("?page=999"); | |
| 46 | expect(res.status).toBe(303); | |
| 47 | expect(res.headers.get("location")).toBe("/explore?page=2"); | |
| 48 | }); | |
| 49 | test("a page past the only page redirects to the filters with no page", async () => { | |
| 50 | const res = await get("?kind=app&sort=forks&owner=nobody&page=999"); | |
| 51 | expect(res.status).toBe(303); | |
| 52 | expect(res.headers.get("location")).toBe("/explore?kind=app&sort=forks&owner=nobody"); | |
| 53 | }); | |
| 54 | test("zero, negative and junk redirect to page 1", async () => { | |
| 55 | for (const p of ["0", "-2", "abc", "1.5"]) { | |
| 56 | const res = await get(`?page=${p}`); | |
| 57 | expect(res.status).toBe(303); | |
| 58 | expect(res.headers.get("location")).toBe("/explore"); | |
| 59 | } | |
| 60 | }); | |
| 61 | test("no page and the real page answer 200", async () => { | |
| 62 | expect((await get("")).status).toBe(200); | |
| 63 | expect((await get("?kind=app")).status).toBe(200); | |
| 64 | }); | |
| 65 | }); |
new above
offline