Render utility generates code with `data-id` attributes
Describe the Bug
Using a snippet from the docs: https://react.email/docs/utilities/render
import * as React from 'react';
import { Button } from '@react-email/button';
import { Hr } from '@react-email/hr';
import { Html } from '@react-email/html';
import { Text } from '@react-email/text';
export function MyTemplate(props) {
return (
<Html lang="en">
<Text>Some title</Text>
<Hr />
<Button href="https://example.com">Click me</Button>
</Html>
);
}
export default MyTemplate;
import { MyTemplate } from './email';
import { render } from '@react-email/render';
const html = render(<MyTemplate />, {
pretty: true,
});
console.log(html);
Will produce following HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<p data-id="react-email-text" style="font-size:14px;line-height:24px;margin:16px 0">Some title</p>
<hr data-id="react-email-hr" style="width:100%;border:none;border-top:1px solid #eaeaea" /><a href="https://example.com" data-id="react-email-button" target="_blank" style="line-height:100%;text-decoration:none;display:inline-block;max-width:100%;padding:0px 0px"><span><!--[if mso]><i style="letter-spacing: 0px;mso-font-width:-100%;mso-text-raise:0" hidden> </i><![endif]--></span><span style="max-width:100%;display:inline-block;line-height:120%;mso-padding-alt:0px;mso-text-raise:0">Click me</span><span><!--[if mso]><i style="letter-spacing: 0px;mso-font-width:-100%" hidden> </i><![endif]--></span></a>
</html>
which includes data-id="…” attributes, I don’t think it’s intended behaviour, so I file it as a bug.
Alternatively, additional option to render utility could be added that enables / disables that behaviour (I would vote for disabled by default)
Which package is affected (leave empty if unsure)
@react-email/render
Link to the code that reproduces this issue
https://github.com/dolbyzerr/react-email-test
To Reproduce
npm run dev
Visit: http://localhost:3000/preview/email?view=source&lang=markup
Expected Behavior
No data-id="…” attributes are rendered in the final email markup
What's your node version? (if relevant)
No response
Hi @dolbyzerr, The data attributes are coming from the components you are using to build your email template i.e. Text => p data_id="react-email-text"
This is not a bug as stated here by @bukinoshita
Is there any reason in particular you would want them removed?
Yup, this is intended and not a bug. This is helpful to easily identify the component being rendered, specially when you're using a lot of rows/columns (generating a lot of table inside of table).
But I'm interest to understand the reason to remove.
Hey @bukinoshita @lordelogos,
Generally, I don’t have any excellent reason apart from controlling the output of the render. A couple of minor reasons
- It makes output HTML bigger, which is not a huge concern but yet still important in modern emails (some clients will cut bigger emails https://mailchimp.com/help/gmail-is-clipping-my-email/)
- It's not consistent with render output "spirit": by default, it's uglified, spaces are trimmed in the style property, newlines are stripped etc, I believe it's just a React default behaviour though. It would make more sense to me to make everything that is not necessary under the flag or put that behaviour under the "pretty" flag.
I do understand the purpose of easily addressable HTML nodes for testing and debugging and not against it at all. Just saying it should be optional and not the default.
My specific case is that I am working on an HTML email signature generator for internal usage in the company, and I want to make email signatures as small as possible.
I have a couple of ideas of how it could be achieved:
- Store that option in React context and conditionally render
data-idattributes. Provide context option on render depending on the use case. - Strip down all unnecessary things after HTML is rendered.
Renderpackage is already dependent onhtml-to-textnpm module which is dependent onhtmlparser2so it should be pretty doable without adding too much overhead.
Given that there's a hard limit on the size of the markup that email clients will render above "the fold," it seems unnecessary outside of development/preview environments to render those properties, and dare I say, damaging to do so for production output.
Yes, the Gmail email signature has a limit of 10,000 characters, and each mailbox may have different limits. As far as I know, there is also a limit of 4,000 characters 😅. Therefore, we should simplify the HTML structure as much as possible, remove unnecessary attributes and styles, and use abbreviations whenever possible.
Additionally, I will use html-minifier-terser to further compress the HTML string.
Thanks for the feedbacks! Decided to remove the data-id from components. Let's do it
It's got value for debugging. The issue is rendering for production; the attributes need to be cleaned when doing a production render.
thanks for the active development of the project! Agree that data attribute for developing/testing purposes should be removed on production build if its not functionally used (like in javascript logic hook - not in our email client case :))
This can be clsoed now that most data-id have been removed overall.