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

mjml2html render in web worker throws Uncaught ReferenceError: window is not defined #2853

Open
jlarmstrongiv opened this issue Apr 15, 2024 · 2 comments

Comments

@jlarmstrongiv
Copy link

jlarmstrongiv commented Apr 15, 2024

Describe the bug

In a web worker:

mjml-browser.js?v=275a58ae:25 Uncaught ReferenceError: window is not defined
    at ../../../node_modules/mjml-browser/lib/index.js (mjml-browser.js?v=275a58ae:25:7)
    at __require2 (chunk-IRSSH5FH.js?v=275a58ae:23:50)
    at mjml-browser.js?v=275a58ae:24126:16

To Reproduce
Steps to reproduce the behavior:

  1. In vite, create a web worker
  2. Try importing import mjml2html from "mjml-browser";
  3. See error

Expected behavior
I expect mjml-browser to work in web workers, without throwing any errors. It’s a cpu heavy process that would be perfect for web workers

MJML environment (please complete the following information):

  • OS: MacOS
  • MJML Version: 4.15.3
  • MJML tool used: mjml-browser

Email sending environment(for rendering issues):
N/A

Affected email clients (for rendering issues):
N/A

Screenshots
N/A

Additional context
window is not a global variable in web workers https://stackoverflow.com/a/11237259

@bmcminn
Copy link

bmcminn commented Jul 13, 2024

As you mentioned window is not available to web worker contexts.
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers

The way the mjml-browser build script works, it just dumps out a generic amd style export that binds the window object.
build script source: https://github.com/mjmlio/mjml/blob/master/packages/mjml-browser/webpack.config.js
public compiled source: https://cdn.jsdelivr.net/npm/[email protected]/lib/index.js

This would likely require a mjml-webworker package to be built for this use case.

@jlarmstrongiv
Copy link
Author

If we remove the window binding (which the mjml library does not depend on), then the mjml-browser package would work in web workers.

So, I’m not sure if a separate package is required, as long as we modify the generic amd style export in some way:

  • bind to globalThis instead of window
  • only bind to window if it exists

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

No branches or pull requests

2 participants