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

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

Open
tszhong0411 opened this issue Aug 26, 2024 · 11 comments

Comments

@tszhong0411
Copy link

tszhong0411 commented Aug 26, 2024

Describe the Bug

Using React 19 + Next.js 15

I want to use @react-email/render on edge runtime. However, I got this error:

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

Which package is affected (leave empty if unsure)

@react-email/render

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/react-email-render-on-edge-runtime-5dlkxp

To Reproduce

  1. Go to /api/email
  2. See the error

Expected Behavior

No errors. And render the component successfully in email.

What's your node version? (if relevant)

No response

@gabrielmfern
Copy link
Collaborator

gabrielmfern commented Aug 27, 2024

Not 100% sure we can do something about this as react-dom, which we use internally, seems to be the one using MessageChannel here. All the other APIs we use are supported on the edge without issues.

I've also noticed that the issue goes away when downgrading the React version and Next version to latest, so I do believe we will have support for MessageChannel and this issue going away soon. That being said, we should keep the issue open until it is fixed on Next. I suppose you can also downgrade React to an RC that did not use MessageChannel yet.

@shkreios
Copy link

@gabrielmfern , I’m not sure if supporting the Edge runtime as a separate environment, alongside browser and Node, is within the scope of react-email. However, if it is, you might find it helpful to review how the Next.js team addressed this issue here.

They used the react-dom/server.edge import to ensure compatibility between React and the Edge runtime.

@gabrielmfern
Copy link
Collaborator

I’m not sure if supporting the Edge runtime as a separate environment, alongside browser and Node, is within the scope of react-email
They used the react-dom/server.edge import to ensure compatibility between React and the Edge runtime.

Definitely within the scope. Thanks for letting us know about the react-dom/server.edge export, but that does seem to be exclusive to the newer RCs, so I don't think we can really use it yet until we remove support for 18.x.

@gabrielmfern
Copy link
Collaborator

gabrielmfern commented Oct 24, 2024

On another note, seems like Next 15 with edge routes also have this error when using the latest @react-email/render. Another possibility is that we can attempt at importing the edge version and fallback to other versions if it fails. Might need some finagling, but we'll need a way to have both versions working at the same time.

@cjkihl
Copy link

cjkihl commented Oct 25, 2024

Wrote my own render function using react-dom/server.edge

import type { ReactDOMServerReadableStream } from "react-dom/server";

const readStream = async (stream: ReactDOMServerReadableStream) => {
	let result = "";
	// means it's a readable stream
	const writableStream = new WritableStream({
		write(chunk) {
			result += new TextDecoder("utf-8").decode(chunk);
		},
	});
	await stream.pipeTo(writableStream);
	return result;
};

export const renderEdge = async (element: React.ReactNode) => {
	const renderToReadableStream = (await import("react-dom/server.edge"))
		.renderToReadableStream;
	const r = await renderToReadableStream(element);
	const html = await readStream(r);
	const doctype =
		'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
	const document = `${doctype}${html.replace(/<!DOCTYPE.*?>/, "")}`;
	return document;
};

It solves the problem but I encountered a new issue instead:
TypeError: Cannot read properties of undefined (reading 'H')

It seems like React mail is not supporting NextJS 15 unfortunately. I'm not sure how to get past this.

In any case If it's complicated to support edge in the same render function due to backwards compatibility I would't mind having a separate render function for edge runtime. :)

@gabrielmfern
Copy link
Collaborator

@cjkihl Noticed the same error when experimenting with the export. Looking at the compiled code, I am pretty sure this is because Next does its thing with pre-compiled React versions while still involving the user's React version, might be a bug on their side.

@gabrielmfern
Copy link
Collaborator

Made an issue about the error when trying to render with react-dom/server.edge vercel/next.js#71865

@gabrielmfern
Copy link
Collaborator

gabrielmfern commented Oct 31, 2024

Based on what I get from vercel/next.js#71865 (comment), this issue is probably only going to be fully fixed once we get react-markup fully implemented and released, unless we might adopt it while it is still experimental

@dmarcucci
Copy link

dmarcucci commented Nov 6, 2024

So I just got this issue today myself, but I've been using Pages Router and it wasn't happening until I tried to refactor to use App Router instead. In both scenarios, I have the function set to use Edge Runtime.

Is everybody here only using App Router? If so, does the issue resolve if you switch to Pages Router?

Edit: I just realized that somebody on the Next team confirmed this on the issue that @gabrielmfern logged there. So if you don't mind switching to Pages Router, that seems to be a temporary workaround.

@dualdetail
Copy link

Is there a timeline for react-markup anywhere?

@gabrielmfern
Copy link
Collaborator

@dualdetail Not that I know of, I don't think that they even announced it anywhere yet. But I assume it might release around the time React 19 is released.

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

No branches or pull requests

9 participants
@cjkihl @dmarcucci @shkreios @tszhong0411 @gabrielmfern @dualdetail and others