So Jekyll is awesome! Fast, lean, and mean. But what about sitemaps?
It’s actually really easy.
Via a Plugin
A very simple way is to use the jekyll-sitemap plugin. This plugin, when activated, will create a sitemap.xml file on your site that will contain a list of all of your pages.
Add this to your _config.yaml
file, making sure to add it to the following ‘gems’ and ‘whitelists’ areas if they are already specified:
gems:
- jekyll-sitemap
whitelist:
- jekyll-sitemap
Depending on your setup, you may have to add it to you Gemfile
:
gem "jekyll-sitemap"
And then either install the gem:
bundle install
Now just build/serve, and you have your sitemap!
Via a file
The other option is to make a file called ‘sitemap.xml’ in your main jekyll folder, as in next to _post
, _pages
, and _includes
.
All the file needs to contain is the following:
---
layout: null
---
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for post in site.posts %}
<url>
<loc>{{ site.url }}{{ post.url }}</loc>
{% if post.lastmod == null %}
<lastmod>{{ post.date | date_to_xmlschema }}</lastmod>
{% else %}
<lastmod>{{ post.lastmod | date_to_xmlschema }}</lastmod>
{% endif %}
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
{% endfor %}
{% for page in site.pages %}
{% if page.sitemap != null and page.sitemap != empty %}
<url>
<loc>{{ site.url }}{{ page.url }}</loc>
<lastmod>{{ page.sitemap.lastmod | date_to_xmlschema }}</lastmod>
<changefreq>{{ page.sitemap.changefreq }}</changefreq>
<priority>{{ page.sitemap.priority }}</priority>
</url>
{% endif %}
{% endfor %}
</urlset>
When you run serve
or build
, the liquid code above will generate a list of all the pages in your blog/site and serve/save it as ’example.com/sitemap.xml’