create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Preserving HTML comments in the index.html

Open thenathanjones opened this issue 8 years ago • 9 comments

Hey all,

I'm wondering if there is a way to preserve HTML comments from the public/index.html in the generated HTML when running a build?

I'm trying to avoid ejecting at all costs, so if need be I'm just going to add them back in as a simple script, but it'd be easier if I didn't have to do anything.

Thanks in advance.

Cheers, Nathan

thenathanjones avatar Apr 03 '18 09:04 thenathanjones

Why do you need to preserve the comments?

iansu avatar Apr 03 '18 16:04 iansu

Hey @iansu, I have some nginx SSI statements I want included in the final HTML. They're implemented as HTML comments.

thenathanjones avatar Apr 04 '18 08:04 thenathanjones

We currently use the HTML Webpack Plugin to generate index.html so you might want to file an issue with them: https://github.com/jantimon/html-webpack-plugin

iansu avatar Apr 10 '18 19:04 iansu

Hey @iansu thanks for the pointer. Looking at the create-react-app configuration, it appears this behaviour is as intended, and there doesn't look like a logical way to change it without ejecting. I guess I'll stick with the post-processing script option.

thenathanjones avatar Apr 10 '18 19:04 thenathanjones

Ah, you're right, we are removing them intentionally in the production config. I guess the reasoning there is that we've heavily commented our index.html template and there's no reason to ship that to production. That makes it tricky for us to change that setting.

iansu avatar Apr 10 '18 19:04 iansu

Hey @thenathanjones, I am trying to accomplish the same thing. Would you mind sharing the post-processing script you used for this?

MattSidor avatar Apr 30 '18 20:04 MattSidor

Hey @MattSidor. Sorry on the delay, I've been on leave. I've stuck a version of the script in this gist: https://gist.github.com/thenathanjones/88ccf785826ef880c45d2c89d3b3ce96

Essentially I put an tag in my markup where I want everything and then use awk to replace it with the contents of a file containing my SSI tags. I went with the tag approach as it doesn't render anything when developing, yet because the browser doesn't know what it is, it just ignores it.

It's not pretty, but it has caused me no issues so I've not actually revisted it.

thenathanjones avatar May 03 '18 09:05 thenathanjones

Thanks @thenathanjones, very helpful!

MattSidor avatar May 03 '18 17:05 MattSidor

I know create-react-app has a quite strict policy regarding the configurability of the tools it uses (in this case, webpack), but this issue can be quite challenging.

I am using styled-components and material-ui in a couple of projects, both personal and professional. Since they both inject style tags in the head element, the material-ui documentation indicates that it is possible to overcome css-priority issues by using a comment to define the insertion point of the jss-generated tags. This is where the issue with the current webpack configuration comes for me.

This is probably not such a uncommon use case and may suggest that the webpack configuration should not remove the comments when processing the html file ?

dispix avatar Jun 20 '18 10:06 dispix