Recently we wanted to add functionality to let our readers comment on the blog articles we post on the YGen Website.  After discussing our available options, we chose to use Disqus, a widely used platform for adding comments to any blog.

The initial integration went smoothly as I was easily able to use their JavaScript to add the embedded comment frame to our blog articles.  Next up I wanted to display a comment count for each article on the blog's home page.  Initially the comment count appeared to be working, however, after more testing I noticed that after adding additional comments, the counts shown were no longer correct.

My initial instinct was to blame caching for the incorrect count, since the previous count that was displayed was accurate.  After some investigation I confirmed my suspicion. An inaccurate comment count is annoying but what really bothered me was the fact that when I applied filtering (either by tag or date), the comment count became inconsistent.  I ended up in situations where the same article's comment count would be one number on the blog home page and another when filtering by date.

I discovered that when using the Disqus JavaScript for retrieving the comment count, it first analyzes the elements that will show counts and then generates a parameterized URL to a script file - count-data.js.  This is where the inconsistent comment counts come from.  This file gets cached on the Disqus servers based on the full URL and since the article previews shown varies based on the applied filter, it results in two counts for the same article that are different.

Busting this cached file was relatively simple, I chose to add a hidden "dummy article" comment count based on a variable that will always be different - current time.  An example implementation of this in ASP.NET Razor syntax of this would be:

<span class="hidden disqus-comment-count" data-disqus-identifier="@DateTime.UtcNow.Ticks"></span>

This will result in a generated URL such as "count-data.js?1=my-blog-article&2=636282175638532289" and since this URL will never be the same for two requests, it will always give a consistent comment count.  This solution does come with a small performance decrease of course since a cached version of this file is no longer retrieved.

So if you too are bothered by an inconsistent comment count for your blog articles, you may consider using this cache busting solution on your own site.  Thanks for reading, and if you have any comments or questions, let me know!