Skip to content

How to create sitemap.xml? #1853

Closed
Closed

Description

@rolandtoth

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

Activity

  1. paulrobertlloyd commented on Sep 22, 2018

    @paulrobertlloyd
    Contributor

    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!

  2. rolandtoth commented on Sep 22, 2018

    @rolandtoth
    Author

    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

  3. edwardhorsford commented on Sep 22, 2018

    @edwardhorsford
    Contributor

    Thanks, this looks really useful too!

    Possibly a good candidate for the tutorials section.

  4. edwardhorsford commented on Sep 22, 2018

    @edwardhorsford
    Contributor

    @rolandtoth I'm curious what your valueIfEmpty('foobar') filter is doing.

    Why not do {{ item.data.changefreq | "monthly" }}?

  5. rolandtoth commented on Sep 22, 2018

    @rolandtoth
    Author

    It's a filter I created for nunjucks so that if the variable I apply on doesn't exist then it will try using alternative values. I tried using your suggestion but the build failed ("Template render error").

    module.exports = function () {
        for (arg of arguments) {
            if (arg) {
                return arg;
            }
        }
    
        return false;
    };

    It's not too sophisticated but so far so good. It accepts multiple values, eg. for the SEO description I use something like this:

    {{ seo_description | valueIfEmpty(excerpt, title) }}
    

    So if the frontmatter seo_description is not set, it will try other frontmatter data (excerpt and title) and outputs the first that has value.

  6. edwardhorsford commented on Sep 22, 2018

    @edwardhorsford
    Contributor

    Oops, I got my syntax wrong. What I wanted to suggest was this:
    {{ item.data.changefreq or "monthly" }}

    For your case I'm pretty sure you could do:
    {{ seo_description or excerpt or title}}

  7. edwardhorsford commented on Sep 22, 2018

    @edwardhorsford
    Contributor

    @zachleat is collections.all documented? I hadn't seen it. It's not mentioned on the collections page. It's used on the permalinks page though.

    I'd assumed that since collections group things together by tag, that a tag would be required for it to ever appear. The collections.all method is great.

  8. rolandtoth commented on Sep 22, 2018

    @rolandtoth
    Author

    @edwardhorsford

    Thanks for the "var1 or var2" syntax, it works. However, if you apply a filter on it, you need to put them in parenthesis, otherwise the filter will be applied to the last item only.

    <lastmod>{{ (item.data.premier.date or item.date) | dateDisplay("toISOString") }}</lastmod>

  9. mathiasbynens commented on Sep 24, 2018

    @mathiasbynens
    Contributor

    Here’s how I did it for v8.dev: 11ty/eleventy-base-blog#22 (before I saw this thread)

  10. edwardhorsford commented on Sep 25, 2018

    @edwardhorsford
    Contributor

    @rolandtoth @paulrobertlloyd @mathiasbynens

    per 11ty/buildawesome#253, if you're using pagination, are pages that are generated showing up in your sitemap? I found collections.all was not listing all the pages.

  11. rolandtoth commented on Sep 25, 2018

    @rolandtoth
    Author

    Sorry, I don't have pagination in any project where I have sitemap :)

    Update: just added a sitemap and pages like /page-3/, /page-4/, etc are not included.

  12. zachleat commented on Oct 5, 2018

    @zachleat
    Member

    @edwardhorsford per your comment here #1853 (comment) it looks like no! I’ve filed #21

  13. zachleat commented on Jan 8, 2019

    @zachleat
    Member

    @rolandtoth ah, you might have gotten bit by 11ty/buildawesome#253? Can you follow along over there?

  14. zachleat commented on Jan 8, 2019

    @zachleat
    Member

    This repository is now using lodash style issue management for documentation requests. This means documentation issues will now be closed instead of leaving them open.

    View the documentation queue backlog here. Don’t forget to upvote the top comment with 👍!

  15. Ryuno-Ki commented on Apr 7, 2019

    @Ryuno-Ki
    Contributor

    @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 ?

  16. MaybeThisIsRu commented on Jul 4, 2020

    @MaybeThisIsRu
    Contributor

    per 11ty/buildawesome#253, if you're using pagination, are pages that are generated showing up in your sitemap? I found collections.all was not listing all the pages.

    Additionally, pages generated dynamically such as tag pages also seem to be hit or miss when it comes to the sitemap.

  17. transferred this issue from11ty/buildawesomeon Nov 6, 2025
  18. locked and limited conversation to collaborators on Nov 6, 2025
  19. converted this issue into a discussion #1854 on Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions