Implementing Giscus in Blazor WASM
One of the features that was sitting in our backlog was to enable comments on our blogs. There isn't a whole lot of traffic, yet, but it's an expected feature to have. For a small business, cost is obviously a concern, so I went searching for a low / no cost option to get this feature in quickly. The gold standard seems to be Disqus, and I went down that path first. The implementation was relatively easy, but I quickly found that some bloat in the product existed.
It is well documented on the web that Disqus has, relatively recently, implemented ads in all free tier implementations. At first I wasn't terribly bothered by that, as these days it is kind of expected to see ads on sites. It quickly became a problem when releasing, however, as the ads created a host of issues in the site. As I always make sure to follow security best practices, the ads threw a bunch of errors because I didn't include their roots in my content security policy (CSP). Seeing the overwhelming amount of errors was a red flag. I had to research the various roots to see if they were safe to include in the CSP. I became frustrated with this process, as it shouldn't be this much work to get something implemented. Then, researching the internet, I found out how much Disqus now uses the product to compile and aggregate user data, with or without the users' knowledge. This was unacceptable to me. Compounded with the fact that the interface of the comments looked very amateurish, especially with the reactions that were implemented in the Disqus interface, I went looking for an alternative.
After reading several articles it was apparent I wasn't alone in my concerns, and the alternative that I kept seeing was with a tool called Giscus. Giscus is an application extension in GitHub that leverages GitHub discussions to implement, track, and display comments. Here are two posts that helped guide my research and implementation:
- Brice Dutheil - Moving from Disqus to Giscus
- Andrew Lock - Replacing Disqus with GitHub discussions using giscus
The implementation with Giscus was smooth, but it took a little extra studying than the Disqus implementation. The core implementation of Giscus also seems to be centered on standard web page models, rather than Single Page Applications (SPAs). Since I was using Blazor to implement the comments, it took some nuance to get up and running. More on that in a sec.
The one major downside I see in the use of Giscus is that it requires a GitHub account to comment. This works fine for a blog centered on technology like for Jacaru Tech, but we have other companies in our fold for which this may be a little dicey. Pure Chic Creations is a design and craft based business. Jacaru REI is centered on Real Estate. I can't expect the target audience of those pages to have GitHub accounts. While I implemented Giscus on those sites, I may need to look at Facebook Comments, instead, taking into account the use cases of those companies. Disqus allows one to login using their email or other social media accounts, which is definitely the one thing that sets it apart.
Back to the Blazor implementation of Giscus...
The Giscus documentation does a pretty good and straightforward job of explaining what you need to do to get it up an running. The problem I ran into was that the docs tell you to put the following script tag in your file:
<script src="https://giscus.app/client.js" data-repo="[ENTER REPO HERE]" data-repo-id="[ENTER REPO ID HERE]" data-category="[ENTER CATEGORY NAME HERE]" data-category-id="[ENTER CATEGORY ID HERE]" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="preferred_color_scheme" data-lang="en" crossorigin="anonymous" async> </script>
This is fine for a multi-page application, where you can place the tag on the relevant page, but since a SPA is technically only one page controlled by routings, it ended up putting a comment block on every page of my application. Integrating this into a Blazor SPA took some finesse and leveraging the JSInterop functionality. Full disclosure, I used a combination of internet research and ChatGPT to formulate my solution. ChatGPT does a great job as a supplement to other research, especially to give you a starting point for the implementation code. This isn't a GPT / AI blog, however, so I will leave my comments at that disclaimer.
The first thing I did was to create a giscus.js file in the wwwroot folder of my client project. You will see that it takes a lot of what is written in the script block above and creates the script dynamically. A script tag needs to be placed in the index.html file of the client project that references this new file. You need other tags, but that is well described in the Giscus docs.
function initializeGiscus() {
const script = document.createElement("script");
script.src = "https://giscus.app/client.js";
script.setAttribute("data-repo", "[ENTER REPO HERE]");
script.setAttribute("data-repo-id", "[ENTER REPO ID HERE]");
script.setAttribute("data-mapping", "pathname");
script.setAttribute("data-category", "Announcements");
script.setAttribute("data-category-id", "[ENTER CATEGORY ID HERE]");
script.setAttribute("data-strict", "1");
script.setAttribute("data-reactions-enabled", "1");
script.setAttribute("data-theme", "preferred_color_scheme");
script.setAttribute("data-emit-metadata", "0");
script.setAttribute("data-input-position", "top");
script.setAttribute("data-lang", "en");
script.setAttribute("data-loading", "lazy");
script.setAttribute("crossorigin", "anonymous");
script.async = true;
script.id = "giscus"
document.body.appendChild(script);
}
Next, I created a shared component in the client project. The component is very lightweight. I opted to use a code block on the page rather than a code behind file, since it was already skinny. The component calls the initialization code after render. I then placed the component in the appropriate "page" of my client project. You need to replace the "[]" information with your specific details, but Giscus does a good job of aiding that during implementation. Here is the component code:
@inject IJSRuntime JsRuntime
<div class="giscus"></div>
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JsRuntime.InvokeVoidAsync("initializeGiscus");
}
}
}
And that's it! Comments enabled. This may or may not be an elegant solution, but it worked for me. I'd love to hear alternatives in the comments!
