Last week I introduced a commenting system on my hexo blog.
It all went well at first, until I found that at some point, the comment area and the blog post itself didn’t match.
Finally I modified some code writing and finally fixed the problem.
This blog post documents this slightly tortuous process.
I’ve always wanted to introduce a commenting system, preferably one that doesn’t rely too much on other 3rd party services.
In the end I chose Gitalk. Gitalk is great, convenient, and fits my needs perfectly.
The introduction was very smooth. After referring to the official website and other people’s articles, I successfully introduced it in my blog.
Similar to others, my approach is to introduce the relevant code in the
content-view.ejs section of the layout of my hexo
theme.(Some themes may have a separate
Just like the following code.
<!-- gitalk -->
To debug, I commented on one of my own blogs. All goes well, debugging, code release.
But when I switch pages at will, I find that the comments I just posted sometimes appear, sometimes they don’t.
After some experimentation, I found that when I jumped to this blog post from another page, the comments did not appear. When I refresh the page, or go directly to it, the comments appear normally. This phenomenon is very stable.
At first I thought it was a network problem, but then I realized there must be something wrong.
I wrote some debugging code where the comment code was introduced, and found the clue. code show as below.
<!-- gitalk -->
I found that the output id is not always as expected, i.e. not always the path of the current page. Sometimes it’s the path to the previous page.
After debugging and inspection, it can be found that the page jump of hexo does not refresh the entire page, but partially refreshes and updates the link displayed by the browser.
When the page jumps, the execution timing of this part of
Based on this, the fix is also very simple, that is, where the id is provided, do not use
ejs to obtain it.The corrected code is as follows.
// wrong code
In this way, the correct page path will be generated directly into the html code without relying on the
The essence of this problem is due to the partial refresh of the hexo page jump and the way Gitalk comment id is generated.
Now, the comment system of my blog can finally work smoothly. More comments, suggestions and opinions are welcome. grateful.