Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't add html comments in _document.js #3904

Closed
shanedaugherty opened this issue Feb 27, 2018 · 9 comments
Closed

Can't add html comments in _document.js #3904

shanedaugherty opened this issue Feb 27, 2018 · 9 comments

Comments

@shanedaugherty
Copy link

Can't add html comments in _document.js, I believe this is just a something that isn't possible with jsx, but wondering if there may be a workaround.

The reason I need to do this is because google is trying to index urls that are in the __NEXT_DATA__ object and I'd like to wrap the whole script tag in <!--googleoff: all-->.

@dbrodie122
Copy link

You can write comments in JSX by wrapping them in curly braces. But keep in mind you have to return a single element from React components.

This works:

const MyComponent = (props) => {
  return (
    <div>
      {/*This is a comment*/}
      Some Text
    </div>
  )
}

This will cause an error:

const MyComponent = (props) => {
  return (
      {/*This is a comment*/}
    <div>
      Some Text
    </div>
  )
}

@ghost
Copy link

ghost commented Feb 28, 2018

Hey @dbrodie122 dbrodie122 thanks for the input but I need an html comment to appear in the dom / server side rendered html. Jsx comments don't make it into the dom.

Example: <!--googleoff: all-->

I think it's the ! that causes issues.

@haohcraft
Copy link

Have you tied this in your own _document.js?

export default class MyDocument extends Document {
	render() {
		return (
			<html lang="en">
				<Head />
				<body>
					<div className="root">{"you main app"}</div>
					<script dangerouslySetInnerHTML={{ __html: `<!--googleoff: all-->` }} />
					<NextScript />
					<script dangerouslySetInnerHTML={{ __html: `<!--googleon: all-->` }} />
				</body>
			</html>
		);
	}
}

So later in DOM, it would be like

<script><!--googleoff: all--></script>
<script> __NEXT_DATA__  = xxxx</script>
<script><!--googleon: all--></script>

Since google would crawl the document as a plain text, feels like this would work.

@shanedaugherty
Copy link
Author

Pretty clever @haohcraft . That does the trick. Thanks 🙏

@miwialex
Copy link

miwialex commented Dec 5, 2018

I also need to do this, but unfortunately can't be inside a script tag. Its something that needs to be injected right into the header. I'm using nginx to do virtual includes so it needs to look like

<!--# include virtual="<thing to include>" -->

any thoughts?

@craigcbrunner
Copy link

I also need what miwialex is saying too... Not inside a script tag... Can't figure out a way.

@craigcbrunner
Copy link

@miwialex I figured out a way to do this, hackily tonight.. I had a huge issue with JSS and Emotion conflicting and needed to add a comment which JSS references to change the insertion order of the CSS... Anyways long story short what worked for me is this, in my _document.js

Basically above my (the Next.js HEAD component), I added this:

      <head dangerouslySetInnerHTML={{ __html: '<!-- jss-insertion-point -->' }}>
      </head>

Then everything in the HEAD component got appended after that, which worked for my use case, at least in chrome, I haven't tested other browsers...

So for me it looks like this:

class MyDocument extends Document {
  constructor(props) {
    super(props);
    const { __NEXT_DATA__, ids } = props;
    if (ids) {
      __NEXT_DATA__.ids = ids;
    }
  }

  render() {
    const { pageContext } = this.props;

    return (
      <html lang="en" dir="ltr">
      <head dangerouslySetInnerHTML={{ __html: '<!-- jss-insertion-point -->' }}>
      </head>
        <Head>
          <meta charSet="utf-8" />
          {/* Use minimum-scale=1 to enable GPU rasterization */}
          <meta
            name="viewport"
            content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no"
          />
          .... other crap removed to shorten this.

        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </html>
    );
  }
}

@doronnac
Copy link

@craigcbrunner @haohcraft You both deserve a medal, spent so much time trying to figure this out. Thank you.

@jlbovenzo
Copy link
Contributor

jlbovenzo commented Feb 5, 2019

@craigcbrunner To change the CSS injection order, I followed this steps:

  • add a noscript tag inside Head component (_document.js)
return (
  <html lang="en" dir="ltr">
    <Head>
    	.
    	.
    	.
      <noscript id="jss-insertion-point"></noscript>
      .
      .
      .
    </Head>
    <body>
      <Main />
      <NextScript />
    </body>
  </html>
);
  • specify in JSS create method the element created (_app.js)
const jss = create({
  insertionPoint: process.browser ? document.getElementById('jss-insertion-point') : null,
});

@lock lock bot locked as resolved and limited conversation to collaborators Feb 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants