You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Edit: I made a new version that uses handlebars to template so you dont have to worry at all about sanitizing props. Quick benchmark showed it rendering 1000 emails in 9ms, while react email render took 2400ms. This version creates the template at runtime with no code-gen. You could also save the templates as files if you want to avoid react email as a runtime dependency.
I made this script as an alternative to using await render(<MyTemplate />). It generates a .ts file that exports functions that will generate the emails from props with template literals.
I'm not sure if this functionality already exists and I overlooked it, but I cant find anything in the docs or github, so I thought I might share this in-case anyone else finds it useful / for people to point out any issues with this approach.
The advantage over the render function is no runtime dependencies, and a couple orders of magnitude faster generation and it's synchronous. The disadvantages are the rendered output cannot be conditional on the props, props must be all strings and the email must insert props into the html without modification. Additionally, there is no sanitation, so be sure to sanitize props.
I also added the ability to define a subject function using MyTemplate.Subject = (props) => `Welcome ${props.username}`;
It assumes the directory './emails' contains all your react emails. Just run it with tsx (or similar).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Edit: I made a new version that uses handlebars to template so you dont have to worry at all about sanitizing props. Quick benchmark showed it rendering 1000 emails in 9ms, while react email render took 2400ms. This version creates the template at runtime with no code-gen. You could also save the templates as files if you want to avoid react email as a runtime dependency.
https://gist.github.com/hillac/0a412201d56591d7d229e173548911ea
I made this script as an alternative to using
await render(<MyTemplate />)
. It generates a .ts file that exports functions that will generate the emails from props with template literals.https://gist.github.com/hillac/52eabc4cabcf32386e81c46b0a852eec
I'm not sure if this functionality already exists and I overlooked it, but I cant find anything in the docs or github, so I thought I might share this in-case anyone else finds it useful / for people to point out any issues with this approach.
The advantage over the render function is no runtime dependencies, and a couple orders of magnitude faster generation and it's synchronous. The disadvantages are the rendered output cannot be conditional on the props, props must be all strings and the email must insert props into the html without modification. Additionally, there is no sanitation, so be sure to sanitize props.
I also added the ability to define a subject function using
MyTemplate.Subject = (props) => `Welcome ${props.username}`;
It assumes the directory './emails' contains all your react emails. Just run it with tsx (or similar).
Beta Was this translation helpful? Give feedback.
All reactions