From 65e24cd710e6e7cceaf3d5d63f77a89d795b7ccc Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Fri, 13 Nov 2026 08:10:08 +0100 Subject: [PATCH 1/3] Scope a boundary's children to its pending streamed range A range the server is still streaming opens with $s? and holds the fallback, a range it gave up on opens with $s!. When the client rendered the content there, hydration adopted the fallback's elements with their attributes, or elements of a later sibling. The boundary's children now only get that range to adopt from and diff the attributes of what they adopt. --- .../test/browser/suspense-hydration.test.jsx | 284 ++++++++++++++++++ src/diff/index.js | 31 ++ 2 files changed, 315 insertions(+) diff --git a/compat/test/browser/suspense-hydration.test.jsx b/compat/test/browser/suspense-hydration.test.jsx index 3e92add74c..5f2327f0f6 100644 --- a/compat/test/browser/suspense-hydration.test.jsx +++ b/compat/test/browser/suspense-hydration.test.jsx @@ -2424,4 +2424,288 @@ describe('suspense hydration owning a streamed range', () => { rerender(); expect(scratch.innerHTML).to.equal('
'); }); + + describe('pending streamed ranges', () => { + // A range whose chunk hasn't arrived opens with `$s?` and still holds the + // fallback, wrapped in ``/`` (comments where the parser has + // no processing instructions). + const pending = [ + ['processing instructions', '', ''], + ['comments', '', ''] + ]; + + pending.forEach(([name, start, end]) => { + it(`should not adopt the fallback for content that rendered first (${name})`, () => { + scratch.innerHTML = `
${start}

loading

${end}
foot
`; + const footer = scratch.querySelector('footer'); + const onClick = vi.fn(); + + hydrate( +
+ loading

}> +

+ content +

+
+
foot
+
, + scratch + ); + rerender(); + + expect(scratch.querySelectorAll('p').length).to.equal(1); + expect(scratch.querySelector('p').outerHTML).to.equal( + '

content

' + ); + expect(scratch.querySelector('footer')).to.equal(footer); + expect(scratch.querySelector('p').nextElementSibling).to.equal(footer); + scratch.querySelector('p').dispatchEvent(createEvent('click')); + expect(onClick).toHaveBeenCalledOnce(); + }); + + it(`should not adopt the fallback when the client resolves before the chunk (${name})`, async () => { + scratch.innerHTML = `
${start}

loading

${end}
foot
`; + const footer = scratch.querySelector('footer'); + const [Lazy, resolve] = createLazy(); + + hydrate( +
+ loading

}> + +
+
foot
+
, + scratch + ); + rerender(); + expect(scratch.querySelector('p').className).to.equal('fb'); + + await resolve(() =>

content

); + rerender(); + + expect(scratch.querySelectorAll('p').length).to.equal(1); + expect(scratch.querySelector('p').outerHTML).to.equal( + '

content

' + ); + expect(scratch.querySelector('p').nextElementSibling).to.equal(footer); + }); + }); + + it('should not adopt fallback text', () => { + scratch.innerHTML = + '
loading
'; + + hydrate( +
+ content +
, + scratch + ); + rerender(); + + expect(scratch.textContent).to.equal('content'); + }); + + it('should still adopt the content of a range that was patched', () => { + scratch.innerHTML = + '

content

'; + const p = scratch.querySelector('p'); + clearLog(); + + hydrate( +
+ loading

}> +

content

+
+
, + scratch + ); + rerender(); + + expect(scratch.querySelector('p')).to.equal(p); + expect(getLog()).to.deep.equal([]); + }); + + it('should mount every node of content that outnumbers the fallback', () => { + scratch.innerHTML = + '

loading

after

'; + + hydrate( +
+ loading

}> +

a

+

b

+
+

after

+
, + scratch + ); + rerender(); + + expect( + [...scratch.querySelectorAll('p')].map(p => p.outerHTML) + ).to.deep.equal([ + '

a

', + '

b

', + '

after

' + ]); + }); + + it('should hydrate the chunk that arrives while the client is suspended', async () => { + scratch.innerHTML = + '

loading

'; + const [Lazy, resolve] = createLazy(); + + hydrate( +
+ loading

}> + +
+
, + scratch + ); + rerender(); + + // what the stream's patch does to the range + const start = scratch.firstChild.firstChild; + start.data = '$s:1'; + while (start.nextSibling.data != '/$s:1') start.nextSibling.remove(); + start.after( + Object.assign(document.createElement('p'), { + className: 'content', + textContent: 'content' + }) + ); + const p = scratch.querySelector('p'); + clearLog(); + + await resolve(() =>

content

); + rerender(); + + expect(scratch.querySelector('p')).to.equal(p); + expect(getLog()).to.deep.equal([]); + }); + + it('should render nested boundaries of a pending range', () => { + scratch.innerHTML = + '

outer

after

'; + + hydrate( +
+ outer

}> +
+ inner

}> +

inner

+
+
+
+

after

+
, + scratch + ); + rerender(); + + expect(scratch.firstChild.innerHTML.replace(//g, '')).to.equal( + '

inner

after

' + ); + }); + + it('should render over the fallback of a range the server gave up on', () => { + scratch.innerHTML = + '

loading

late

'; + const late = scratch.querySelector('#late'); + + hydrate( +
+ loading

}> +

content

+
+

late

+
, + scratch + ); + rerender(); + + expect(scratch.querySelector('#late')).to.equal(late); + expect( + [...scratch.querySelectorAll('p')].map(p => p.outerHTML) + ).to.deep.equal([ + '

content

', + '

late

' + ]); + }); + + it('should render over the fallback of an unstreamed range the server gave up on', () => { + scratch.innerHTML = + '

loading

late

'; + const late = scratch.querySelector('#late'); + + hydrate( +
+ loading

}> +

content

+
+

late

+
, + scratch + ); + rerender(); + + expect(scratch.querySelector('#late')).to.equal(late); + expect( + [...scratch.querySelectorAll('p')].map(p => p.outerHTML) + ).to.deep.equal([ + '

content

', + '

late

' + ]); + }); + + it('should scope each of two adjacent pending ranges', () => { + scratch.innerHTML = + '
one

two

after

'; + const after = scratch.querySelector('#after'); + + hydrate( +
+ one}> +

a

+
+ two

}> +

b1

+

b2

+
+

after

+
, + scratch + ); + rerender(); + + expect(scratch.querySelector('#after')).to.equal(after); + expect(scratch.firstChild.innerHTML).to.equal( + '

a

b1

b2

after

' + ); + }); + + it('should adopt siblings after a pending range', () => { + scratch.innerHTML = + '

loading

after

'; + const after = scratch.querySelector('.after'); + + hydrate( +
+ loading

}> +

content

+
+

after

+
, + scratch + ); + rerender(); + + expect(scratch.querySelector('.after')).to.equal(after); + expect( + [...scratch.querySelectorAll('p')].map(p => p.className) + ).to.deep.equal(['content', 'after']); + }); + }); }); diff --git a/src/diff/index.js b/src/diff/index.js index c3e499ac7e..69cc67fa5d 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -69,6 +69,8 @@ export function diff( /** @type {any} */ let tmp, resumed, + // What follows the range a boundary took over, in an array + rangeNext, newType = newVNode.type; // Only diff VNodes created by Preact. @@ -89,10 +91,34 @@ export function diff( if (tmp.nodeType == 8) { // Re-scan DOM from stored start marker for streamed hydration. oldDom = rangeEnd(tmp, excessDomChildren); + // The client resolved before the server's chunk: this is the fallback. + excessDomChildren._excess = tmp.data[2] != ':' && tmp.data[2]; } else { excessDomChildren.push(tmp); } oldDom = excessDomChildren[0] || oldDom; + } else if ( + isHydrating && + newType && + (tmp = newType.prototype) && + tmp._childDidSuspend + ) { + // A boundary in front of a range that holds a fallback, because the + // server is still streaming it or gave up on it (`$s!`): its children + // only get that range, and diff the attributes of what they adopt. + resumed = [oldDom]; + if ( + (tmp = rangeEnd(oldDom, resumed)) && + oldDom.data[2] != ':' && + oldDom.data[2] + ) { + resumed.push(tmp); + rangeNext = [tmp.nextSibling]; + resumed.some(node => { + excessDomChildren[excessDomChildren.indexOf(node)] = NULL; + }); + (excessDomChildren = resumed)._excess = 1; + } else resumed = NULL; } if ((tmp = options._diff)) tmp(newVNode); @@ -341,6 +367,8 @@ export function diff( // Remove the kept DOM this resumed render didn't claim if (resumed) resumed.some(removeNode); + // The markers the cursor stopped at are gone with it + if (rangeNext) oldDom = rangeNext[0]; if (c._renderCallbacks.length) { commitQueue.push(c); @@ -546,6 +574,9 @@ function diffElementNodes( else if (!namespace) namespace = XHTML_NAMESPACE; if (excessDomChildren) { + // DOM that held a fallback isn't what we render: diff its attributes too + if (excessDomChildren._excess) isHydrating = false; + for (i = 0; i < excessDomChildren.length; i++) { value = excessDomChildren[i]; From b0e6e4949c7f8dfceb16f2e38a56777bd6308c11 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 18 Nov 2026 13:18:19 +0100 Subject: [PATCH 2/3] Golf the pending streamed range scoping Same behaviour, 24 B gzip less in core. - rangeEnd() notes on the list it collects whether the range holds a fallback, so the resume block and the boundary branch don't each spell out the marker test. - rangeEnd() collects a start node that isn't a comment, so the resume block doesn't branch on the node type. - rangeEnd() collects the closing marker too: the boundary branch takes the list as it is, the resume block pops the marker off. - The boundary branch collects into `tmp`, so there is nothing to reset when it leaves the range alone, and keeps what followed the range without wrapping it in an array. --- src/diff/index.js | 71 ++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/src/diff/index.js b/src/diff/index.js index 69cc67fa5d..703522c990 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -68,9 +68,10 @@ export function diff( ) { /** @type {any} */ let tmp, - resumed, - // What follows the range a boundary took over, in an array + // The closing marker of the range a boundary takes over, then what + // followed it rangeNext, + resumed, newType = newVNode.type; // Only diff VNodes created by Preact. @@ -78,8 +79,7 @@ export function diff( // If the previous diff bailed out, resume creating/hydrating into the DOM // that was kept while suspended (server-rendered, or already in the - // container we rendered into). `tmp` holds the stored excess node until the - // options._diff call below. + // container we rendered into). if ( oldVNode._flags & MODE_SUSPENDED && // @ts-expect-error This is MODE_HYDRATE or 0 @@ -87,38 +87,27 @@ export function diff( (tmp = oldVNode._component._excess)) ) { newVNode._flags |= isHydrating; - resumed = excessDomChildren = []; - if (tmp.nodeType == 8) { - // Re-scan DOM from stored start marker for streamed hydration. - oldDom = rangeEnd(tmp, excessDomChildren); - // The client resolved before the server's chunk: this is the fallback. - excessDomChildren._excess = tmp.data[2] != ':' && tmp.data[2]; - } else { - excessDomChildren.push(tmp); - } - oldDom = excessDomChildren[0] || oldDom; + // Re-scan DOM from stored start marker for streamed hydration. + tmp = rangeEnd(tmp, (resumed = excessDomChildren = [])); + oldDom = excessDomChildren[0]; + // The closing marker stays until unmount. + if (tmp) excessDomChildren.pop(); } else if ( isHydrating && newType && (tmp = newType.prototype) && - tmp._childDidSuspend + tmp._childDidSuspend && + (rangeNext = rangeEnd(oldDom, (tmp = [oldDom]))) && + tmp._excess ) { // A boundary in front of a range that holds a fallback, because the // server is still streaming it or gave up on it (`$s!`): its children // only get that range, and diff the attributes of what they adopt. - resumed = [oldDom]; - if ( - (tmp = rangeEnd(oldDom, resumed)) && - oldDom.data[2] != ':' && - oldDom.data[2] - ) { - resumed.push(tmp); - rangeNext = [tmp.nextSibling]; - resumed.some(node => { - excessDomChildren[excessDomChildren.indexOf(node)] = NULL; - }); - (excessDomChildren = resumed)._excess = 1; - } else resumed = NULL; + rangeNext = rangeNext.nextSibling; + tmp.some(node => { + excessDomChildren[excessDomChildren.indexOf(node)] = NULL; + }); + resumed = excessDomChildren = tmp; } if ((tmp = options._diff)) tmp(newVNode); @@ -367,8 +356,8 @@ export function diff( // Remove the kept DOM this resumed render didn't claim if (resumed) resumed.some(removeNode); - // The markers the cursor stopped at are gone with it - if (rangeNext) oldDom = rangeNext[0]; + // The markers the cursor stopped at go with a range a boundary took + if (resumed && rangeNext !== UNDEFINED) oldDom = rangeNext; if (c._renderCallbacks.length) { commitQueue.push(c); @@ -473,20 +462,26 @@ export function diff( /** * Find the closing marker of a streamed range. * @param {PreactElement} start A `$s` marker opening a range - * @param {PreactElement[]} [nodes] Collects the nodes inside the range + * @param {PreactElement[]} [nodes] Collects the nodes after `start` up to and + * including the closing marker, or `start` itself when it isn't a comment. For + * a comment its `_excess` tells whether the range holds a fallback (`$s?`, + * `$s!`). * @returns {PreactElement} The closing marker, or undefined when `start` doesn't * open a range or the range is unclosed */ function rangeEnd(start, nodes) { - if (start && start.nodeType == 8 && start.data.startsWith('$s')) { - for (let depth = 1; (start = start.nextSibling); ) { - if (start.nodeType == 8) { - if (start.data.startsWith('$s')) depth++; - else if (start.data.startsWith('/$s') && !--depth) return start; + if (start && start.nodeType == 8) { + if (nodes) nodes._excess = start.data[2] && start.data[2] != ':'; + if (start.data.startsWith('$s')) { + for (let depth = 1; (start = start.nextSibling); ) { + if (nodes) nodes.push(start); + if (start.nodeType == 8) { + if (start.data.startsWith('$s')) depth++; + else if (start.data.startsWith('/$s') && !--depth) return start; + } } - if (nodes) nodes.push(start); } - } + } else if (nodes) nodes.push(start); } function markAsForce(vnode) { From 63e1c8191aa7aea5ecd0963819e9bdb677c4bf3e Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 18 Nov 2026 12:29:53 +0100 Subject: [PATCH 3/3] Use an attribute that isn't a DOM property in the stale-attribute tests Removing a prop that maps to a DOM property leaves the attribute empty (`id=""`), so the tests compared against markup they can't get. --- compat/test/browser/suspense-hydration.test.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/test/browser/suspense-hydration.test.jsx b/compat/test/browser/suspense-hydration.test.jsx index 5f2327f0f6..24123d71b0 100644 --- a/compat/test/browser/suspense-hydration.test.jsx +++ b/compat/test/browser/suspense-hydration.test.jsx @@ -2436,7 +2436,7 @@ describe('suspense hydration owning a streamed range', () => { pending.forEach(([name, start, end]) => { it(`should not adopt the fallback for content that rendered first (${name})`, () => { - scratch.innerHTML = `
${start}

loading

${end}
foot
`; + scratch.innerHTML = `
${start}

loading

${end}
`; const footer = scratch.querySelector('footer'); const onClick = vi.fn();