|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +import { useFixture } from "./fixture"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Dev prerender result cache (#654): /__rsc_prerender memoizes rendered |
| 6 | + * payloads keyed by router-instance identity, so repeated requests between |
| 7 | + * HMR edits skip the module re-import render and getParams work. The |
| 8 | + * `x-rango-prerender-cache` header exposes HIT/MISS for observability. |
| 9 | + * |
| 10 | + * Determinism note: the runtime dev store ALWAYS sends `routeName` |
| 11 | + * (see prerender-api-design.md, Dev Mode), so these requests deliberately |
| 12 | + * OMIT it — the cache key's routeName dimension makes that key unreachable |
| 13 | + * by any page-navigation warming from sibling suites sharing the dev server. |
| 14 | + * |
| 15 | + * The edit-invalidation half of the contract (edit → MISS + fresh content) |
| 16 | + * lives in prerender-hmr.test.ts (local-only, file-watcher territory). |
| 17 | + */ |
| 18 | + |
| 19 | +test.describe("prerender dev cache", () => { |
| 20 | + // isolatedServer: the MISS/HIT sequence needs a dev server whose endpoint |
| 21 | + // cache no other suite (or a sibling checkout's shared-port server) can |
| 22 | + // touch. The fixture spawns a dedicated dev server on a discovered port. |
| 23 | + const f = useFixture({ |
| 24 | + root: "./e2e/test-app", |
| 25 | + mode: "dev", |
| 26 | + isolatedServer: true, |
| 27 | + }); |
| 28 | + |
| 29 | + test("endpoint serves MISS then HIT with a byte-identical body", async ({ |
| 30 | + request, |
| 31 | + }) => { |
| 32 | + const url = f.url("/__rsc_prerender?pathname=/docs"); |
| 33 | + |
| 34 | + // First-ever request for this key on this server: deterministically a |
| 35 | + // MISS regardless of any concurrent invalidation (a churned identity |
| 36 | + // still has no cached body for the key). |
| 37 | + const first = await request.get(url); |
| 38 | + expect(first.status()).toBe(200); |
| 39 | + expect(first.headers()["x-rango-prerender-cache"]).toBe("MISS"); |
| 40 | + const firstBody = await first.text(); |
| 41 | + |
| 42 | + // The payload is the real prerender wire format, not an error shape. |
| 43 | + const parsed = JSON.parse(firstBody); |
| 44 | + expect(Array.isArray(parsed.segments)).toBe(true); |
| 45 | + expect(parsed.segments.length).toBeGreaterThan(0); |
| 46 | + |
| 47 | + // HIT via paired poll: request twice per attempt and require the second |
| 48 | + // response to be a HIT byte-identical to the first. A legitimate |
| 49 | + // identity churn between the pair (dep-optimizer re-optimization, a |
| 50 | + // sibling process touching a watched file) retries the pair; a broken |
| 51 | + // cache never returns HIT and the poll times out. |
| 52 | + await expect |
| 53 | + .poll( |
| 54 | + async () => { |
| 55 | + const a = await request.get(url); |
| 56 | + const aBody = await a.text(); |
| 57 | + const b = await request.get(url); |
| 58 | + const bBody = await b.text(); |
| 59 | + return b.headers()["x-rango-prerender-cache"] === "HIT" && |
| 60 | + bBody === aBody |
| 61 | + ? "HIT-identical" |
| 62 | + : `retry(${b.headers()["x-rango-prerender-cache"]})`; |
| 63 | + }, |
| 64 | + { timeout: 15000 }, |
| 65 | + ) |
| 66 | + .toBe("HIT-identical"); |
| 67 | + }); |
| 68 | + |
| 69 | + test("one render warms the intercept variant key too", async ({ |
| 70 | + request, |
| 71 | + }) => { |
| 72 | + // /docs/getting-started: fresh pathname for this test (still no |
| 73 | + // routeName, so no sibling warming). The main-variant render must warm |
| 74 | + // intercept=1 as well — matchForPrerender computes intercept segments |
| 75 | + // unconditionally, so the second variant costs no extra render. |
| 76 | + // |
| 77 | + // Paired poll: each attempt renders/warms via the main variant, then |
| 78 | + // requests the intercept variant. On the (typical) first attempt an |
| 79 | + // intercept HIT can only come from the main render's warming — the |
| 80 | + // strong claim. Later attempts only occur under a concurrent identity |
| 81 | + // churn; the precise both-keys-from-one-render semantics are pinned |
| 82 | + // deterministically by the dev-prerender-cache unit tests. |
| 83 | + const mainUrl = f.url("/__rsc_prerender?pathname=/docs/getting-started"); |
| 84 | + const interceptUrl = f.url( |
| 85 | + "/__rsc_prerender?pathname=/docs/getting-started&intercept=1", |
| 86 | + ); |
| 87 | + await expect |
| 88 | + .poll( |
| 89 | + async () => { |
| 90 | + const main = await request.get(mainUrl); |
| 91 | + if (main.status() !== 200) return `main-${main.status()}`; |
| 92 | + const intercept = await request.get(interceptUrl); |
| 93 | + if (intercept.status() !== 200) return `int-${intercept.status()}`; |
| 94 | + return intercept.headers()["x-rango-prerender-cache"]; |
| 95 | + }, |
| 96 | + { timeout: 15000 }, |
| 97 | + ) |
| 98 | + .toBe("HIT"); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +test.describe("prerender dev cache absent (production)", () => { |
| 103 | + const f = useFixture({ |
| 104 | + root: "./e2e/test-app", |
| 105 | + mode: "build", |
| 106 | + isolatedServer: true, |
| 107 | + }); |
| 108 | + |
| 109 | + test("prerendered route serves the frozen artifact with no dev-cache header; the dev endpoint does not exist", async ({ |
| 110 | + request, |
| 111 | + }) => { |
| 112 | + const page = await request.get(f.url("/docs")); |
| 113 | + expect(page.status()).toBe(200); |
| 114 | + expect(page.headers()["x-rango-prerender-cache"]).toBeUndefined(); |
| 115 | + expect(await page.text()).toContain("pre-rendered documentation content"); |
| 116 | + |
| 117 | + // Production serves prerendered payloads from the built manifest inside |
| 118 | + // the worker — the dev endpoint must not exist (and must not leak the |
| 119 | + // dev-cache header) in a production serve. |
| 120 | + const endpoint = await request.get( |
| 121 | + f.url("/__rsc_prerender?pathname=/docs"), |
| 122 | + ); |
| 123 | + expect(endpoint.headers()["x-rango-prerender-cache"]).toBeUndefined(); |
| 124 | + expect(endpoint.headers()["content-type"] ?? "").not.toContain( |
| 125 | + "application/json", |
| 126 | + ); |
| 127 | + }); |
| 128 | +}); |
0 commit comments