Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
284 changes: 284 additions & 0 deletions compat/test/browser/suspense-hydration.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2424,4 +2424,288 @@ describe('suspense hydration owning a streamed range', () => {
rerender();
expect(scratch.innerHTML).to.equal('<main></main>');
});

describe('pending streamed ranges', () => {
// A range whose chunk hasn't arrived opens with `$s?` and still holds the
// fallback, wrapped in `<?start>`/`<?end>` (comments where the parser has
// no processing instructions).
const pending = [
['processing instructions', '<?start name="1">', '<?end>'],
['comments', '<!--?start name="1"-->', '<!--?end-->']
];

pending.forEach(([name, start, end]) => {
it(`should not adopt the fallback for content that rendered first (${name})`, () => {
scratch.innerHTML = `<div><!--$s?:1-->${start}<p class="fb" data-fallback="1">loading</p>${end}<!--/$s:1--><footer>foot</footer></div>`;
const footer = scratch.querySelector('footer');
const onClick = vi.fn();

hydrate(
<div>
<Suspense fallback={<p class="fb">loading</p>}>
<p class="content" onClick={onClick}>
content
</p>
</Suspense>
<footer>foot</footer>
</div>,
scratch
);
rerender();

expect(scratch.querySelectorAll('p').length).to.equal(1);
expect(scratch.querySelector('p').outerHTML).to.equal(
'<p class="content">content</p>'
);
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 = `<div><!--$s?:1-->${start}<p class="fb">loading</p>${end}<!--/$s:1--><footer>foot</footer></div>`;
const footer = scratch.querySelector('footer');
const [Lazy, resolve] = createLazy();

hydrate(
<div>
<Suspense fallback={<p class="fb">loading</p>}>
<Lazy />
</Suspense>
<footer>foot</footer>
</div>,
scratch
);
rerender();
expect(scratch.querySelector('p').className).to.equal('fb');

await resolve(() => <p class="content">content</p>);
rerender();

expect(scratch.querySelectorAll('p').length).to.equal(1);
expect(scratch.querySelector('p').outerHTML).to.equal(
'<p class="content">content</p>'
);
expect(scratch.querySelector('p').nextElementSibling).to.equal(footer);
});
});

it('should not adopt fallback text', () => {
scratch.innerHTML =
'<div><!--$s?:1--><!--?start name="1"-->loading<!--?end--><!--/$s:1--></div>';

hydrate(
<div>
<Suspense fallback="loading">content</Suspense>
</div>,
scratch
);
rerender();

expect(scratch.textContent).to.equal('content');
});

it('should still adopt the content of a range that was patched', () => {
scratch.innerHTML =
'<div><!--$s:1--><p class="content">content</p><!--/$s:1--></div>';
const p = scratch.querySelector('p');
clearLog();

hydrate(
<div>
<Suspense fallback={<p class="fb">loading</p>}>
<p class="content">content</p>
</Suspense>
</div>,
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 =
'<div><!--$s?:1--><!--?start name="1"--><p class="fb">loading</p><!--?end--><!--/$s:1--><p class="after">after</p></div>';

hydrate(
<div>
<Suspense fallback={<p class="fb">loading</p>}>
<p class="a">a</p>
<p class="b">b</p>
</Suspense>
<p class="after">after</p>
</div>,
scratch
);
rerender();

expect(
[...scratch.querySelectorAll('p')].map(p => p.outerHTML)
).to.deep.equal([
'<p class="a">a</p>',
'<p class="b">b</p>',
'<p class="after">after</p>'
]);
});

it('should hydrate the chunk that arrives while the client is suspended', async () => {
scratch.innerHTML =
'<div><!--$s?:1--><!--?start name="1"--><p class="fb">loading</p><!--?end--><!--/$s:1--></div>';
const [Lazy, resolve] = createLazy();

hydrate(
<div>
<Suspense fallback={<p class="fb">loading</p>}>
<Lazy />
</Suspense>
</div>,
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(() => <p class="content">content</p>);
rerender();

expect(scratch.querySelector('p')).to.equal(p);
expect(getLog()).to.deep.equal([]);
});

it('should render nested boundaries of a pending range', () => {
scratch.innerHTML =
'<div><!--$s?:1--><!--?start name="1"--><p class="fb">outer</p><!--?end--><!--/$s:1--><p class="after">after</p></div>';

hydrate(
<div>
<Suspense fallback={<p class="fb">outer</p>}>
<section>
<Suspense fallback={<p class="fb">inner</p>}>
<p class="inner">inner</p>
</Suspense>
</section>
</Suspense>
<p class="after">after</p>
</div>,
scratch
);
rerender();

expect(scratch.firstChild.innerHTML.replace(/<!--.*?-->/g, '')).to.equal(
'<section><p class="inner">inner</p></section><p class="after">after</p>'
);
});

it('should render over the fallback of a range the server gave up on', () => {
scratch.innerHTML =
'<div><!--$s!:1--><!--?start name="1"--><p class="fb">loading</p><!--?end--><!--/$s:1--><p id="late">late</p></div>';
const late = scratch.querySelector('#late');

hydrate(
<div>
<Suspense fallback={<p class="fb">loading</p>}>
<p class="content">content</p>
</Suspense>
<p id="late">late</p>
</div>,
scratch
);
rerender();

expect(scratch.querySelector('#late')).to.equal(late);
expect(
[...scratch.querySelectorAll('p')].map(p => p.outerHTML)
).to.deep.equal([
'<p class="content">content</p>',
'<p id="late">late</p>'
]);
});

it('should render over the fallback of an unstreamed range the server gave up on', () => {
scratch.innerHTML =
'<div><!--$s!--><p class="fb">loading</p><!--/$s--><p id="late">late</p></div>';
const late = scratch.querySelector('#late');

hydrate(
<div>
<Suspense fallback={<p class="fb">loading</p>}>
<p class="content">content</p>
</Suspense>
<p id="late">late</p>
</div>,
scratch
);
rerender();

expect(scratch.querySelector('#late')).to.equal(late);
expect(
[...scratch.querySelectorAll('p')].map(p => p.outerHTML)
).to.deep.equal([
'<p class="content">content</p>',
'<p id="late">late</p>'
]);
});

it('should scope each of two adjacent pending ranges', () => {
scratch.innerHTML =
'<div><!--$s?:1--><!--?start name="1"--><i>one</i><!--?end--><!--/$s:1--><!--$s?:2--><!--?start name="2"--><p class="fb">two</p><!--?end--><!--/$s:2--><p id="after">after</p></div>';
const after = scratch.querySelector('#after');

hydrate(
<div>
<Suspense fallback={<i>one</i>}>
<p id="a">a</p>
</Suspense>
<Suspense fallback={<p class="fb">two</p>}>
<p id="b1">b1</p>
<p id="b2">b2</p>
</Suspense>
<p id="after">after</p>
</div>,
scratch
);
rerender();

expect(scratch.querySelector('#after')).to.equal(after);
expect(scratch.firstChild.innerHTML).to.equal(
'<p id="a">a</p><p id="b1">b1</p><p id="b2">b2</p><p id="after">after</p>'
);
});

it('should adopt siblings after a pending range', () => {
scratch.innerHTML =
'<div><!--$s?:1--><!--?start name="1"--><p class="fb">loading</p><!--?end--><!--/$s:1--><p class="after">after</p></div>';
const after = scratch.querySelector('.after');

hydrate(
<div>
<Suspense fallback={<p class="fb">loading</p>}>
<p class="content">content</p>
</Suspense>
<p class="after">after</p>
</div>,
scratch
);
rerender();

expect(scratch.querySelector('.after')).to.equal(after);
expect(
[...scratch.querySelectorAll('p')].map(p => p.className)
).to.deep.equal(['content', 'after']);
});
});
});
62 changes: 44 additions & 18 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export function diff(
) {
/** @type {any} */
let tmp,
// The closing marker of the range a boundary takes over, then what
// followed it
rangeNext,
resumed,
newType = newVNode.type;

Expand All @@ -76,23 +79,35 @@ 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
((isHydrating = oldVNode._flags & MODE_HYDRATE),
(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);
} 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 &&
(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.
rangeNext = rangeNext.nextSibling;
tmp.some(node => {
excessDomChildren[excessDomChildren.indexOf(node)] = NULL;
});
resumed = excessDomChildren = tmp;
}

if ((tmp = options._diff)) tmp(newVNode);
Expand Down Expand Up @@ -341,6 +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 go with a range a boundary took
if (resumed && rangeNext !== UNDEFINED) oldDom = rangeNext;

if (c._renderCallbacks.length) {
commitQueue.push(c);
Expand Down Expand Up @@ -445,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) {
Expand Down Expand Up @@ -546,6 +569,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];

Expand Down
Loading