VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 技术文档

  • GitHub技巧

  • Nodejs

  • 博客搭建

    • Solving Baidu's Inability to Index Personal Blogs Hosted on GitHub
    • Implementing a Serverless Comment System for Static Blogs with Gitalk
      • Introduction
      • Prerequisites
      • Implementation
        • Installation
        • Usage
        • Bug Fixes
    • GitHub + jsDelivr + TinyPNG + PicGo - Building a Stable, Fast, and Free Image Hosting Solution
    • Vdoing Theme Screenshots
  • 技术
  • 博客搭建
xugaoyi
2019-12-25
Contents

Implementing a Serverless Comment System for Static Blogs with Gitalk

# Implementing a Serverless Comment System for Static Blogs with Gitalk

# Introduction

Gitalk is a comment plugin based on GitHub Issues and Preact.

Let's use it to add a comment section to a blog built with VuePress.

# Prerequisites

Before using something new, you should first understand it.

Gitalk demo: https://gitalk.github.io/ (opens new window)

Gitalk GitHub: https://github.com/gitalk/gitalk (opens new window)

# Implementation

How to implement it? The best approach, in my opinion, is to read the official documentation (opens new window). Here I'm just documenting the implementation steps.

We'll use an existing vuepress-plugin-comment (opens new window) plugin to help integrate Gitalk into our VuePress static blog.

# Installation

npm install --save vuepress-plugin-comment
1

# Usage

The options configuration is the same as Gitalk's configuration.

module.exports = {
  plugins: [
    [
      'vuepress-plugin-comment',
      {
        choosen: 'gitalk',
        options: {
          clientID: 'GitHub Application Client ID',
          clientSecret: 'GitHub Application Client Secret',
          repo: 'GitHub repo',
          owner: 'GitHub repo owner',
          admin: ['GitHub repo owner and collaborators, only these guys can initialize github issues'],
          distractionFreeMode: false
        }
      }
    ]
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

You need a GitHub Application. If you don't have one, click here to create one (opens new window). Set the Authorization callback URL to the domain of the page using this plugin.

After completing the registration, you'll receive a Client ID and Client Secret. Then fill in the corresponding parameters in the configuration, for example:

module.exports = {
  plugins: [
    [
      'vuepress-plugin-comment',
      {
        choosen: 'gitalk',
        options: {
          clientID: 'a6e*******4709b88b',
          clientSecret: 'f0e***************beb8b2d54d7241',
          repo: 'blog', // GitHub repository
          owner: 'xugaoyi', // GitHub repository owner
          admin: ['xugaoyi'], // Users with write access to the repository
          distractionFreeMode: false
        }
      }
    ]
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

After configuring and restarting the project, you'll see a comment section on the page, indicating the comment feature is working! But there are still some bugs to fix. Let's keep improving it.

# Bug Fixes

Comment section style doesn't match the blog

Solution: Create a global style file .vuepress/styles/index.styl and add the following styles:

#vuepress-plugin-comment
  max-width $contentWidth
  margin 0 auto
  padding 2rem 2.5rem
  @media (max-width: $MQNarrow)
    padding 2rem
  @media (max-width: $MQMobileNarrow)
    padding 1.5rem
1
2
3
4
5
6
7
8

Comment section shows Error: Validation Failed.

Problem analysis: When the page URL is too long or contains Chinese characters, the entire URL string exceeds 50 characters, causing the Issues API request to fail with a 422 status code. (Chinese URLs are automatically encoded and become very long; the id parameter defaults to the URL and cannot exceed 50 characters.)

Solution: Manually set the id value and limit its length to 50 characters.

{
 choosen: 'gitalk',
 options: {
   ...
   id: "<%- (window.location.origin + (frontmatter.to.path || window.location.pathname)).slice(-50) %>", //  Unique page identifier, length cannot exceed 50
   title: "Comment: <%- document.title %>", // GitHub issue title
   labels: ["Gitalk", "Comment"], // GitHub issue labels
   body:"<%- document.title %>: <%- window.location.origin + (frontmatter.to.path || window.location.pathname) %>" // GitHub issue body
 }
}
1
2
3
4
5
6
7
8
9
10

When accessing variables like $frontmatter or window, use EJS syntax. Callback functions can't be used in the configuration as they get filtered by VuePress, so EJS syntax is used instead. -- Plugin author's documentation note

After switching pages, the comment section still shows comments from the previous page

Solution: When getting the path for the id, use frontmatter.to.path. The plugin has built-in frontmatter.from and frontmatter.to.

{
 choosen: 'gitalk',
 options: {
   ...
   id: "<%- (window.location.origin + (frontmatter.to.path || window.location.pathname)).slice(-50) %>", //  Unique page identifier, length cannot exceed 50
   title: "Comment: <%- document.title %>", // GitHub issue title
   labels: ["Gitalk", "Comment"], // GitHub issue labels
   body:"<%- document.title %>: <%- window.location.origin + (frontmatter.to.path || window.location.pathname) %>" // GitHub issue body
 }
}
1
2
3
4
5
6
7
8
9
10
Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36
Solving Baidu's Inability to Index Personal Blogs Hosted on GitHub
GitHub + jsDelivr + TinyPNG + PicGo - Building a Stable, Fast, and Free Image Hosting Solution

← Solving Baidu's Inability to Index Personal Blogs Hosted on GitHub GitHub + jsDelivr + TinyPNG + PicGo - Building a Stable, Fast, and Free Image Hosting Solution→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode