Since I've recently replaced Disqus with Commento I thought it'd be a good idea to share how to embed a different commenting system - in our case Commento - into your Gatsby site (or React app in general).
Zero dependency component
Disqus has an official package for embedding it into React applications which I used to add Disqus to my blog. For Commento I wanted to get rid of the additional dependency; additional dependencies increase maintenance and always come with a certain amount of risk. It's not that relying on open source is a bad idea - it's just that sometimes adding a package for small things is just excessive exaggeration and not worth the price.
So I've implemented a very small component myself which is responsible for embedding Commento with less than 40 lines of code.
Since most commenting systems work the same way (by embedding a simple JavaScript file into your page) this approach should work for other systems like Isso, Schnack, Remark42, etc. too (with small adjustments).
It's a functional component which makes use of the useEffect
hook to embed Commento on the desired pages. Additionally it uses two helper functions (borrowed from disqus-react) to add and remove scripts from our page.
The entire implementation is fairly easy:
Done. That was easy.
The Commento script gets added to our page as soon as the associated container (which id equals commento
) is found and is re-rendered as soon as the id
prop (which should be the post or page id) changes (see optimizing performance by skipping effects for further information).
We can now add comments to all pages by simply adding the <Commento id={uniquePostId} />
component to wherever we want to have comments.