DEV Community

Philipp Trommler
Philipp Trommler

Posted on • Originally published at blog.philipp-trommler.me on

Sitemaps for (Small) Static Sites

You may know that I run a small CV site and since I'd like our precious observers to regularly scan my toy sites, I tend to provide a sitemap for them. I've implemented the updates of those sitemaps with a git commit hook I'd like to share with you here.

First of all, I've created a standard sitemap within the root of the repository that feeds my CV website. It goes like the following and should contain no surprises if you're used to them:

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://cv.philipp-trommler.me/</loc>
        <lastmod>2020-05-21</lastmod>
        <changefreq>monthly</changefreq>
        <priority>1.0</priority>
    </url>
</urlset>

Updating this file whenever I change something on my site seems like a cumbersome and error prone task, thus I've written a really small shell script that I use as a pre-commit hook by moving it to .git/hooks/pre-commit:

#!/bin/sh
sed -i "s#<lastmod>.\*</lastmod>#<lastmod>$(date --rfc-3339=date)</lastmod>#g" sitemap.xml
git add sitemap.xml

This script simply exchanges everything inside of <lastmod></lastmod> with the output of date --rfc-3339=date which is the current date in the format expected by the search engines and adds the resulting changes to the index. Therefore, the commit I'm currently editing automatically contains the current date within the sitemap.

Of course this solution doesn't scale well onto bigger sites, there you'd probably go better with a full fledged static site generator. But maybe you also have some kind of one-pager, portfolio site or online CV, that you want to upgrade with an automatic sitemap.

This was a rather short blog entry, but since I haven't written anything in quite some time I thought I share it with you nonetheless. If you have any improvements or found an error, please let me know!

Top comments (0)