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

The feed's first paint is server-rendered with its first twenty posts in under 50 ms warm, measured in Server-Timing #13675

Merged opened this

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

Merged

scout/forge:scout/13659-the-feed-s-first-paint-is-server-rendere → main 1 commit fork

  • Merged. scout/13659-the-feed-s-first-paint-is-server-rendere was merged into main as 13e6ad2.

Review

Sign in to review this change.
1 file changed +82 −0
test/feed-budget.test.ts added +82
@@ -0,0 +1,82 @@
1 /**
2 * The feed's first paint on a forge of 500 posts (#13659): server-rendered with its first twenty,
3 * its stretches named in Server-Timing, and warm inside 50 ms, with no client fetch for the page.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtempSync, rmSync } from "node:fs";
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 { createUser, type User } from "../src/core/users.ts";
12 import { createSession } from "../src/core/sessions.ts";
13 import { serve, type Servers } from "../src/http/server.ts";
14 import { promote } from "./helpers/members.ts";
15
16 const SEEDED = 500;
17 const BUDGET_MS = 50;
18
19 let root = "";
20 let app: App;
21 let viewer: User;
22 let servers: Servers;
23 let cookie = "";
24
25 beforeAll(async () => {
26 root = mkdtempSync(join(tmpdir(), "forge-feed-budget-"));
27 app = createApp({ dataDir: root, port: 0 });
28 const first = await createUser(app.db, { origin: "seed", trusted: true, username: "root", allowReserved: true, email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
29 if (!first.ok) throw new Error(first.error);
30 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", allowReserved: true, email: "[email protected]", password: "a-good-passphrase", adminIfFirst: false });
31 if (!made.ok) throw new Error(made.error);
32 viewer = made.value;
33 promote(app.db, viewer);
34 const repo = await createRepo(app, viewer, { name: "open", visibility: "public" });
35 if (!repo.ok) throw new Error(JSON.stringify(repo));
36 app.db.transaction(() => {
37 for (let i = 0; i < SEEDED; i++)
38 app.bus.append({ kind: "run.finished", repoId: repo.value.id, actorId: viewer.id, payload: { kind: "agent", state: "ok", summary: `post ${i}`, runId: `r${i}` } });
39 })();
40 servers = await serve(app);
41 cookie = `forge_session=${createSession(app.db, viewer.id).token}`;
42 });
43 afterAll(() => {
44 servers.stop();
45 app.close();
46 rmSync(root, { recursive: true, force: true });
47 });
48
49 async function look(path: string): Promise<{ html: string; timing: string; wall: number }> {
50 const began = performance.now();
51 const res = await fetch(`${servers.url}${path}`, { headers: { cookie } });
52 const html = await res.text();
53 const wall = performance.now() - began;
54 expect(res.status).toBe(200);
55 return { html, timing: res.headers.get("server-timing") ?? "", wall };
56 }
57
58 test("GET /feed names its stretches in Server-Timing", async () => {
59 const { timing } = await look("/feed");
60 const names = timing.split(", ").map((p) => p.split(";")[0]);
61 expect(names).toEqual(expect.arrayContaining(["app", "session", "feed", "render"]));
62 });
63
64 test("the warm first paint holds the budget", async () => {
65 await look("/feed");
66 const runs: number[] = [];
67 for (let i = 0; i < 3; i++) {
68 const { timing } = await look("/feed");
69 runs.push(Number(/(?:^|, )app;dur=([\d.]+)/.exec(timing)![1]));
70 }
71 expect(Math.min(...runs)).toBeLessThan(BUDGET_MS);
72 });
73
74 test("the first twenty posts are in the HTML, and no script fetches them", async () => {
75 const { html } = await look("/feed");
76 const cards = html.match(/data-item=/g) ?? [];
77 expect(cards.length).toBe(20);
78 expect(html).toContain("post ");
79 // The page asks for more only by a plain link, never by a script's fetch of the feed.
80 expect(html).not.toMatch(/fetch\(\s*['"`]\/api\/v1\/feed(?!\/)/);
81 expect(html).toMatch(/href="[^"]*cursor=/);
82 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs