0

i use Skeleventy to generate my static site.

Njk template there uses collections.all to generate sitemap for all possible pages, like so

---
permalink: sitemap.xml
hidden: true
---
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for page in collections.all %}
{%- if not page.data.hidden %}
    <url>
        <loc>{{ site.url }}{{ page.url | url }}</loc>
        <lastmod>{{ page.date | htmlDateDisplay }}</lastmod>
    </url>
{%- endif %}
{%- endfor %}

One of the outputs in a resulting sitemap is

https://skeleventy.netlify.app/category/all/

which is a collection of all possible pages - a bit of a mess.

Instead of "category all", it would be better that google indexes each category, for example

<url>
<loc>https://skeleventy.netlify.app/category/software/</loc>
<lastmod>2020-7-20</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/category/writing/</loc>
<lastmod>2020-7-20</lastmod>
</url>

But how can i edit that njk template so that it -captures and outputs different categories in the sitemap? -excludes category/all -leaves other important pages like homepage, each blog post etc.

John Conde
  • 217,595
  • 99
  • 455
  • 496

1 Answers1

1

I think I have it working. I've got this running on my machine and if you want a copy, just reach out. I don't use Nunjucks normally so forgive any dumb mistake. The first mod I did to sitemap.njk was to hide collections.all:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for page in collections.all %}
{%- if not page.data.hidden %}
    {%- if page.url !== "/category/all/" %}
    <url>
        <loc>{{ site.url }}{{ page.url | url }}</loc>
        <lastmod>{{ page.date | htmlDateDisplay }}</lastmod>
    </url>
    {% endif %}
{%- endif %}
{%- endfor %}

Kinda hacky but worked. Next, I needed a way to get the blog category pages. I looked at tags.njk. Based on what I saw there, I wrote a filter for .eleventy.js named categories. I do not think this is a great name:

eleventyConfig.addFilter("categories", function(collections) {
      let categories = Object.keys(collections).filter(c => c !== 'all');
      return categories;
});

Back in the sitemap, I then did this:

{%- set cats = collections | categories %}
{%- for cat in cats %}
    {% set newestDate = collections[cat] | getLatestDate %}
    <url>
        <loc>{{ site.url }}/category/{{ cat }}/</loc>
        <lastmod>{{ newestDate | htmlDateDisplay }}</lastmod>
    </url>
{%- endfor %}
</urlset>

Note the getLatestDate filter, this is defined as such:

eleventyConfig.addFilter("getLatestDate", function(collection) {
    console.log('running getLatestDate');
    return collection[0].date;
});

It seemed to work well. Here is my output:

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://skeleventy.netlify.app/blog/post-1/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    <url>
        <loc>https://skeleventy.netlify.app/blog/post-2/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    <url>
        <loc>https://skeleventy.netlify.app/blog/post-3/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    <url>
        <loc>https://skeleventy.netlify.app/about/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    <url>
        <loc>https://skeleventy.netlify.app/blog/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    <url>
        <loc>https://skeleventy.netlify.app/contact/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    <url>
        <loc>https://skeleventy.netlify.app/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    
    <url>
        <loc>https://skeleventy.netlify.app/category/blog/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    <url>
        <loc>https://skeleventy.netlify.app/category/business/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    <url>
        <loc>https://skeleventy.netlify.app/category/digital/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
    
    <url>
        <loc>https://skeleventy.netlify.app/category/health/</loc>
        <lastmod>2020-8-26</lastmod>
    </url>
</urlset>

If you want a complete copy, just reach out to me.

Raymond Camden
  • 10,661
  • 3
  • 34
  • 68