bsdnerds logo

Comments Static Site

Comments can be added to a static site. A static site saves you a lot of time compared to a dynamic site (no updates required!). Adding comments conists of two steps: the submission form and the comments.

The comment form I create using Netlify forms and comments are static json files. I chose this because it’s easy to maintain and migrate json files. So the comments are static comments, simply json files. The only interactive part is the Netlify form.


Comment form

Visitors should be able to write comments. A submission form is required. I’ve seen many websites use disqus or other data-hogs (that aren’t free). So can you do it for free or cheap? yes, you can. See Netlify price page

The solution is to use Netlify forms. A nice addition is that it comes with spam-filtering by default. It uses Askimet for that. I added recaptcha to further stop spammers, this is an optional field in the form below.

Comments on static blog

Simply add the form below to your articles. Change the name bsdContactForm to a form name that makes sense for your site. Then change the contact-form-url to display your post url. This is the url the person commented on, I’m using hexo static blog instead of pelican or hugo, so change it to the one for your blog.

    <p class="form-row">
        <label id="contact-form-email-label" for="contact-form-email" class="form-label">Email address</label><br>
        <input type="email" name="email" id="contact-form-email" aria-labelledby="contact-form-email-label" class="pcomment-input" />

    <p class="form-row">
       <input type="hidden" name="url" id="url" for="contact-form-url" value="/<%- page.path %>">

    <p class="form-row">
       <label id="contact-form-message-label" for="contact-form-message" class="form-label">Message</label><br>
    <div data-netlify-recaptcha="true" class="form-row"></div>
    <p class="form-row form-submit">
        <button type="submit" class="button">Send Message</button>

Then you can see submitted forms in your Netlify forms tab. Then click on the respective form. Your website can have multiple forms that are added here.

Netlify form

You can then view your comments submissions by clicking on the form. Any form submission will be shown here.

Netlify comments

These comments won’t show up in your site automatically, because it’s a static site. You can add those comments yourself manually by creating a json file

Netlify forms support notifications in the site settings tab. You can do email, slack or API POST url notification.

Static comments

I list comments using the code below. I’m using Hexo as static blog generator, but a similar code should work on any other static blog system.

What this does is go over the json file that are stored in /source/_data. It parses it and renders the comments to the blog post.

<% var p = page.path.substring(0, page.path.length - 1); %>

<% for (var i in[p]){ %>
  <div class="pcomment">
   <div class="pcomment-<%-[p][i].indent %>">
     <span class="pcomment-username"><%-[p][i].who %></span>
     <span class="pcomment-date">•</span>
     <span class="pcomment-date"><%-[p][i].createdAt %></span>
     <%-[p][i].message %>
<% } %>

So there is a .json file for every blog post in /source/_data. For example, the post /hexo-on-openbsd/ has the blog post hexo-on-openbsd.json. This file looks like this:

        "createdAt": "Mon, 5 Jan 2021",
        "indent": 0,
        "message": "<p>Thanks, have been searching for hours for this</p>",
        "order": "004836450068",
        "who": "Jules"
        "createdAt": "Mon, 5 Jan 2021",
        "indent": 1,
        "message": "<p>No problem, glad to help</p>",
        "order": "004836450068",
        "who": "Frank"

This is the css theme I’ve used for the comments:

/* comments */
.pcomment-input{background:white;border-top:1px solid rgba(0,0,0,0.1);width:96%;height:40px;border-left:1px solid rgba(0,0,0,0.1)}.pcomment{background:white;border-top:1px solid rgba(0,0,0,0.1);padding-top:0.95em}.pcomment-username{font-weight:bold;color:#555}.pcomment-9{padding-left:64px}.pcomment-8{padding-left:64px}.pcomment-7{padding-left:64px}.pcomment-6{padding-left:64px}.pcomment-5{padding-left:64px}.pcomment-4{padding-left:64px}.pcomment-3{padding-left:64px}.pcomment-2{padding-left:64px}.pcomment-1{padding-left:64px}.pcomment-0{padding-bottom:30px}.pcomment-date{color:gray !important;font-weight:normal;text-shadow:none !important}.pcomment-text{background:#fff !important;width:96%;height:150px;color:#000;font-family:'proxima-nova', Arial,Tahoma,Verdana;font-size:15px;padding:10px;margin:5px 0;border:1px solid #dfdfdf;outline:none}

That’s it. You now have comments on your static site, as shown below.

Octoxalis Mon, 22 Feb 2021

The hexo link is dead. How is your json file generated and accessed?

Frank Mon, 22 Feb 2021

Right now it's hard-coded manually

Trevor Mon, 5 Jan 2021

I tried many different solutions like disqus, isso and others but they always felt off, the theme wasn't right, they would be bloated or would cost to much for a hobby blog.

Frank Mon, 5 Jan 2021

I felt the same way, this is a good solution for static blogs