Skip to content

feat(render): edge & cf worker support #2222

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

Closed
wants to merge 7 commits into from

Conversation

MendyLanda
Copy link
Contributor

This PR adds a polyfill for MessageChannel to prevent errors when importing the package in Vercel Edge and Cloudflare Worker runtimes, or anywhere where MessageChannel is not available.

Closes #1630
Polyfill implementation by @erquhart.
Reference: resend/react-email/issues/1630 (comment)

Copy link

changeset-bot bot commented May 6, 2025

⚠️ No Changeset found

Latest commit: 04db11f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link

vercel bot commented May 6, 2025

@MendyLanda is attempting to deploy a commit to the resend Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

pkg-pr-new bot commented May 6, 2025

Open in StackBlitz

npm i https://pkg.pr.new/resend/react-email/@react-email/render@2222

commit: 04db11f

@MendyLanda
Copy link
Contributor Author

@gabrielmfern you can specify the files that has side-effects to be included in the bundle, all other files will be considered as "sideEffects": false

@gabrielmfern
Copy link
Member

gabrielmfern commented May 7, 2025

you can specify the files that has side-effects to be included in the bundle

@MendyLanda yeah, didn't know about that - good work!

Merging in a few after testing a bit more

@gabrielmfern
Copy link
Member

gabrielmfern commented May 7, 2025

Trying it out with the preview release from pkg.pr.new, it seems to still fail with NextJS. Just do npm install && npm run dev and open http://localhost:3000/edge in https://github.com/gabrielmfern/1630-message-channel-does-not-exist

the problem seems to be because the MessageChannel is not actually undefined, it's just a function that throws: function() { throwUnsupportedAPIError(name); }. Might be very unstable if we check for it being a function too here, so I think we can create another export edge-light and always set the polyfill there, since NextJS imports from it AFAIK.

And then other environments that require it will still work with the current if-statement strategy

@gabrielmfern
Copy link
Member

gabrielmfern commented May 7, 2025

Yeah, my solution still doesn't work because Next.js defines a getter for MessageChannel on global that can't be re-set so it just does nothing and I think it does make sense for them to do it like this. I think it might not be the best way to polyfill it ourselves here, we should use react-dom/server.edge once we can properly support it by merging #1747 instead

@gabrielmfern
Copy link
Member

gabrielmfern commented May 7, 2025

Going to close this for now, unless we think of a better way of doing this

@styxlab
Copy link

styxlab commented Jun 28, 2025

Great work! This implementation helped me for using the edge runtime with react server actions in next.js 15, thanks a lot!

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

Successfully merging this pull request may close these issues.

Error: A Node.js API is used (MessageChannel) which is not supported in the Edge Runtime.
3 participants