fix(render): remove nul bytes when using React 18 #3406
Open
rockingskier wants to merge 1 commit intoresend:canaryfrom
Open
fix(render): remove nul bytes when using React 18 #3406rockingskier wants to merge 1 commit intoresend:canaryfrom
rockingskier wants to merge 1 commit intoresend:canaryfrom
Conversation
|
Contributor
|
@rockingskier is attempting to deploy a commit to the resend Team on Vercel. A member of the Team first needs to authorize it. |
commit: |
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.
Workaround for React 18
renderToPipeableStreamnul-byte bugThe bug
React 18's server renderer encodes HTML into a reusable 2048-byte
Uint8ArrayusingTextEncoder.encodeInto(). When a multi-byte UTF-8 character (e.g. an em-dash —, 3 bytes) would straddle the end of the buffer,encodeIntostops early rather than splitting the character in half, leaving a few trailing bytes of the buffer unwritten.The bug is that React flushes the entire 2048-byte buffer instead of just the portion that was actually written, so those uninitialised trailing bytes (\0) leak into the output stream.
The result is HTML that contains stray nul bytes near internal 2KB boundaries. Most browsers shrug these off, but many email clients interpret them as end-of-file and silently truncate the email at the first nul byte.
Trigger conditions are narrow but absolutely reachable in real emails:
The fix in React
Facebook fixed this upstream in facebook/react#26228 by slicing the buffer to the number of bytes actually written before flushing.
The fix shipped in React 19 and was deliberately not backported to the 18.x line.
The fix in React Email
Since
@react-email/renderstill supports React 18, we need to work around the bug by stripping nul bytes from the HTML afterrenderToPipeableStreamfinishes:This is scoped to the
renderToPipeableStreamcode path (React 18 in Node). TherenderToReadableStreampath untouched since it's unaffected by the bug. Nul bytes have no legitimate place in HTML output, so stripping them unconditionally is safe.References
Upstream fix: facebook/react#26228
Affected: react-dom@18.0.0 - 18.3.1
Fixed in: react-dom@19.0.0+
Fixes
Among others I'm sure
Summary by cubic
Remove stray NUL bytes from HTML when using React 18 server rendering (
renderToPipeableStream) to prevent emails from being cut off in clients like Outlook. We now strip\0characters after the stream completes as a safe workaround for a React 18 bug fixed in React 19.Written for commit 326345e. Summary will update on new commits.