Preserving HTML comments in the index.html
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
Why do you need to preserve the comments?
Hey @iansu, I have some nginx SSI statements I want included in the final HTML. They're implemented as HTML comments.
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
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.
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.
Hey @thenathanjones, I am trying to accomplish the same thing. Would you mind sharing the post-processing script you used for this?
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
It's not pretty, but it has caused me no issues so I've not actually revisted it.
Thanks @thenathanjones, very helpful!
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 ?