Archived Content
tl;dr
Just show me the code!
Have you ever been browsing around the web and thought something on a webpage looked cool and wondered how they made that work? Maybe you were on Keybase and fired up your dev tools with F12
. All of a sudden, you are greeted with this:
âOh, how nice,â you think to yourself. âWhat a nice little greeting.â
You smile thinking about how cool the keybase team is with their little â\o/ \o/â fake emojis and you forget why you even opened your dev tools in the first place.
You suddenly rise from your chair and exclaim âIâd like to add something like that to my site to make the developers of the world smile!â
But, you remember, âMy site is built with Gatsby. How in the heck am I going to make that work?â
Rest easy, child. Iâve got your back. Keep reading to discover the secret to adding HTML comments to a website in 2020.
Really, a comment? Whatâs so hard about that?
When you first go to try and implement something like this, you might run into the fact that there really isnât a good way to render HTML comments with React without resorting to hacks. A library like React (and Gatsby which uses React) mostly treat your outputted HTML as a build artifact. If you view it like that, there isnât really a reason to put comments in the build output. You can put comments all over your JS files, but none of that makes it to the outputted HTML.
Simple Implementation
There a few workarounds to get an HTML comment to show up. The main way to do it is to use dangerouslySetInnerHTML
. If all you are trying to do is get a comment to show up somewhere in your HTML, you could do this:
This will output a div
with a comment in it:
Thatâs not really what we want, though. We want our comment to live outside of any containing element. Not only that, but we want it to live as the first element under html
(more on that later). We could try hijacking the div
using imperative DOM methods to get rid of the containing element. Something like this could work:
This will work and get you some comments in your HTML without a surrounding div
. However, since we are making use of react-dom
here, this will not work for SSR which is what Gatsby does to render your site statically. useEffect
also does not fire with SSR since the component is never actually mounted.
Using Gatsbyâs SSR APIs
Gatsby provides some extension points to add components to your head
or body
. We could make use of them by adding a gatsby-ssr.js
:
This will add a script
tag as the first element in your head
with its content set to a JS comment. Although this works and has the benefit of being able to be packaged up into a plugin that can easily be reused across all of your sites, it isnât exactly what we want. If you open dev tools with this solution:
You donât see shit. Chrome doesnât expand the head
, much less a random script
by default. No developers are going to see it buried in there:
We really need the comment to be the first thing under html
for anyone to see it when they first open their dev tools. Unfortunately, Gatsbyâs SSR APIs donât provide a mechanism to make that work.
The Solution
When the SSR APIs arenât enough, Gatsby provides html.js
, a fallback mechanism to customize the default HTML that Gatsby generates.
Overriding the default html.js
is not my first choice for extending Gatsby. It canât be packaged up into a plugin and if the default HTML is updated, youâd have to update your custom copy. But, for now, it seems to be the only way to get what we want.
We can add a src/html.js
file to our site:
There are a few things going on here. We take the default html.js
component Gatsby provides, rename it to Root
and take the root html
element out of it. We then add an Html
component which renders Root
to a string using ReactDOMServer.renderToStaticMarkup
which will work when using SSR. Finally, we stuff all of that HTML into the root html
element putting our comment first.
After all of that, when we open dev tools, we are greeted with:
Ah ha! We done did it! It was a long road with lots of plot twists, but we got here. If youâve read this far, you are now armed with the skills to add comments to websites. Feel free to add that bullet point to your rĂ©sumĂ©.
Now go out there and add your own message to your own site. Youâll have to think of something witty to say on your own.
If you enjoyed this article, follow me on Twitter to get more of my crazy ideas as I dream them up in real-time. đ