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
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
32 changes: 19 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,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;

Expand All @@ -459,16 +463,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());
}
}

Expand Down
43 changes: 43 additions & 0 deletions test/compat/async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,47 @@ 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 {
Suspender: SuspenderThree,
suspended: suspendedThree
} = createSuspender();

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

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

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

const rendered = await promise;

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