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

Fix rendering waterfall of suspended components in a single Suspense boundary #413

Merged
merged 3 commits into from
Jan 19, 2025
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
5 changes: 5 additions & 0 deletions .changeset/long-ties-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix async rendering of multiple suspended components in a single Suspense boundary
17 changes: 2 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,21 +542,8 @@ function _renderToString(
: result;
} catch (e) {
if (!e || typeof e.then != 'function') throw e;

return e.then(() => {
const result = _renderToString(
rendered,
context,
isSvgMode,
selectValue,
vnode,
asyncMode,
renderer
);
return vnode._suspended
? BEGIN_SUSPENSE_DENOMINATOR + result + END_SUSPENSE_DENOMINATOR
: result;
}, renderNestedChildren);

return e.then(renderNestedChildren);
}
};

Expand Down
42 changes: 42 additions & 0 deletions test/compat/async.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,48 @@ describe('Async renderToString', () => {
expect(rendered).to.equal(expected);
});

it('should render JSX with multiple suspended direct children within a single suspense boundary that resolve one-after-another', 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>
<SuspenderTwo>
<li>two</li>
</SuspenderTwo>
<SuspenderThree>
<li>three</li>
</SuspenderThree>
</Suspense>
</ul>
);

const expected = `<ul><!--$s--><li>one</li><!--/$s--><!--$s--><li>two</li><!--/$s--><!--$s--><li>three</li><!--/$s--></ul>`;

suspendedOne.promise.then(() => { void suspendedTwo.resolve();});
suspendedTwo.promise.then(() => { void suspendedThree.resolve();});

suspendedOne.resolve();

const rendered = await promise;

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

it('should rethrow error thrown after suspending', async () => {
const { suspended, getResolved } = createSuspender();

Expand Down
Loading