Back to overview

Using GitHub Discussions to host my Astro blog comments and reactions

Github discussion style comment section
| 3 min read
View count:

Until the day of writing, I was using a custom made comment section for my blog posts. This included setting up a database with PlanetScale and Prisma as ORM, and a TRPC server to handle the requests, which was a lot of work for a simple comment section. And even then, it was not perfect, because I had to manually clean up spam comments (since I was too lazy to add authentication :-D), I didn’t have support for replies to comments, and I didn’t have reactions.

When I was reading one of the blogs on TkDodo’s website, I noticed he was using GisCus for his comment section. GisCus is based on GitHub Discussions, so I already had this feature available to me as part of the repository I host my website on. The setup was very easy, on the GisCus website you can select the repository you want to use, and it will generate a script for you to include in your website. This will look something like this:

  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]"

The blanks are filled in with the repository name, repository ID, category name and category ID automatically. The only thing I noticed was that using the default <script> tag, clicking on the “Sign in with GitHub” button would refresh the whole page, and it doesn’t scroll down to the comment section. For me that’s quite annoying, so I decided to use the GisCus React component instead. This component is very easy to use, you just have to install it with npm install @giscus/react and then import it in your React component. The props you need to pass to the component are the same as the data attributes in the script tag, but camelCased instead of kebab-cased.

import * as React from "react";
import Giscus from "@giscus/react";

const id = "inject-comments";

const Comments = () => {
  const [mounted, setMounted] = React.useState(false);

  React.useEffect(() => {
  }, []);

  return (
    <div id={id}>
      {mounted ? (
          repo="[ENTER REPO HERE]"
          repoId="[ENTER REPO ID HERE]"
          category="[ENTER CATEGORY HERE]"
          categoryId="[ENTER CATEGORY ID HERE]"
      ) : null}

export default Comments;

To have this comment section on every blog I create, I updated my BlogLayout.astro (which I used to wrap all my blogs ) file to include this component. The relevant part in this file:

<article class="mdx">
  <slot />
<Comments client:idle />

You can see I only load the component when the browser is idle by using the built in client directives from Astro.

And that’s it! A fully functional comment section for my blog posts, without having to manage a database or authentication. Hope it’s useful for you too! Full code can be found on my GitHub.

Did you like this post? Check out my latest blog posts:

Astral picture
29 Mar 2024

Astro DB: Migrating my analytics data from Vercel Postgres

5 min reading time

  • astro
  • database
  • vercel
  • postgres
  • libsql
Screenshot of an analytics dashboard
16 Jan 2024

Basic analytics with Vercel Postgres - Drizzle - Astro

10 min reading time

  • vercel
  • databases
  • typescript
  • astro
Page of a dictionary
15 Nov 2023

A minimal dependency-free translation system for Next.js

6 min reading time

  • nextjs
  • react
  • javascript