Solvedeleventy How to create sitemap.xml?

Could you provide some hint on how to create a sitemap.xml automatically?

17 Answers

✔️Accepted Answer

Hi @rolandtoth. I’ve created one for my (yet un-launched) site. You can use the following template (this uses liquid, but the underlying idea will work regardless of template format):

---
permalink: /sitemap.xml
---
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for item in collections.all %}
{%- unless item.data.ignore == true %}
  <url>
    <loc>https://example.com{{ item.url }}</loc>
    <lastmod>{{ item.date | date: '%Y-%m-%d' }}</lastmod>
    <changefreq>{{ item.data.changefreq }}</changefreq>
    <priority>{{ item.data.priority }}</priority>
  </url>
{%- endunless %}
{%- endfor %}
</urlset>

You’ll need to create a collection (I’ve used collections.all) that encompasses all the files you want in your sitemap (see the documentation on how to achieve this). You’ll also want to ensure pages have the appropriate priorities and change frequencies set. I do this by using directory specific data files, to save adding these values to every page I create. Here’s an example for all my article pages:

{
  "layout": "layouts/article",
  "permalink": "{{ page.date | date: '%Y/%m' }}/{{ page.fileSlug }}.html",
  "changefreq": "monthly",
  "priority": "0.9"
}

For single pages, or if you need to override the above in a particular instance, just set the same values in your frontmatter, like so:

---
title: 'My great page'
permalink: /path/to/index.html
changefreq: weekly
priority: 1
---
Hello, and welcome to my great page!

I also allow pages to be excluded from the sitemap if they have the value ignore set to true. For example, here’s my 404 page:

---
title: '404 - Page Not Found'
layout: layouts/page
permalink: /404.html
ignore: true
---

Hope this helps!

Other Answers:

Thanks @paulrobertlloyd ,

this was really useful. I made some minor modifications (added my own filters and adjusted to nunjucks, plus added an xsl stylesheet):

---
permalink: /sitemap.xml
sitemapIgnore: true
---
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/assets/styles/sitemap.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for item in collections.all %}
{%- if item.data.sitemapIgnore !== true %}
  <url>
    <loc>{{ item.url | httpUrl }}</loc>
    {% if item.data.premier.date -%}
    <lastmod>{{ item.data.premier.date | dateDisplay("toISOString") }}</lastmod>
    {%- else -%}
    <lastmod>{{ item.date | dateDisplay("toISOString") }}</lastmod>
    {%- endif %}
    <changefreq>{{ item.data.changefreq | valueIfEmpty("monthly") }}</changefreq>
    <priority>{{ item.data.priority | valueIfEmpty("0.5") }}</priority>
  </url>
{%- endif %}
{%- endfor %}
</urlset>

https://neptunbrigad.hu/sitemap.xml

@zachleat Where would you like to see this documented?
I've played around with the tips here and have a working sitemap.xml (for my use case).
So I could open a PR if you tell me, where the file should go to.

I assume, in the docs folder of https://github.com/11ty/11ty.io ?

More Issues: