Skip to content

Commit 24d0fb5

Browse files
ryansolidGrok via Cursorcursoragent
authored
test(web): adopt every call of one server function on the page (#3964)
Three calls with different arguments each render their own site. Hydration claims all three in place and does not refetch. This is the docs-page shape from #3932, beside the shared-factory pin from #3889. Co-authored-by: Grok via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent f64fcfe commit 24d0fb5

4 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "multicall-hydration-3932",
3+
"shell": "<solid-frame data-fid=\"multicall-3932\" style=\"display:contents\"><p class=\"block\">one</p></solid-frame><!--!$--><solid-frame data-fid=\"multicall-3932~2\" style=\"display:contents\"><p class=\"block\">two</p></solid-frame><!--!$--><solid-frame data-fid=\"multicall-3932~3\" style=\"display:contents\"><p class=\"block\">three</p></solid-frame><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"$B\"]=($R[0]={\"0_fr\":$R[1]=($R[2]=($R[3]=() => {\n\tconst resolver = {\n\t\tp: 0,\n\t\ts: 0,\n\t\tf: 0\n\t};\n\tresolver.p = new Promise((resolve, reject) => {\n\t\tresolver.s = resolve;\n\t\tresolver.f = reject;\n\t});\n\treturn resolver;\n})()).p,\"0001\":$R[4]=($R[5]=$R[3]()).p,\"0004\":$R[6]=($R[7]=$R[3]()).p,\"0007\":$R[8]=($R[9]=$R[3]()).p});(b=>{for(var k in b)_$HY.r[k]=b[k];delete _$HY.r[\"$B\"]})(_$HY.r[\"$B\"]);($R[11]=(resolver, data) => {\n\tresolver.s(data);\n\tresolver.p.s = 1;\n\tresolver.p.v = data;\n})($R[5],$R[10]=(self._$SC||(self._$SC={c:{},a:{},b:{},r(i,a){var c=this.c[i]||(this.c[i]=(p,b)=>self._$SC.impl(i,p,b));if(!a)return c;this.a[a]=i;this.reg&&this.reg(a,i);return this.b[a]||(this.b[a]=Object.assign(p=>c(p,()=>a),{[Symbol.for(\"solid.component-binding\")]:{component:c,address:a}}))}})).r(\"multicall-3932\",\"multicall-3932:1g2hle\"));$R[11]($R[7],$R[12]=(self._$SC||(self._$SC={c:{},a:{},b:{},r(i,a){var c=this.c[i]||(this.c[i]=(p,b)=>self._$SC.impl(i,p,b));if(!a)return c;this.a[a]=i;this.reg&&this.reg(a,i);return this.b[a]||(this.b[a]=Object.assign(p=>c(p,()=>a),{[Symbol.for(\"solid.component-binding\")]:{component:c,address:a}}))}})).r(\"multicall-3932\",\"multicall-3932:1g5vfw\"));$R[11]($R[9],$R[13]=(self._$SC||(self._$SC={c:{},a:{},b:{},r(i,a){var c=this.c[i]||(this.c[i]=(p,b)=>self._$SC.impl(i,p,b));if(!a)return c;this.a[a]=i;this.reg&&this.reg(a,i);return this.b[a]||(this.b[a]=Object.assign(p=>c(p,()=>a),{[Symbol.for(\"solid.component-binding\")]:{component:c,address:a}}))}})).r(\"multicall-3932\",\"multicall-3932:16ngi3e\"));_$HY.r[\"sc:live\"]=$R[14]=($R[15]=(stream) => new ReadableStream({ start(controller) {\n\tstream.on({\n\t\tnext(value) {\n\t\t\ttry {\n\t\t\t\tcontroller.enqueue(value);\n\t\t\t} catch (_error) {}\n\t\t},\n\t\tthrow(value) {\n\t\t\tcontroller.error(value);\n\t\t},\n\t\treturn() {\n\t\t\ttry {\n\t\t\t\tcontroller.close();\n\t\t\t} catch (_error) {}\n\t\t}\n\t});\n} }))($R[16]=($R[17]=() => {\n\tconst buffer = [];\n\tconst listeners = [];\n\tlet alive = true;\n\tlet success = false;\n\tlet count = 0;\n\tconst internal = {\n\t\tflush(value, mode, x) {\n\t\t\tfor (x = 0; x < count; x++) {\n\t\t\t\tconst listener = listeners[x];\n\t\t\t\tif (listener) listener[mode](value);\n\t\t\t}\n\t\t},\n\t\tup(listener, x, z, current) {\n\t\t\tfor (x = 0, z = buffer.length; x < z; x++) {\n\t\t\t\tcurrent = buffer[x];\n\t\t\t\tif (!alive && x === z - 1) listener[success ? \"return\" : \"throw\"](current);\n\t\t\t\telse listener.next(current);\n\t\t\t}\n\t\t},\n\t\ton(listener, temp = 0) {\n\t\t\tlet subscribed = alive;\n\t\t\tif (alive) {\n\t\t\t\tfor (temp = 0; temp < count; temp++) if (!listeners[temp]) break;\n\t\t\t\tif (temp === count) count++;\n\t\t\t\tlisteners[temp] = listener;\n\t\t\t}\n\t\t\tinternal.up(listener);\n\t\t\treturn () => {\n\t\t\t\tif (alive && subscribed) {\n\t\t\t\t\tsubscribed = false;\n\t\t\t\t\tlisteners[temp] = void 0;\n\t\t\t\t\twhile (count > 0 && !listeners[count - 1]) count--;\n\t\t\t\t\tlisteners.length = count;\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t};\n\treturn {\n\t\t__SEROVAL_STREAM__: true,\n\t\ton(listener) {\n\t\t\treturn internal.on(listener);\n\t\t},\n\t\tnext(value) {\n\t\t\tif (alive) {\n\t\t\t\tbuffer.push(value);\n\t\t\t\tinternal.flush(value, \"next\");\n\t\t\t}\n\t\t},\n\t\tthrow(value) {\n\t\t\tif (alive) {\n\t\t\t\tbuffer.push(value);\n\t\t\t\tinternal.flush(value, \"throw\");\n\t\t\t\talive = false;\n\t\t\t\tsuccess = false;\n\t\t\t\tlisteners.length = 0;\n\t\t\t}\n\t\t},\n\t\treturn(value) {\n\t\t\tif (alive) {\n\t\t\t\tbuffer.push(value);\n\t\t\t\tinternal.flush(value, \"return\");\n\t\t\t\talive = false;\n\t\t\t\tsuccess = true;\n\t\t\t\tlisteners.length = 0;\n\t\t\t}\n\t\t}\n\t};\n})());$R[11]($R[2],!0);$R[16].return(void 0);</script>",
4+
"rest": ""
5+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
*
4+
* #3932: one server function called at three sites with different arguments,
5+
* the docs-page shape (a highlighted block per call). Each call returns its
6+
* own component. Hydration must adopt every site; the later calls must not
7+
* refetch.
8+
*
9+
* Shared by test/server/multicall-hydration-3932.spec.tsx (ssr compile, an
10+
* in-process answer per call) and test/hydration/multicall-hydration-3932.spec.tsx
11+
* (dom compile, the same tree over the server reference).
12+
*/
13+
import { Loading } from "solid-js";
14+
import { dynamic } from "@solidjs/web";
15+
16+
export const FID = "multicall-3932";
17+
export const ARTIFACT = "multicall-hydration-3932";
18+
export const TEXTS = ["one", "two", "three"] as const;
19+
20+
/**
21+
* The reporter's tree: each block calls the same function with its own
22+
* text. `render` is the server's in-process answer on one side and the
23+
* client reference on the other.
24+
*/
25+
export function makeApp(render: (text: string) => any) {
26+
function Block(props: { text: string }) {
27+
const Content = dynamic(() => render(props.text));
28+
return <Content />;
29+
}
30+
return function App() {
31+
return (
32+
<Loading fallback="loading">
33+
<Block text="one" />
34+
<Block text="two" />
35+
<Block text="three" />
36+
</Loading>
37+
);
38+
};
39+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*
5+
* #3932: hydrating three calls of one server function, each with different
6+
* arguments, must adopt every block. The server rendered all three; the
7+
* later calls must not go back to the network.
8+
*
9+
* One page per file: the frames client's boundary index is module state.
10+
*/
11+
import { expect, test, vi } from "vitest";
12+
import { flush } from "solid-js";
13+
import { hydrate } from "@solidjs/web";
14+
import { installServerComponents } from "../../frames/src/client.js";
15+
import { createServerReference } from "../../server-functions/src/client.js";
16+
import { makeHost } from "../lifecycle-matrix/harness.js";
17+
import { applyChunk, drain } from "./frame-live-document-helpers.js";
18+
import { ARTIFACT, FID, makeApp, TEXTS } from "../harness/multicall-hydration-3932.jsx";
19+
import { existsSync, readFileSync } from "node:fs";
20+
import { resolve, dirname } from "node:path";
21+
import { fileURLToPath } from "node:url";
22+
23+
const artifactsDir = resolve(dirname(fileURLToPath(import.meta.url)), "../harness/__artifacts__");
24+
25+
function loadArtifact(): { shell: string; rest: string } {
26+
const file = resolve(artifactsDir, `${ARTIFACT}.json`);
27+
if (!existsSync(file))
28+
throw new Error(
29+
`Missing artifact "${ARTIFACT}" — run the server spec first: ` +
30+
`vitest run --config vite.config.server.mjs test/server/multicall-hydration-3932.spec.tsx`
31+
);
32+
return JSON.parse(readFileSync(file, "utf-8"));
33+
}
34+
35+
test("three calls with different arguments adopt in place and do not refetch (#3932)", async () => {
36+
const { shell, rest } = loadArtifact();
37+
(globalThis as any)._$HY = { events: [], completed: new WeakSet(), r: {}, fe() {} };
38+
const container = document.createElement("div");
39+
document.body.appendChild(container);
40+
const { host } = makeHost();
41+
installServerComponents(host);
42+
43+
const urls: string[] = [];
44+
vi.stubGlobal("fetch", async (input: any) => {
45+
urls.push(typeof input === "string" ? input : input.url);
46+
throw new Error("unexpected fetch");
47+
});
48+
const warnings: string[] = [];
49+
vi.spyOn(console, "warn").mockImplementation((...args: unknown[]) => {
50+
warnings.push(args.map(String).join(" "));
51+
});
52+
const errors: string[] = [];
53+
vi.spyOn(console, "error").mockImplementation((...args: unknown[]) => {
54+
errors.push(args.map(String).join(" "));
55+
});
56+
57+
const render = createServerReference(FID);
58+
const App = makeApp((text: string) => render(text));
59+
60+
applyChunk(container, shell, true);
61+
if (rest) applyChunk(container, rest, false);
62+
63+
const before = [...container.querySelectorAll("p.block")];
64+
expect(before.map(el => el.textContent)).toEqual([...TEXTS]);
65+
66+
const dispose = hydrate(() => <App />, container);
67+
flush();
68+
await drain();
69+
70+
const blocks = [...container.querySelectorAll("p.block")];
71+
expect(blocks.length, `${container.innerHTML}\n${warnings.join("\n")}\n${urls.join("\n")}`).toBe(
72+
3
73+
);
74+
expect(blocks.map(el => el.textContent)).toEqual([...TEXTS]);
75+
expect(blocks[0], "first block claimed in place").toBe(before[0]);
76+
expect(blocks[1], "second block claimed in place").toBe(before[1]);
77+
expect(blocks[2], "third block claimed in place").toBe(before[2]);
78+
expect(urls, "later calls must not refetch").toEqual([]);
79+
expect(warnings.filter(w => /hydration|unclaimed|key miss/i.test(w))).toEqual([]);
80+
expect(errors).toEqual([]);
81+
82+
dispose();
83+
container.remove();
84+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
*
4+
* Server half of #3932. Three calls of one server function, each with its
5+
* own argument, each answering with its own component. Writes the artifact
6+
* the hydration spec replays.
7+
*/
8+
import { describe, expect, test } from "vitest";
9+
import { frameTransformDirectResult, ServerComponentPlugin } from "../../frames/src/frame-sink.js";
10+
import { ARTIFACT, FID, TEXTS, makeApp } from "../harness/multicall-hydration-3932.jsx";
11+
import { recordStream, writeArtifact } from "./artifact-recorder.js";
12+
13+
const visibleText = (html: string) =>
14+
html.replace(/<script[\s\S]*?<\/script>/g, "").replace(/<[^>]*>/g, "");
15+
16+
describe("three calls of one server function (#3932) — server render", () => {
17+
test("each argument renders its own block, under its own site id", async () => {
18+
const render = (text: string) =>
19+
Promise.resolve(
20+
frameTransformDirectResult(() => <p class="block">{text}</p>, { id: FID, args: [text] })
21+
);
22+
const App = makeApp(render);
23+
const { shell, rest } = await recordStream(() => <App />, {
24+
plugins: [ServerComponentPlugin]
25+
} as any);
26+
const full = shell + rest;
27+
const visible = visibleText(full);
28+
29+
expect(visible).not.toContain("loading");
30+
for (const text of TEXTS) expect(visible).toContain(text);
31+
expect(full.match(/class="block"/g)?.length).toBe(3);
32+
expect(full.match(/<solid-frame\b/g)?.length).toBe(3);
33+
// The first site keeps the function id. Each later call stamps its own.
34+
expect(full).toContain(`data-fid="${FID}"`);
35+
expect(full).toContain(`data-fid="${FID}~2"`);
36+
expect(full).toContain(`data-fid="${FID}~3"`);
37+
38+
writeArtifact(ARTIFACT, { name: ARTIFACT, shell, rest });
39+
});
40+
});

0 commit comments

Comments
 (0)