Skip to content

Commit 4d31bda

Browse files
committed
fix(web): a falsy primitive fallback (0) re-claims its region too (#4374)
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent f73400c commit 4d31bda

5 files changed

Lines changed: 37 additions & 10 deletions

File tree

‎packages/web/src/client.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,13 +1480,14 @@ export function installHydrationRuntime() {
14801480
// loose text positionally — elements recover through the registry, text
14811481
// only has position. The region is `parent`'s children, or for
14821482
// marker-bounded holes the nodes back to the matching `<!--$-->` start.
1483-
// A sole primitive (text the claim kept raw: a `<Loading>`'s string
1484-
// fallback; only a markerless hole holds one) tracks no node, so it
1485-
// cannot tell a swap happened: the region is re-claimed (#4374).
1483+
// A sole primitive (text the claim kept raw: a `<Loading>`'s string or
1484+
// number fallback, `0` included; only a markerless hole holds one)
1485+
// tracks no node, so it cannot tell a swap happened: the region is
1486+
// re-claimed (#4374).
14861487
reclaimRegion(current, parent, marker) {
1487-
if (!sharedConfig.hydrating || !current || !parent.isConnected) return current;
1488+
if (!sharedConfig.hydrating || current == null || !parent.isConnected) return current;
14881489
const first = Array.isArray(current) ? current[0] : current;
1489-
if (!first || first.isConnected) return current;
1490+
if (first == null || first.isConnected) return current;
14901491
let nodes;
14911492
if (marker) {
14921493
nodes = [];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"shell": "<main _hk=1><template id=\"pl-2\"></template>0<!--pl-2--></main><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"0\"]=$R[0]=($R[1]=($R[2]=() => {\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;_$HY.r[\"2_fr\"]=$R[3]=($R[4]=$R[2]()).p;</script>",
3+
"chunks": [
4+
"<template id=\"2\"><i _hk=2000>sib</i>a1</template><script>($R[5]=(resolver, data) => {\n\tresolver.s(data);\n\tresolver.p.s = 1;\n\tresolver.p.v = data;\n})($R[1],1);$df(\"2\");function $df(e){return _$HY.f?_$HY.f(e):$dfr(e)}function $dfr(e,n,o,t){if(!(n=document.getElementById(e)))return 0;if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dq=_$HY.dq||{})[e]=1,0;for(;o&&(8!==o.nodeType||o.nodeValue!==\"pl-\"+e);)t=o.nextSibling,o.remove(),o=t;t=o.parentNode,(_$HY.v=_$HY.v||{})[e]=n.hasAttribute(\"e\")?[...n.content.childNodes]:1,o.replaceWith(n.content),n.remove(),_$HY.fe(e,t),_$HY.hp&&_$HY.hp[e]&&($dh(_$HY.hp[e]),delete _$HY.hp[e]),$dfd();return 1}function $dfl(e,o,n){if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dlq=_$HY.dlq||{})[e]=1,0;if(o._$fl)return 1;for(n=o.nextSibling;n;){if(8===n.nodeType&&n.nodeValue===\"pl-\"+e){o.parentNode&&o.parentNode.insertBefore(o.content.cloneNode(!0),n),o._$fl=1,$dfd();return 1}n=n.nextSibling}return 0}function $dflj(e,i){for(i=0;i<e.length;i++)$dfl(e[i])}function $dfd(e,i){if(e=_$HY.dq){_$HY.dq=0;for(i in e)$df(i)}if(e=_$HY.dlq){_$HY.dlq=0;for(i in e)$dfl(i)}}function $dfs(e,c,d){(_$HY.sc=_$HY.sc||{})[e]=c,d&&((_$HY.sd=_$HY.sd||{})[e]=1)}function $dfg(e,g,i,k){if(!(g=_$HY.sg&&_$HY.sg[e]))return;for(i=0;i<g.length;i++)if(_$HY.sc&&_$HY.sc[g[i]]>0)return;for(i=0;i<g.length;i++)k=g[i],delete _$HY.sg[k],$df(k)}function $dfc(e){if(--_$HY.sc[e]<=0){delete _$HY.sc[e],_$HY.sg&&_$HY.sg[e]?$dfg(e):!(_$HY.sd&&_$HY.sd[e])&&$df(e);_$HY.sd&&delete _$HY.sd[e]}}function $dfj(e,i,n){for(i=0;i<e.length;i++)if(_$HY.sc&&_$HY.sc[e[i]]>0){for(n=0;n<e.length;n++)(_$HY.sg=_$HY.sg||{})[e[n]]=e;return}for(i=0;i<e.length;i++)$df(e[i])}function $dfe(e,t,k){(t=e.target)&&t.getAttribute&&!t._$dc&&(k=t.getAttribute(\"data-dfc\"))&&(t._$dc=1,$dfc(k))}document.addEventListener(\"load\",$dfe,!0);document.addEventListener(\"error\",$dfe,!0);</script><script>$R[5]($R[4],!0);</script>"
5+
]
6+
}

‎packages/web/test/harness/errored-midstream-4374.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export const nestedErroredName = (o: NestedErroredShape) =>
3030

3131
// The same region with no Errored: the Loading's text fallback, then
3232
// content whose text follows an element. Pins the hole's bookkeeping, not
33-
// the boundaries.
34-
export function createTextAfterElementApp() {
33+
// the boundaries. `fallback` may be a falsy primitive (`0`).
34+
export function createTextAfterElementApp(fallback: string | number = "[LOAD]") {
3535
const [k, setK] = createSignal(1);
3636
let asks = 0;
3737
function App() {
@@ -43,7 +43,7 @@ export function createTextAfterElementApp() {
4343
});
4444
return (
4545
<main>
46-
<Loading fallback="[LOAD]">
46+
<Loading fallback={fallback}>
4747
<i>sib</i>
4848
{"a" + a()}
4949
</Loading>

‎packages/web/test/hydration/errored-midstream-4374.spec.tsx‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ async function run(
7171
app: App,
7272
mode: "loaded" | "streamed",
7373
served: string,
74-
recovered: string
74+
recovered: string,
75+
fallback = "[LOAD]"
7576
) {
7677
const { shell, chunks } = load(name);
7778
bootstrapHydrationGlobal();
@@ -89,7 +90,7 @@ async function run(
8990
dispose = hydrate(() => <App />, container);
9091
await settle();
9192
if (mode === "streamed") {
92-
expect(main().textContent).toBe("[LOAD]");
93+
expect(main().textContent).toBe(fallback);
9394
for (const c of chunks) applyChunk(container, c, false);
9495
// The fragment swapped in: the server's text, before the resume.
9596
serverTexts = texts(main());
@@ -143,4 +144,15 @@ describe("#4374: a string fallback's region, claimed after the swap", () => {
143144
"siba1",
144145
"siba2"
145146
));
147+
// A falsy primitive fallback (`0`) re-claims too.
148+
for (const mode of ["loaded", "streamed"] as const)
149+
test(`a zero fallback: ${mode}`, () =>
150+
run(
151+
"errored-midstream-4374-zero-fallback",
152+
createTextAfterElementApp(0),
153+
mode,
154+
"siba1",
155+
"siba2",
156+
"0"
157+
));
146158
});

‎packages/web/test/server/errored-midstream-4374.gen.spec.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ test("render errored-midstream-4374-text-after-element chunks", async () => {
3434
expect(chunks.join("")).toContain("<i _hk=");
3535
writeArtifact("errored-midstream-4374-text-after-element", { shell, chunks });
3636
});
37+
38+
test("render errored-midstream-4374-zero-fallback chunks", async () => {
39+
const { App } = createTextAfterElementApp(0);
40+
const { shell, chunks } = await recordStream(() => <App />);
41+
expect(shell).toMatch(/>0<!--pl-/);
42+
expect(chunks.join("")).toContain("<i _hk=");
43+
writeArtifact("errored-midstream-4374-zero-fallback", { shell, chunks });
44+
});

0 commit comments

Comments
 (0)