Skip to content

Commit

Permalink
Publish fixes for streaming renders
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jul 16, 2024
1 parent f7f9d9b commit 852f511
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
24 changes: 24 additions & 0 deletions compat/test/browser/suspense-hydration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ describe('suspense hydration', () => {
});
});

it('should leave DOM untouched when suspending while hydrating', () => {
scratch.innerHTML = '<!-- test --><div>Hello</div>';
clearLog();

const [Lazy, resolve] = createLazy();
hydrate(
<Suspense>
<Lazy />
</Suspense>,
scratch
);
rerender(); // Flush rerender queue to mimic what preact will really do
expect(scratch.innerHTML).to.equal('<!-- test --><div>Hello</div>');
expect(getLog()).to.deep.equal([]);
clearLog();

return resolve(() => <div>Hello</div>).then(() => {
rerender();
expect(scratch.innerHTML).to.equal('<!-- test --><div>Hello</div>');
expect(getLog()).to.deep.equal([]);
clearLog();
});
});

it('should properly attach event listeners when suspending while hydrating', () => {
scratch.innerHTML = '<div>Hello</div><div>World</div>';
clearLog();
Expand Down
8 changes: 5 additions & 3 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,15 @@ export function diff(
newVNode._original = null;
// if hydrating or creating initial tree, bailout preserves DOM:
if (isHydrating || excessDomChildren != null) {
newVNode._dom = oldDom;
newVNode._flags |= isHydrating
? MODE_HYDRATE | MODE_SUSPENDED
: MODE_HYDRATE;

while (oldDom && oldDom.nodeType === 8 && oldDom.nextSibling) {
oldDom = oldDom.nextSibling;
}
excessDomChildren[excessDomChildren.indexOf(oldDom)] = null;
// ^ could possibly be simplified to:
// excessDomChildren.length = 0;
newVNode._dom = oldDom;
} else {
newVNode._dom = oldVNode._dom;
newVNode._children = oldVNode._children;
Expand Down

0 comments on commit 852f511

Please sign in to comment.