Skip to content

Dynamic import in @react-email/render breaks when using sentry #2529

@alex-e-leon

Description

@alex-e-leon

Describe the Bug

@gabrielmfern This is a follow up from my closed PR (#2455) - I finally had the time to investigate the issue.

The recentish change to dynamically import react-dom in @react-email/render (https://github.com/resend/react-email/blob/canary/packages/render/src/node/render.tsx#L9) is problematic as dynamic imports do not work in workers as per the esm spec (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import)

IMO its a pretty severe issue, as its not always clear whether workers are even being used in an application.

In my particular case after a lot of investigation I discovered that @sentry/node wraps monitored services in workers (I'm guessing to get better access to debugging/process information, but honestly I'm not exactly sure why) - meaning that any node app running sentry cannot call @react-email/render

@gabrielmfern - you mentioned that the dynamic import was added to handle some nextjs issues - I'm not clear on what they are or if there are any other solutions, but if not, maybe it makes sense to export 2 seperate versions of render, one with dynamic imports and one without?

Which package is affected (leave empty if unsure)

@react-email/render

To Reproduce

You'll need to setup a sentry account to reproduce the issue. Otherwise you may also be able to trigger it by manually calling render inside a node worker.

Expected Behavior

For node not to crash.

The error message for reference is:

TypeError [Error]: Cannot read properties of undefined (reading 'resolve')
    at MessagePort.handleMessage (node:internal/modules/esm/worker:179:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:827:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28)

What's your node version? (if relevant)

22

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions