react-email icon indicating copy to clipboard operation
react-email copied to clipboard

Render utility generates code with `data-id` attributes

Open dolbyzerr opened this issue 3 years ago • 9 comments

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>&nbsp;</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>&nbsp;</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

dolbyzerr avatar Jul 03 '23 04:07 dolbyzerr

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"

Screenshot 2023-07-04 at 01 25 12

This is not a bug as stated here by @bukinoshita

Screenshot 2023-07-04 at 01 28 26

Is there any reason in particular you would want them removed?

lordelogos avatar Jul 04 '23 00:07 lordelogos

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.

bukinoshita avatar Jul 04 '23 01:07 bukinoshita

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:

  1. Store that option in React context and conditionally render data-id attributes. Provide context option on render depending on the use case.
  2. Strip down all unnecessary things after HTML is rendered. Render package is already dependent on html-to-text npm module which is dependent on htmlparser2 so it should be pretty doable without adding too much overhead.

dolbyzerr avatar Jul 04 '23 02:07 dolbyzerr

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.

shellscape avatar Aug 24 '23 18:08 shellscape

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.

u3u avatar Aug 25 '23 13:08 u3u

Thanks for the feedbacks! Decided to remove the data-id from components. Let's do it

bukinoshita avatar Sep 21 '23 22:09 bukinoshita

It's got value for debugging. The issue is rendering for production; the attributes need to be cleaned when doing a production render.

shellscape avatar Sep 22 '23 01:09 shellscape

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 :))

roderickhsiao avatar Sep 29 '23 01:09 roderickhsiao

This can be clsoed now that most data-id have been removed overall.

gabrielmfern avatar Nov 11 '23 08:11 gabrielmfern