|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + * |
| 5 | + * A stream cut before its fragments settle (#2958), where each boundary's |
| 6 | + * content creates its own async reads, so the shell carries their records |
| 7 | + * still pending. Each cut fragment renders fresh on the client and adopts |
| 8 | + * what the server sent by id (#4319): `b:fast`'s value, and `c:bad`'s error |
| 9 | + * for the <Errored> in C's content. The reads the server never answered |
| 10 | + * compute: the cut drops their records from the registry before the fresh |
| 11 | + * render reads it, so each slow read asks once and its boundary shows the |
| 12 | + * client's value. The records used to be dropped a macrotask after the |
| 13 | + * fresh render had already adopted their promises, which never settle: the |
| 14 | + * boundaries stayed on their fallbacks and nothing was asked. |
| 15 | + * |
| 16 | + * The complete stream runs first: the ledger's truncation record is module |
| 17 | + * state, and a boundary id it marks stays marked for the rest of the file. |
| 18 | + * |
| 19 | + * Replays the chunks test/server/truncated-fragment-fresh-render.gen.spec.tsx renders. |
| 20 | + */ |
| 21 | +import { afterEach, describe, expect, test, vi } from "vitest"; |
| 22 | +import { readFileSync } from "node:fs"; |
| 23 | +import { dirname, resolve } from "node:path"; |
| 24 | +import { fileURLToPath } from "node:url"; |
| 25 | +import { flush } from "solid-js"; |
| 26 | +import { hydrate } from "@solidjs/web"; |
| 27 | +import { createTruncatedFragmentApp } from "../harness/truncated-fragment-fresh-render.jsx"; |
| 28 | +import { bootstrapHydrationGlobal } from "../harness/hydration-bootstrap.js"; |
| 29 | + |
| 30 | +const { shell, chunks } = JSON.parse( |
| 31 | + readFileSync( |
| 32 | + resolve( |
| 33 | + dirname(fileURLToPath(import.meta.url)), |
| 34 | + "../harness/__artifacts__/truncated-fragment-fresh-render.json" |
| 35 | + ), |
| 36 | + "utf-8" |
| 37 | + ) |
| 38 | +) as { shell: string; chunks: string[] }; |
| 39 | + |
| 40 | +const SERVER_TEXT = "a:slowb:fastb:slowc:bad failedc:slow"; |
| 41 | + |
| 42 | +const sleep = (ms: number) => new Promise(r => setTimeout(r, ms)); |
| 43 | +async function settle() { |
| 44 | + for (let i = 0; i < 4; i++) { |
| 45 | + await sleep(15); |
| 46 | + flush(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function applyChunk(container: HTMLElement, chunk: string, first: boolean) { |
| 51 | + const scriptRe = /<script(?:[^>]*)>([\s\S]*?)<\/script>/g; |
| 52 | + const scripts = [...chunk.matchAll(scriptRe)].map(m => m[1]); |
| 53 | + const stripped = chunk.replace(scriptRe, ""); |
| 54 | + if (first) container.innerHTML = stripped; |
| 55 | + else container.insertAdjacentHTML("beforeend", stripped); |
| 56 | + for (const s of scripts) (0, eval)(s); |
| 57 | +} |
| 58 | + |
| 59 | +// The parser finishing: the document transport's close. |
| 60 | +function endDocument() { |
| 61 | + Object.defineProperty(document, "readyState", { value: "interactive", configurable: true }); |
| 62 | + document.dispatchEvent(new Event("DOMContentLoaded")); |
| 63 | +} |
| 64 | + |
| 65 | +type Page = { container: HTMLElement; asks: () => string[]; hy: any; start: () => void }; |
| 66 | + |
| 67 | +async function withPage(body: (page: Page) => Promise<void>) { |
| 68 | + const hy = bootstrapHydrationGlobal(); |
| 69 | + delete (globalThis as any).$R; |
| 70 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 71 | + const error = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 72 | + const container = document.createElement("div"); |
| 73 | + document.body.appendChild(container); |
| 74 | + // The runtime boots while the document is still streaming: the only |
| 75 | + // window where the truncation sweep arms. |
| 76 | + Object.defineProperty(document, "readyState", { value: "loading", configurable: true }); |
| 77 | + const { App, asks } = createTruncatedFragmentApp(); |
| 78 | + let dispose: (() => void) | undefined; |
| 79 | + try { |
| 80 | + applyChunk(container, shell, true); |
| 81 | + // `body` replays what it needs, then starts hydration. |
| 82 | + await body({ container, asks, hy, start: () => (dispose = hydrate(() => <App />, container)) }); |
| 83 | + const diagnostics = [...warn.mock.calls, ...error.mock.calls].map(c => String(c[0])); |
| 84 | + expect(diagnostics).toEqual([]); |
| 85 | + } finally { |
| 86 | + dispose?.(); |
| 87 | + container.remove(); |
| 88 | + warn.mockRestore(); |
| 89 | + error.mockRestore(); |
| 90 | + delete (document as any).readyState; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +afterEach(async () => { |
| 95 | + await sleep(0); |
| 96 | + delete (globalThis as any)._$HY; |
| 97 | +}); |
| 98 | + |
| 99 | +describe("a fragment's fresh render after a truncated stream", () => { |
| 100 | + test("complete stream: every read claims the server's answer, nothing is asked", () => |
| 101 | + withPage(async page => { |
| 102 | + page.start(); |
| 103 | + await settle(); |
| 104 | + expect(page.container.textContent).toBe("[A][B][C]"); |
| 105 | + for (const chunk of chunks) { |
| 106 | + applyChunk(page.container, chunk, false); |
| 107 | + await settle(); |
| 108 | + } |
| 109 | + endDocument(); |
| 110 | + await settle(); |
| 111 | + expect(page.container.textContent).toBe(SERVER_TEXT); |
| 112 | + expect(page.asks()).toEqual([]); |
| 113 | + expect(page.hy.done).toBe(true); |
| 114 | + })); |
| 115 | + |
| 116 | + test("cut after the first chunk: what the server sent is adopted, the rest computes", () => |
| 117 | + withPage(async page => { |
| 118 | + applyChunk(page.container, chunks[0], false); |
| 119 | + page.start(); |
| 120 | + await settle(); |
| 121 | + expect(page.container.textContent).toBe("[A][B][C]"); |
| 122 | + expect(page.asks()).toEqual([]); |
| 123 | + |
| 124 | + endDocument(); |
| 125 | + await settle(); |
| 126 | + await settle(); |
| 127 | + |
| 128 | + // Each slow read asks once; `b:fast` and `c:bad` are not asked again. |
| 129 | + expect(page.asks()).toEqual(["a:slow", "b:slow", "c:slow"]); |
| 130 | + // The served value and the served error, beside the client's values. |
| 131 | + expect(page.container.textContent).toBe(SERVER_TEXT); |
| 132 | + expect(page.hy.done).toBe(true); |
| 133 | + })); |
| 134 | +}); |
0 commit comments