Supercharge your Astro Blog with comments

Play this article

Astro is a powerful, it allows you to ship less JavaScript which means fast sites. One thing I noticed is a lot of Astro blogs don’t seem to have comments, so lets fix that!

What is utterances?

Utterances A lightweight comments widget built on GitHub issues. It allows you to use GitHub issues for blog comments, these are the three main reason I chose Utterances:

  • It’s Open Source
  • You own the data
  • No tracking, no ads, always free.

How to setup Utterance

Utterance uses a single script to handle loading comments and allowing people to make comments on your blog posts. First thing you need to do is create a public repo to house all of your comments. Once you have that you need a script

  repo="[ENTER REPO HERE]"

How to add Utterance to a Astro using Svelte

The power of Astro is It ships 0 bytes of JavaScript by default, only loading it on demand if even needed, all while being able to choose your own UI framework including React, Preact, Svelte, and Vue.

The only difference from using Svelte vs Vue vs React is how you implment your component, but the Astro component will be the same!

Creating our Svelte component

Inside your Components folder create a svelte component named Comments and enter the following:

import { onMount } from 'svelte'
onMount(() => {
    const s = document.createElement('script')
    const tag = document.getElementById('utterances')
    s.src = ''
    tag.parentNode.insertBefore(s, tag)

<div id="utterances"></div>

As you can see from this svelte component, I am telling svelte to load the script, on mount and add it to the <div> declared at the bottom. You will need to update the REPO_NAME with your repo name.

Adding our Svelte component to our Astro component

In my specific Astro application I have an astro component called prose.astro that holds my articles. This is what it currently looks like:

<article class="prose">
  <slot />

  .prose {
    @apply max-w-none
        /* Size Modifiers: */
        /* Color Themes: */;

So to add in the comments we just import the component at the top and then add the component to the html.

import Comments from "./Comments.svelte" ---

<article class="prose">
  <slot />
  <Comments client:load />

  .prose {
    @apply max-w-none
        /* Size Modifiers: */
        /* Color Themes: */;

What does it look like?

Comments in astro.png

At the bottom of every post you will now see box for someone to add their comments! Pretty awesome right?Let me know what you think in the comments! Go on, you know you want to!

Did you find this article valuable?

Support James Perkins by becoming a sponsor. Any amount is appreciated!