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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

f0x52
Copy link
Contributor

@f0x52 f0x52 commented Jan 16, 2025

I ran into an issue that is very similar to #335, where multiple suspended children in the same Suspense boundary throw a promise that bubbles out of the render functions, resulting in Error: the promise {} was thrown, throw an Error :).

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

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

suspendedOne.resolve();

This only occurs when there are 3+ direct children in a Suspense boundary, rendered async, where the Promises resolve one by one, which is why the test case added by #335 doesn't catch this.
Staggering the Promise resolves makes a key difference: with the existing test, rendering suspends on the Promise thrown in SuspenderOne, after which all three promises are resolved, and then rendering continues: SuspenderTwo and SuspenderThree never throw a promise.

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);

The issue arises here due to the use of .then(onFulfilled, onRejected) instead of .then(onFulfilled).catch(onRejected): when the onFulfilled callback throws an error here, it won't be caught.
But after changing that in the code (which makes all the tests pass) I realised that block could also be simplified to just

const renderNestedChildren = () => {
	try {
		const result = _renderToString(
			rendered,
			context,
			isSvgMode,
			selectValue,
			vnode,
			asyncMode,
			renderer
		);
		return vnode._suspended
			? BEGIN_SUSPENSE_DENOMINATOR + result + END_SUSPENSE_DENOMINATOR
			: result;
	} catch (e) {
		if (!e || typeof e.then != 'function') throw e;
		
		return e.then(renderNestedChildren);
	}
};

return error.then(renderNestedChildren);

Without the additional inline closure.

Copy link

changeset-bot bot commented Jan 16, 2025

🦋 Changeset detected

Latest commit: b87bea9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
preact-render-to-string Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@f0x52
Copy link
Contributor Author

f0x52 commented Jan 16, 2025

It might be good to update the should render JSX with multiple suspended direct children within a single suspense boundary test as well, because right now the second and third children never actually suspend, as their promises are already resolved before they ever get rendered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant