|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + * |
| 5 | + * #4570: a client write during the hydration window, while a streamed |
| 6 | + * boundary is still out, re-arms a `<Loading on>` nested in it. When the |
| 7 | + * server's answer streams in, the boundary resumes against the server |
| 8 | + * snapshot and catches up to the write. The re-arm's data (`a1`) is also |
| 9 | + * read outside the inner boundary, so its fallback is never shown (D-16): |
| 10 | + * the catch-up holds the whole content on the snapshot until `a1:1` lands |
| 11 | + * (F-2), and no frame shows the inner fallback beside the snapshot's answer |
| 12 | + * (F-1). The inner fallback is never claimed against the server's markup, |
| 13 | + * which never rendered it: no "Hydration key miss", no detached element. |
| 14 | + * Nothing the server answered is asked again. Replays the chunks |
| 15 | + * test/server/hydration-write-rearm-4570.gen.spec.tsx renders. |
| 16 | + */ |
| 17 | +import { afterEach, describe, expect, test, vi } from "vitest"; |
| 18 | +import { readFileSync } from "node:fs"; |
| 19 | +import { dirname, resolve } from "node:path"; |
| 20 | +import { fileURLToPath } from "node:url"; |
| 21 | +import { flush } from "solid-js"; |
| 22 | +import { hydrate } from "@solidjs/web"; |
| 23 | +import { |
| 24 | + createWriteRearmApp, |
| 25 | + writeRearmName, |
| 26 | + writeRearmShapes, |
| 27 | + type WriteRearmShape |
| 28 | +} from "../harness/hydration-write-rearm-4570.jsx"; |
| 29 | +import { bootstrapHydrationGlobal } from "../harness/hydration-bootstrap.js"; |
| 30 | + |
| 31 | +const load = (name: string) => |
| 32 | + JSON.parse( |
| 33 | + readFileSync( |
| 34 | + resolve(dirname(fileURLToPath(import.meta.url)), `../harness/__artifacts__/${name}.json`), |
| 35 | + "utf-8" |
| 36 | + ) |
| 37 | + ) as { shell: string; chunks: string[] }; |
| 38 | + |
| 39 | +// What the screen shows once the server's answer has streamed in (the write |
| 40 | +// already made), and once the client's own question lands. |
| 41 | +// - shared: `a1` is read outside the inner boundary, so the re-arm's |
| 42 | +// fallback is never shown (D-16); the catch-up holds the whole content on |
| 43 | +// the snapshot until `a1:1` lands (F-2, F-1). |
| 44 | +// - own: nothing outside waits on `a2:1`, so the re-arm's fallback shows |
| 45 | +// now (D-15), created fresh: the server never rendered it. |
| 46 | +const expected: Record<WriteRearmShape, { resumed: string; landed: string; asks: string[] }> = { |
| 47 | + shared: { resumed: "La1=10a1=10", landed: "La1=11a1=11", asks: ["a1:1"] }, |
| 48 | + own: { resumed: "F1a1=10", landed: "La2=11a1=10", asks: ["a2:1"] } |
| 49 | +}; |
| 50 | +const lands: Record<WriteRearmShape, string> = { shared: "a1:1", own: "a2:1" }; |
| 51 | + |
| 52 | +const sleep = (ms: number) => new Promise(r => setTimeout(r, ms)); |
| 53 | +async function settle() { |
| 54 | + for (let i = 0; i < 4; i++) { |
| 55 | + await sleep(5); |
| 56 | + flush(); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function applyChunk(container: HTMLElement, chunk: string, first: boolean) { |
| 61 | + const scriptRe = /<script(?:[^>]*)>([\s\S]*?)<\/script>/g; |
| 62 | + const scripts = [...chunk.matchAll(scriptRe)].map(m => m[1]); |
| 63 | + const stripped = chunk.replace(scriptRe, ""); |
| 64 | + if (first) container.innerHTML = stripped; |
| 65 | + else container.insertAdjacentHTML("beforeend", stripped); |
| 66 | + for (const s of scripts) (0, eval)(s); |
| 67 | +} |
| 68 | + |
| 69 | +afterEach(async () => { |
| 70 | + await sleep(0); |
| 71 | + delete (globalThis as any)._$HY; |
| 72 | +}); |
| 73 | + |
| 74 | +describe("#4570: a write in the hydration window re-arms a nested Loading", () => { |
| 75 | + for (const o of writeRearmShapes) { |
| 76 | + test(o, async () => { |
| 77 | + const artifact = load(writeRearmName(o)); |
| 78 | + bootstrapHydrationGlobal(); |
| 79 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 80 | + const error = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 81 | + const container = document.createElement("div"); |
| 82 | + document.body.appendChild(container); |
| 83 | + const { App, write, land, asks } = createWriteRearmApp(o); |
| 84 | + const text = () => container.querySelector("main")!.textContent; |
| 85 | + let dispose: (() => void) | undefined; |
| 86 | + try { |
| 87 | + applyChunk(container, artifact.shell, true); |
| 88 | + dispose = hydrate(() => <App />, container); |
| 89 | + await settle(); |
| 90 | + expect(text()).toBe("F0"); |
| 91 | + |
| 92 | + // The write lands while the server's answer is still out. |
| 93 | + write(1); |
| 94 | + await settle(); |
| 95 | + expect(text()).toBe("F0"); |
| 96 | + |
| 97 | + // The server's answer streams in: the boundary resumes on the |
| 98 | + // snapshot, then catches up to the write. |
| 99 | + for (const c of artifact.chunks) { |
| 100 | + applyChunk(container, c, false); |
| 101 | + await settle(); |
| 102 | + } |
| 103 | + expect(text()).toBe(expected[o].resumed); |
| 104 | + // Every node on screen is in the document: nothing rendered detached. |
| 105 | + for (const p of container.querySelectorAll("main p, main i")) |
| 106 | + expect(p.isConnected).toBe(true); |
| 107 | + |
| 108 | + land(lands[o], 11); |
| 109 | + await settle(); |
| 110 | + expect(text()).toBe(expected[o].landed); |
| 111 | + // Nothing the server answered is asked again. |
| 112 | + expect(asks()).toEqual(expected[o].asks); |
| 113 | + |
| 114 | + const diagnostics = [...warn.mock.calls, ...error.mock.calls].map(c => String(c[0])); |
| 115 | + expect(diagnostics).toEqual([]); |
| 116 | + } finally { |
| 117 | + dispose?.(); |
| 118 | + container.remove(); |
| 119 | + warn.mockRestore(); |
| 120 | + error.mockRestore(); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | +}); |
0 commit comments