Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add comment to Next.js and see it in developer tools?

Tags:

next.js

If I add comment to HTML file, I can see this comment in developer tool in element section, correct? But how can I add comment in Next.js app? I cannot find html file.

I was trying to add comment in _document.js folder, but I dont see comment in dev tool. I would like to write message for visitors of website. Thank you.

import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
  return (
    <Html lang="en">
       {/* Welcome developers! */}
      <Head />
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}

like image 896
pete Avatar asked Sep 11 '25 03:09

pete


1 Answers

You can add this normal browser head tag to the beginning of your document BEFORE your NextJS Head tag, and the Head tag will append all of it's content to your comment.

export default function Document() {
  return (
    <Html lang="en">
      <head dangerouslySetInnerHTML={{ __html: '<!-- Welcome developers! -->' }}>
      <Head />
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}

If you need it to be in your body, one way to do this would be to have it in a script tag, like this:

export default function Document() {
  return (
    <Html lang="en">
      <Head />
      <body>
        <script dangerouslySetInnerHTML={{ __html: '<!-- Welcome developers! -->' }}>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}

Source: https://github.com/vercel/next.js/issues/3904

like image 108
eten Avatar answered Sep 13 '25 18:09

eten