Skip to content

Conversation

gabrielmfern
Copy link
Member

@gabrielmfern gabrielmfern commented Apr 25, 2024

This fixes the issues that were causing #649. What was causing that issue
was that @react-email/render would import react-dom as the Next
bundled version, which would not be the same React instance used
inside the email templates, this caused that globals such as their
dispatcher would not have the expected values.

I noticed this was the cause of that issue after looking at our
updated trace on the error, and it pointed down into the
bundled version of react-dom inside next. The same issue was
also happening for versions prior to 2.0, so this was not a regression either.

To fix this, I added a simple esbuild plugin to when the email template
is loaded, so that it adds a export { renderAsync } from '@react-email/render'
at the end of it. This way, it would also give us access to the renderAsync
and react-dom used in the user's project, so that this mismatch is not there
anymore.

Since I was experimenting a bit as well, I added in a test to make
sure the getEmailComponent function works properly at least with a local
very simple file.

How can I test this?

  1. Run pnpm install and turbo build inside packages/react-email
  2. Add the following email template at apps/demo/emails/test.tsx
import React from "react";

export default function Test() {
  const containerRef = React.useRef<HTMLDivElement>(null);

  return <div ref={containerRef}>Hello world!</div>;
}
  1. Run npx tsx ../../packages/react-email/src/cli/index.ts dev inside apps/demo
  2. Open http://localhost:3000/preview/test and verify the error from preview server (email dev) does not work with any React hook #649 does not happen

@gabrielmfern gabrielmfern self-assigned this Apr 25, 2024
Copy link

vercel bot commented Apr 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-email ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 10, 2024 2:48pm
react-email-demo ❌ Failed (Inspect) May 10, 2024 2:48pm

@gabrielmfern gabrielmfern added the Package: react-email This is the CLI we generally use as just `email` on the temrinal. label Apr 25, 2024
@gabrielmfern gabrielmfern requested a review from vcapretz April 25, 2024 17:10
name: 'add-export-for-render-async',
setup(b) {
b.onLoad(
{ filter: new RegExp(path.basename(emailPath)) },

Check failure

Code scanning / CodeQL

Regular expression injection

This regular expression is constructed from a [environment variable](1). This regular expression is constructed from a [environment variable](2). This regular expression is constructed from a [environment variable](3).
@gabrielmfern gabrielmfern force-pushed the canary branch 2 times, most recently from eea0258 to e5d2948 Compare April 29, 2024 19:53
@gabrielmfern gabrielmfern force-pushed the fix/different-version-of-react-dom-rendering-templates-on-preview-server branch from 2064b8b to 05209a2 Compare May 8, 2024 15:18
@gabrielmfern gabrielmfern force-pushed the fix/different-version-of-react-dom-rendering-templates-on-preview-server branch from aa375e4 to 5f62de5 Compare May 8, 2024 16:29
…orking and that its returning the renderAsync
This was mostly caused because `pnpm` doesn't allow for resolving
`@react-email/render` when only `@react-email/components` is installed
like npm or other package managers do.

The solution for this was overriding the resolution of the module to get
`renderAsync` from, and then trying both `@react-email/render` and
`@react-email/components` until it actually errors.
…r @react-email/render or @react-email/components
@gabrielmfern gabrielmfern merged commit 42168d0 into canary May 10, 2024
@gabrielmfern gabrielmfern deleted the fix/different-version-of-react-dom-rendering-templates-on-preview-server branch May 10, 2024 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: react-email This is the CLI we generally use as just `email` on the temrinal.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants