Hakyll Pt. 2 – Generating a Sitemap XML File

2018-11-17

This is part 2 of a multipart series where we will look at getting a website / blog set up with hakyll and customized a fair bit.

  • Pt. 1 – Setup & Initial Customization
  • Pt. 2 – Generating a Sitemap XML File
  • (wip) Pt. 3 – Generating RSS and Atom XML Feeds
  • (wip) Pt. 4 – Customizing Markdown Compiler Options
  • (wip) Pt. 5 – Generating Custom Post Filenames From a Title Slug

Overview

  1. Adding a Sitemap Template
  2. Generating the Sitemap XML File
  3. Adding Other Pages and Directories

Adding a Sitemap Template

A sitemap.xml template, just like the templates in the last post, receives context fields to work with (variables, essentially), and outputs the result of applying said context to the template. Here is what our sitemap template will look like today in our project’s templates/sitemap.xml:

Apart from the normal sitemap boilerplate, you can see root, pages, url, date and updated context fields. While date and updated would come from your metadata fields defined for a post, and the url is built from hakyll’s defaultContext, the root and pages fields are custom defined in what will be our very own sitemapCtx context. In the next section, we’ll use this template to generate our sitemap.xml file.

Generating the Sitemap XML File

If you create a hakyll project from scratch, you will start out with a few files that we can add to our sitemap:

  • index.html
  • about.rst
  • contact.markdown
  • posts/2015-08-12-spqr.html
  • posts/2015-10-07-rosa-rosa-rosam.html
  • posts/2015-11-28-carpe-diem.html
  • posts/2015-12-07-tu-quoque.html

You should note that your site.hs file also has the following:

It’s important that you understand that any files you want to be loaded and sent to templates/sitemap.xml must first be matched and compiled before the sitemap can be built. If you don’t do this, you’ll pull your hair out wondering why the file (or folder) you’re trying to include in the sitemap never shows up.

Now, there is something that we are going to emulate to make this sitemap a reality (this should already be in site.hs):

Reading the code above, this essentially says

  1. here’s a file we want to create that does not yet exist (how create differs from match)
  2. when you create the route, keep the filename (what idRoute does)
  3. when you compile, load all the posts, specify what the context to send to each template will be, then make the item (the "" is an identifier… see the source for more), then pass the context to the archive template and pass that on to the default template, ultimately building up a full webpage from the inside-out

Let’s change this 3-step rule to suit our needs before we wrangle the code. We want our rules to say:

  1. here’s a file we want to create that does not yet exist (sitemap.xml)
  2. when you create the route, keep the filename (what idRoute does)
  3. when you compile, load all the posts, load all the other pages, specify what the context to send to each template will be, then make the item, then pass the context to the sitemap template, ultimately building up an XML file

This is almost the same! Let’s write it:

This is starting to look good! But what’s wrong here? Remember the root context bits? We’re going to need to define what that is, and the best way that I’ve found right now is simply as a String; if you want to do something fancy with configuration or reading it in dynamically, then go nuts.

With that defined, we can add it to our contexts:

Hint: if the <> is throwing you for a loop, it’s defined as the same as thing as mappend.

See how we defined constField "root" root in two places? We’re talking about two different contexts here: the sitemap context and the post context. While you could have the postCtx be combined with the sitemapCtx, thus giving the pages field access to the root field, you probably want to use root (and perhaps other constants) wherever you work with posts, so adding them to postCtx for use everywhere seems like the right thing to do.

Once you’ve got all this, run the following to build (or rebuild) your docs/sitemap.xml file:

  1. λ stack build
  2. λ stack exec site clean
  3. λ stack exec site build

Your docs/sitemap.xml should now have all your pages defined in it!

Adding Other Pages and Directories

We’ve done some epic traveling in New Zealand and now want to include a bunch of pages we’ve written in the sitemap. Those pages are:

  • new-zealand/index.md
  • new-zealand/otago/index.md
  • new-zealand/otago/dunedin-area.md
  • new-zealand/otago/queenstown-area.md
  • new-zealand/otago/wanaka-area.md

First, we make sure that our pages get compiled (we’ll use postCtx for them):

And then we want to make sure we add them to our create function:

I could not figure out how to mix globs (new-zealand/**) in with individual file paths (included in fromList), so I had to load them separately; if you figure out how, let me know!

Once you’ve got all this, run the following to rebuild your docs/sitemap.xml file:

  1. λ stack build
  2. λ stack exec site rebuild

Wrapping Up

In this lesson we learned how to dynamically generate a sitemap.xml file using hakyll. Next time, we’ll use these same skills to generate our own RSS and Atom XML feeds.

Next up:

  • (wip) Pt. 3 – Generating RSS and Atom XML Feeds
  • (wip) Pt. 4 – Customizing Markdown Compiler Options
  • (wip) Pt. 5 – Generating Custom Post Filenames From a Title Slug

Thank you for reading!
Robert