How to generate sitemap.xml with Bridgetown

bridgetown, sitemap

28 Mar 2021

During playing with Bridgetown (that I am using on this site), I needed to add a sitemap.xml file for search engines.

As Bridgetown does not have any official solution, Konnor recommended me a solution from _wout_ (thank you!). The solution described in this post is based on it heavily.

Preparing data for sitemap

I wanted to extend the sitemap.xml file to include the lastmod (and changefreq) for each item. Firstly, I needed to have the ability to ignore some of the pages defined in my menu. I added sitemap.ignore attribute for it:

- title: Home
  href: "/"
  content: '<i class="fas fa-user-circle"></i>'
    changefreq: weekly
    ignore: false
- title: Blog
  href: "/blog/"
  content: '<span>Blog</span>'
    changefreq: daily
    ignore: false
- title: Resume on LinkedIn
  href: ""
  content: '<span>Resume</span> <i class="fas fa-external-link-square-alt text-xs ml-1"></i>'
    ignore: true

I also added changefreq, but I am not sure, how much the attribute is valid for Google. In the worst case, it will be ignored.

The lastmod attribute was a little bit more tricky for me, as I am not much experienced with Liquid. As the easiest way, with my knowledge, I choose to have a new post attribute that I called published_at. I wanted to have the date as the initial published date and the last updated date at the same time, to be able to sort posts by only one date.

In terms of a template, I ended up with this code:

<p class="text-indigo-gray-600 dark:text-white mb-2 font-medium">
  {% if post.published_at %}
    {{ post.published_at | date: "%e %b %Y" }}
    <em>・ Updated on {{ | date: "%e %b %Y" }}</em>
  {% else %}
    {{ | date: "%e %b %Y" }}
  {% endif %}

Whenever I want to have the “updated on” for my post, I simply set the published_at for the initial publishing date and change date to the date I updated the post.

Sitemap generation

layout: false
permalink: "/sitemap.xml"

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns=""

  {%- assign newest_post = site.posts | sort: "date" | last -%}

  {%- for item in %}
    {%- if item.sitemap.ignore -%}
      {%- continue -%}
    {%- endif -%}
      <loc>{{ site.url }}{{ item.href }}</loc>
      <lastmod>{{ | date_to_xmlschema }}</lastmod>
      <changefreq>{{ item.sitemap.changefreq }}</changefreq>
  {%- endfor %}
  {%- for post in site.posts -%}
      <loc>{{ site.url }}{{ post.url }}</loc>
      <lastmod>{{ | date_to_xmlschema }}</lastmod>
  {%- endfor -%}

Line #12 will select the lastly updated post so I can set this date as lastmod to all pages in the menu. This is, in my case, useful now only for the blog index page, but I would like to have the last post also on the homepage in the future.

Line #15 skips menu items with external links.

Having the with the newest date also simplify the generation for posts (line #27).

You can see generated sitemap.xml of this site here.

If you know about a better way or you have any comments, don’t hesitate to contact me on Mastodon. Thanks!

Do you like it? You can subscribe to RSS (you know how), or follow me on Mastodon.