Growing with the Web

XML sitemaps in Jekyll

Published
Tags:

This post demonstrates how to implement an XML sitemap in Jekyll’s Liquid templating engine. I recommend reading up on XML sitemaps here if you aren’t familiar with them.

Jekyll logo

Basic template

Here is the basic Liquid template used for Growing with the Web’s XML sitemap, it defines:

  • All the posts in the site as 0.5 priority
  • All the pages in the site
    • The home page is marked to change daily with a priority of 1.0
    • Paged index pages using paginator as 0.1 priority
    • Other pages as 0.5 priority

Parts that need to be modified to suit the site are marked with explanations in the variables section below.

---
layout: nil
---

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {% for post in site.posts %}
    <url>
      <loc>site_url{{ site.baseurl }}{{ post.url }}</loc>
      <priority>0.5</priority>
    </url>
  {% endfor %}
  {% for page in site.pages %}
    <url>
      <loc>site_url{{ site.baseurl }}{{ page.url }}</loc>
      {% if page.url == "home_page_inc" %}
        <priority>1.0</priority>
        <changefreq>daily</changefreq>
      {% else %}
        {% if page.url contains "home_page" %}
          <priority>0.1</priority>
        {% else %}
          <priority>0.5</priority>
        {% endif %}
      {% endif %}
    </url>
  {% endfor %}
</urlset>

Variables

Variable Description Example
site_url The home URL excluding the / at the end http://www.growingwiththeweb.com
home_page_inc The relative URL of the home page (including /) /index.html
home_page The relative URL of the home page (excluding /) index.html

Further customisations

A custom priority for posts or pages

The basic template covers most bases and lets you get on with building your content without worrying so much about SEO. There are some times when this won’t fit though, like if you have a great page or want to give a post a more fine-grained priority. I’ll look at enabling this for pages in these examples as it’s fairly easy to apply the same technique to posts.

To start with, create a variable in the relevant post’s YAML header.

---
layout    : some-layout
title     : Some post title
sitemap   :
 priority : 0.9
---

Then check if the variable exists in the sitemap and override the default if so.

{% if page.sitemap.priority %}
  <priority>{{ page.sitemap.priority}} </priority>
{% else %}
  <priority>0.5</priority>
{% endif %}

I introduced the concept of ‘featured’ posts recently which are located in the top articles link in the header and also have higher priority in the sitemap. This is because when someone searches for something, I want the posts that match the search query that I consider to be better to take priority over the lesser ones. For example, if someone searched for ‘CSS’, I’d prefer that the article explaining Triangles in CSS in depth appeared over one of my shorter ones with a demo.

This can be accomplished again by introducing another YAML variable and a wrapping if statement in the Liquid template.

---
layout     : some-layout
title      : Some post title
isfeatured : 1
---
{% if post.isfeatured %}
  <priority>1.0</priority>
{% else %}
  <priority>0.5</priority>
{% endif%}

Comments

comments powered by Disqus
Like this article?
Subscribe for more!