fix: renderToStringAsync produces commas for suspended components with complex children#457
Merged
JoviDeCroock merged 2 commits intopreactjs:mainfrom Mar 28, 2026
Conversation
…with markers `renderToStringAsync` corrupts HTML output when a suspended component's children produce an Array or Promise result from `_renderToString`. The suspense marker wrapping code used string concatenation (`+`), which calls `Array.toString()` on arrays (producing commas) and `[object Promise]` on unresolved Promises. This manifests with multi-level cascading suspensions: component A suspends, and after resolving, its render tree contains components that also suspend on independent Promises at different times. The `.then()` handler wrapping A's output receives an Array (from inner elements containing pending Promises) instead of a string. Extract a `wrapWithSuspenseMarkers()` helper that handles all three return types (string, Array, Promise) and use it at both affected call sites: the try-path `.then()` handler and the catch-path `renderNestedChildren`. Fixes corrupted SSR output like ",,,,,[object Promise]," in apps using multi-level suspensions for data fetching.
🦋 Changeset detectedLatest commit: ce6ef71 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix:
renderToStringAsyncproduces commas and[object Promise]for suspended components with complex childrenProblem
When using
renderToStringAsyncwith<Suspense>boundaries, certain component trees produce corrupted HTML output containing commas and[object Promise]literals. For example, rendered output may contain:instead of the expected HTML.
Root cause
Internally,
_renderToStringcan return three types of values:string— when all children resolve to plain textArray— when children contain a mix of strings and pending Promises (e.g. nested elements with further suspensions)Promise— when a child threw a Promise (suspension)The suspense marker wrapping code at two locations assumed the result was always a string and used
+concatenation to wrap it with<!--$s-->/<!--/$s-->markers. When the result is anArray, JavaScript's+operator callsArray.prototype.toString(), which joins elements with commas — injecting comma characters into the HTML. When the result is aPromise, it produces the literal string[object Promise].How to reproduce
The bug requires multi-level cascading suspensions: component A throws a Promise, and after resolving, its render tree contains components B, C, D that also throw independent Promises at different times. The key conditions:
A._suspended = truestrat A's level aPromiseA._suspendedis true, the code entersstr.then((resolved) => N + resolved + q)resolvedvalue in A's.then()callback is anArray, not a stringN + Array + qcallsArray.toString()→ commas in the HTML outputFix
Extract a helper that handles all three return types —
string,Array, andPromise— using the same pattern already present in the synchronous path.