Preserve Disqus Comments with Jekyll

jekyll 6 comments suggest edit

In my last post, I wrote about preserving URLs when migrating to Jekyll. In this post, show how to preserve your Disqus comments.

This ended up being a little bit tricker. By default, disqus stores comments keyed by a URL. So if you people create Disqus comments at, you need to preserve that exact URL in order for those comments to keep showing up.

In my last post, I showed how to preserve such a URL, but it’s not quite exact. With Jekyll, I can get a request to to redirect to Note that trailing slash. To Disqus, these are two different URLs and thus my comments for that page would not load anymore.

Fortunately, Disqus allows you to set a Disqus Identifier that it uses to look up a page’s comment thread. For example, if you view source on a migrated post of mine, you’ll see something like this:

<script type="text/javascript">
  var disqus_shortname = 'haacked';
  var disqus_identifier = '18902';
  var disqus_url = '';
  // ...omitted

The disqus_identifier can pretty much be any string. Subtext, my old blog engine, set this to the database generated ID of the blog post. So to keep my post comments, I just needed to preserve that as I migrated over to Jekyll.

So what I did was add my own field to my migrated Jekyll posts. You can see an example by clicking edit on one of the older posts. Here’s the Yaml frontmatter for that post.

layout: post
title: "Code Review Like You Mean It"
date: 2013-10-28 -0800
comments: true
disqus_identifier: 18902
categories: [open source,github,code]

This adds a new disqus_identifier field that can be accessed in the Jekyll templates. Unfortunately, the default templates you’ll find in the wild (such as the Octopress ones) won’t know what to do with this. So I updated the disqus.html Jekyll template include that comes with most templates. You can see the full source in this gist.

But here’s the gist of that gist:

var disqus_identifier = '{% if page.disqus_identifier %}{{ page.disqus_identifier}}{% else %}{{ site.url }}{{ page.url }}{% endif %}';
var disqus_url = '{{ site.url }}{{ page.url }}';

If your current blog engine doesn’t explicitly set a disqus_identifier, the identifier is the exact URL where the comments are hosted. So you could set the disqus_identifier to that for your old posts and leave it empty for your new ones.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.



6 responses

  1. Avatar for Justin Nel
    Justin Nel December 10th, 2013

    What if you've migrated from Google's Blogger platform and want to preserve your comments? I've managed to import them, and change the URL's they were assigned to, but Disqus has given all the user's a Disqus email address, so all comments I made in reply to other people's now do not show up as me, even though I login to Disqus with the same email account as I did for commenting on Blogger.

  2. Avatar for haacked
    haacked December 10th, 2013

    I have no idea why that would happen. Perhaps contact their support?

  3. Avatar for Mona Ali
    Mona Ali June 21st, 2014
  4. Avatar for Julien Bourdeau
    Julien Bourdeau March 15th, 2015

    Great tip, thank you very much. I was scared my comments would be lost !

  5. Avatar for kef
    kef January 9th, 2016

    Charles Prince of piddle

  6. Avatar for Chris Martin
    Chris Martin February 8th, 2016

    Great tip! 👍