|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + * |
| 5 | + * #4074: the server writes a streamed fragment's reveal (`$df`) and the |
| 6 | + * script that settles its `<id>_fr` record as two scripts. An async entry |
| 7 | + * can start hydrating between them: the fragment is already swapped in |
| 8 | + * (`_$HY.v[id]`) while `_fr` still reads pending. The boundary must not |
| 9 | + * hydrate its fallback, which the reveal already removed (a "Hydration key |
| 10 | + * miss" and a detached element); it adopts the revealed content once `_fr` |
| 11 | + * settles. |
| 12 | + * |
| 13 | + * Replays the real server-rendered chunks of the late-boundary-after-done |
| 14 | + * harness scenario, split between the reveal and the settlement. |
| 15 | + */ |
| 16 | +import { expect, test, vi } from "vitest"; |
| 17 | +import { readFileSync } from "node:fs"; |
| 18 | +import { resolve, dirname } from "node:path"; |
| 19 | +import { fileURLToPath } from "node:url"; |
| 20 | +import { flush } from "solid-js"; |
| 21 | +import { hydrate } from "@solidjs/web"; |
| 22 | +import { scenarios } from "../harness/scenarios.jsx"; |
| 23 | + |
| 24 | +const artifactsDir = resolve(dirname(fileURLToPath(import.meta.url)), "../harness/__artifacts__"); |
| 25 | +const sleep = (ms: number) => new Promise(r => setTimeout(r, ms)); |
| 26 | +const scenario = scenarios.find(s => s.name === "late-boundary-after-done")!; |
| 27 | + |
| 28 | +function applyChunk(container: HTMLDivElement, chunk: string, first: boolean) { |
| 29 | + const scriptRe = /<script(?:[^>]*)>([\s\S]*?)<\/script>/g; |
| 30 | + const scripts = [...chunk.matchAll(scriptRe)].map(m => m[1]); |
| 31 | + const stripped = chunk.replace(scriptRe, ""); |
| 32 | + if (first) container.innerHTML = stripped; |
| 33 | + else container.insertAdjacentHTML("beforeend", stripped); |
| 34 | + return scripts; |
| 35 | +} |
| 36 | + |
| 37 | +test("#4074: a fragment revealed before its _fr settles hydrates the revealed content", async () => { |
| 38 | + const { shell, rest } = JSON.parse( |
| 39 | + readFileSync(resolve(artifactsDir, `${scenario.name}.json`), "utf-8") |
| 40 | + ); |
| 41 | + const container = document.createElement("div"); |
| 42 | + document.body.appendChild(container); |
| 43 | + (globalThis as any)._$HY = { events: [], completed: new WeakSet(), r: {}, fe() {} }; |
| 44 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 45 | + |
| 46 | + for (const s of applyChunk(container, shell, true)) (0, eval)(s); |
| 47 | + const scripts = applyChunk(container, rest, false); |
| 48 | + // The reveal lands; the `_fr` settlement (the last script) has not. |
| 49 | + for (const s of scripts.slice(0, -1)) (0, eval)(s); |
| 50 | + expect(container.querySelector("p")).toBeNull(); |
| 51 | + |
| 52 | + const dispose = hydrate(() => <scenario.App />, container); |
| 53 | + flush(); |
| 54 | + await sleep(10); |
| 55 | + (0, eval)(scripts[scripts.length - 1]); |
| 56 | + await sleep(50); |
| 57 | + flush(); |
| 58 | + await sleep(10); |
| 59 | + flush(); |
| 60 | + |
| 61 | + expect(container.textContent).toBe(scenario.expectedText); |
| 62 | + // Claimed in place: the server node, not a client re-render. |
| 63 | + expect(container.querySelector("section")!.hasAttribute("_hk")).toBe(true); |
| 64 | + const misses = warn.mock.calls.filter( |
| 65 | + (c: unknown[]) => typeof c[0] === "string" && c[0].includes("Hydration key miss") |
| 66 | + ); |
| 67 | + expect(misses).toEqual([]); |
| 68 | + warn.mockRestore(); |
| 69 | + dispose(); |
| 70 | + container.remove(); |
| 71 | +}); |
0 commit comments