Skip to content

Commit 8a5b3e3

Browse files
authored
Merge pull request #4045 from solidjs/claude/project-thread-pe3qnr
fix(web): bind each server-component site to the frame showing its call (#4042)
2 parents d4217f3 + 788777a commit 8a5b3e3

18 files changed

Lines changed: 258 additions & 21 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
Bind each server-component site to its own frame when the server minted the sites in a different order than the client mounts them (#4042). Document adoption took sites of one server function by ordinal (`id`, `id~2`, …), so a site behind async setup, rendered after its neighbours on the server but mounted first on the client, adopted a neighbour's element, and its first update landed in the wrong frame. A document frame for a call with arguments now carries the call's argument hash (`data-fa`), and a mount takes the unclaimed site showing its own arguments, falling back to the first unclaimed site when none does. Argless calls render byte-identical markup.

‎packages/web/frames/src/client.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,15 +1093,25 @@ function indexBoundaries(root: ParentNode) {
10931093
if (key && isBoundaryId(key) && !boundaryIndex!.has(key)) boundaryIndex!.set(key, el);
10941094
});
10951095
}
1096-
function findBoundaryElement(id: string): Element | undefined {
1096+
function findBoundaryElement(id: string, address?: string): Element | undefined {
10971097
if (!boundaryIndex) {
10981098
boundaryIndex = new Map();
10991099
if (typeof document !== "undefined" && document.body) indexBoundaries(document.body);
11001100
}
11011101
// Later sites of one factory (#3889) are `<id>~n`, probed in mint order.
1102+
// Mint order is the server's, not the client's mount order: a site behind
1103+
// async setup mints after its neighbours on the server but mounts first
1104+
// here (#4042). So a mount that knows its call takes the unclaimed site
1105+
// showing its arguments (`data-fa`), and the first unclaimed site when
1106+
// none does.
1107+
const args = address && address.slice(id.length + 1);
1108+
let first: Element | undefined, el: Element | undefined;
11021109
for (let n = 0; ; n++) {
11031110
const key = n ? id + "~" + (n + 1) : id;
1104-
if (!claimedBoundaries.has(key)) return boundaryIndex.get(key);
1111+
if (claimedBoundaries.has(key)) continue;
1112+
if (!(el = boundaryIndex.get(key)) || !args || (el as HTMLElement).dataset.fa == args)
1113+
return el || first;
1114+
first = first || el;
11051115
}
11061116
}
11071117

@@ -1246,7 +1256,8 @@ function documentBoundary(
12461256
// client entry's installServerComponents() call — re-attempt here, where
12471257
// hydration is necessarily live (idempotent via the $sc flag).
12481258
installRevealHook();
1249-
const el = findBoundaryElement(id);
1259+
const address = binding && contentAddress(binding());
1260+
const el = findBoundaryElement(id, address);
12501261
if (el) return adoptBoundary(host, id, el, props, binding);
12511262
// Not in the page (yet). While the page can still deliver it (the document is
12521263
// streaming, or a deferred fragment is still holding its markup — see
@@ -1276,7 +1287,7 @@ function documentBoundary(
12761287
() =>
12771288
live &&
12781289
runWithOwner(owner, () => {
1279-
const node = findBoundaryElement(id);
1290+
const node = findBoundaryElement(id, address);
12801291
// No element after all (the page ran out of reveals, or another
12811292
// mount took it): mount fresh, exactly as an unwaited miss would.
12821293
return node

‎packages/web/frames/src/frame-sink.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,13 +1916,21 @@ export function createDocumentSlotProps(clientProps, frameId) {
19161916
// client-only). `display:contents` keeps it layout-transparent.
19171917
const FRAME_TAG = "solid-frame";
19181918
const FRAME_ID_ATTR = "data-fid";
1919+
// The argument hash of the call a document boundary shows (#4042), so a
1920+
// client mount can pick its own site when sites were minted in a different
1921+
// order than the client mounts them. Written only for calls with arguments:
1922+
// an argless call's sites all show the same content.
1923+
const FRAME_ARGS_ATTR = "data-fa";
19191924

19201925
/** Open tag for a boundary/region element with `id`. `id` is developer-owned
19211926
* (a server-function id, or a per-site scope of one — see `documentSiteId`),
1922-
* but attribute-escaped defensively. */
1923-
function frameElementOpen(id) {
1927+
* but attribute-escaped defensively. `args` is a document boundary's
1928+
* argument hash (base-36, never needs escaping). */
1929+
function frameElementOpen(id, args?) {
19241930
const escaped = sharedConfig.context ? sharedConfig.context.escape(String(id), true) : String(id);
1925-
return `<${FRAME_TAG} ${FRAME_ID_ATTR}="${escaped}" style="display:contents">`;
1931+
return `<${FRAME_TAG} ${FRAME_ID_ATTR}="${escaped}"${
1932+
args ? ` ${FRAME_ARGS_ATTR}="${args}"` : ""
1933+
} style="display:contents">`;
19261934
}
19271935

19281936
// Per-document site scopes (#3889). One server function mounted twice renders
@@ -2240,13 +2248,14 @@ export function frameTransformDirectResult<T>(
22402248
export function frameTransformDirectResult(value, { id, args, tierUrls }) {
22412249
if (typeof value !== "function") return value;
22422250
const component = value;
2251+
const address = frameAddress(id, args);
22432252
const wrapped = props => {
22442253
// Mint at the call, under the mount's owner, before the context barrier:
22452254
// a second consumption site of this same factory gets its own scope
22462255
// while a re-render of this mount reuses the one it already took.
22472256
const siteId = documentSiteId(id);
22482257
return [
2249-
{ t: frameElementOpen(siteId) },
2258+
{ t: frameElementOpen(siteId, address.slice(id.length + 1)) },
22502259
// Slot props are created OUTSIDE the context barrier: their zone owner
22512260
// (captured at proxy creation) is what client positions re-enter, so
22522261
// the client's content keeps full app context while the component's
@@ -2304,7 +2313,7 @@ export function frameTransformDirectResult(value, { id, args, tierUrls }) {
23042313
// original stays reachable for that producer to render on its own terms,
23052314
// under the address the calling client derived for the same call.
23062315
wrapped[SERVER_COMPONENT_SOURCE] = component;
2307-
wrapped[SERVER_COMPONENT_ADDRESS] = frameAddress(id, args);
2316+
wrapped[SERVER_COMPONENT_ADDRESS] = address;
23082317
return wrapped;
23092318
}
23102319

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "dynamic-component-parity-inline",
3-
"shell": "<main _hk=2><!--$--><section _hk=40 id=\"panel\"><h2>client</h2><!--$--><button _hk=42 id=\"panel-counter\"><!--$-->Panel<!--/-->: <!--$-->0<!--/--></button><!--/--></section><!--/--><!--$--><solid-frame data-fid=\"dynamic-component-parity/note-inline\" style=\"display:contents\"><article id=\"content\"><h1>note v1</h1><ul><!--slot:counter#0:start--><button _hk=sc-dynamic-component-parity/note-inline-counter#0-0 id=\"note-counter\"><!--$-->Count<!--/-->: <!--$-->0<!--/--></button><!--slot:counter#0:end--></ul></article></solid-frame><!--/--></main><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"$B\"]=($R[0]={5000:$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,\"5_fr\":$R[4]=($R[5]=$R[3]()).p});(b=>{for(var k in b)_$HY.r[k]=b[k];delete _$HY.r[\"$B\"]})(_$HY.r[\"$B\"]);($R[7]=(resolver, data) => {\n\tresolver.s(data);\n\tresolver.p.s = 1;\n\tresolver.p.v = data;\n})($R[2],$R[6]=(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(\"dynamic-component-parity/note-inline\",\"dynamic-component-parity/note-inline:1xvb\"));_$HY.r[\"sc:tiers\"]=$R[8]=[];_$HY.r[\"sc:live\"]=$R[9]=($R[10]=(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[11]=($R[12]=() => {\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})());_$HY.r[\"sc:slot:dynamic-component-parity/note-inline:counter#0\"]=$R[13]=($R[14]=$R[3]()).p;$R[7]($R[14],$R[15]={label:\"Count\"});$R[7]($R[5],!0);$R[11].return(void 0);</script>",
3+
"shell": "<main _hk=2><!--$--><section _hk=40 id=\"panel\"><h2>client</h2><!--$--><button _hk=42 id=\"panel-counter\"><!--$-->Panel<!--/-->: <!--$-->0<!--/--></button><!--/--></section><!--/--><!--$--><solid-frame data-fid=\"dynamic-component-parity/note-inline\" data-fa=\"1xvb\" style=\"display:contents\"><article id=\"content\"><h1>note v1</h1><ul><!--slot:counter#0:start--><button _hk=sc-dynamic-component-parity/note-inline-counter#0-0 id=\"note-counter\"><!--$-->Count<!--/-->: <!--$-->0<!--/--></button><!--slot:counter#0:end--></ul></article></solid-frame><!--/--></main><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"$B\"]=($R[0]={5000:$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,\"5_fr\":$R[4]=($R[5]=$R[3]()).p});(b=>{for(var k in b)_$HY.r[k]=b[k];delete _$HY.r[\"$B\"]})(_$HY.r[\"$B\"]);($R[7]=(resolver, data) => {\n\tresolver.s(data);\n\tresolver.p.s = 1;\n\tresolver.p.v = data;\n})($R[2],$R[6]=(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(\"dynamic-component-parity/note-inline\",\"dynamic-component-parity/note-inline:1xvb\"));_$HY.r[\"sc:tiers\"]=$R[8]=[];_$HY.r[\"sc:live\"]=$R[9]=($R[10]=(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[11]=($R[12]=() => {\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})());_$HY.r[\"sc:slot:dynamic-component-parity/note-inline:counter#0\"]=$R[13]=($R[14]=$R[3]()).p;$R[7]($R[14],$R[15]={label:\"Count\"});$R[7]($R[5],!0);$R[11].return(void 0);</script>",
44
"rest": ""
55
}

0 commit comments

Comments
 (0)