Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing rendering multiple suspended components within one Suspense boundary #335

Merged
Changes from 1 commit
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
Next Next commit
fixing multiple suspended child components
David Dios committed Feb 19, 2024

Verified

This commit was signed with the committer’s verified signature.
pradyunsg Pradyun Gedam
commit 57a846126d8534c6b3ff907939021ccaace14979
32 changes: 19 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -427,16 +427,20 @@ function _renderToString(
rendered != null && rendered.type === Fragment && rendered.key == null;
rendered = isTopLevelFragment ? rendered.props.children : rendered;

try {
// Recurse into children before invoking the after-diff hook
const str = _renderToString(
const renderChildren = () =>
_renderToString(
rendered,
context,
isSvgMode,
selectValue,
vnode,
asyncMode
);

try {
// Recurse into children before invoking the after-diff hook
const str = renderChildren();
Comment on lines +441 to +453
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth benchmarking in the future. Creating inline closures is likely slower than inlining it into the various places.


if (afterDiff) afterDiff(vnode);
vnode[PARENT] = undefined;

@@ -448,16 +452,18 @@ function _renderToString(

if (!error || typeof error.then !== 'function') throw error;

return error.then(() =>
_renderToString(
rendered,
context,
isSvgMode,
selectValue,
vnode,
asyncMode
)
);
const renderNestedChildren = () => {
try {
return renderChildren();
} catch (e) {
return e.then(
() => renderChildren(),
() => renderNestedChildren()
);
}
};

return error.then(() => renderNestedChildren());
}
}

33 changes: 33 additions & 0 deletions test/compat/async.test.js
Original file line number Diff line number Diff line change
@@ -58,4 +58,37 @@ describe('Async renderToString', () => {

expect(rendered).to.equal(expected);
});

it('should render JSX with multiple suspended direct children within a single suspense boundary', async () => {
const {
Suspender: SuspenderOne,
suspended: suspendedOne
} = createSuspender();
const {
Suspender: SuspenderTwo,
suspended: suspendedTwo
} = createSuspender();

const promise = renderToStringAsync(
<ul>
<Suspense fallback={null}>
<SuspenderOne>
<li>one</li>
</SuspenderOne>
<SuspenderTwo>
<li>two</li>
</SuspenderTwo>
</Suspense>
</ul>
);

const expected = `<ul><li>one</li><li>two</li></ul>`;

suspendedOne.resolve();
suspendedTwo.resolve();

const rendered = await promise;

expect(rendered).to.equal(expected);
});
});