Eleventy: sitemap.xml.pug

Created on 21 May 2019  Â·  6Comments  Â·  Source: 11ty/eleventy

I was trying to create a sitemap using pug (and I'd prefer to use pug rather than nunjucks for consistency across my project).

---
permalink: /sitemap.xml
eleventyExcludeFromCollections: true
---

<?xml version="1.0" encoding="utf-8"?>

urlset(xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
    each page in collections.all
        url
            loc=page.url

… errors with:

Problem writing Eleventy templates: (more in DEBUG output)
> Having trouble rendering pug template ./sitemap.xml.pug (TemplateContentRenderError)
> Having trouble compiling template ./sitemap.xml.pug (TemplateContentCompileError)
> partials:1:1
  > 1| /sitemap.xml
-------^

unexpected token "slash" (Error):

I encounter the same error with permalink: '/sitemap.xml' and permalink: "/sitemap.xml".

On removing frontmater, that is with

<?xml version="1.0" encoding="utf-8"?>

urlset(xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
    each page in collections.all
        url
            loc=page.url

… the sitemap renders to dist/sitemap.xml/index.html.

How do I set it up to output to dist/sitemap.xml?

Most helpful comment

That is weird.

I tested it w/ a vanilla project, and in nunjucks I get _site/sitemap.xml, as you are from this front matter alone.

---
permalink: sitemap.xml
---

A pug file has its front matter parsed as pug code, so it needs to be quoted (since YAML also uses the pipe | character) and then piped to tell pug to output the string.

This works to generate _site/sitemap.xml:

// sitemap.pug
---
permalink: "| sitemap.xml"
---

@shreyasminocha

You could use either of the below to achieve the same goal.
(I work with pug often and have gotten into the habit of using dynamicPermalink: false, which tells Eleventy not to treat the permalink as pug code.)

permalink: "| sitemap.xml"
---
permalink: sitemap.xml
dynamicPermalink: false
---

All 6 comments

@shreyasminocha

https://www.11ty.io/docs/permalinks/#disable-templating-in-permalinks

This will write the file to the exact path of your permalink string.

dynamicPermalink: false
---

@jevets Wow, thanks. I seem to have missed that in the docs.

Weird, my sitemap uses nearly identical syntax and works fine. I'm using nunjucks with frontmatter:

permalink: /sitemap.xml
sitemapIgnore: true
---

@edwardhorsford

The template file is named sitemap.xml.pug.
Is yours named with a template language extension?

Mine is just sitemap.njk.

Since the filename isn't used in the permalink, wouldn't that point to this being a bug?

That is weird.

I tested it w/ a vanilla project, and in nunjucks I get _site/sitemap.xml, as you are from this front matter alone.

---
permalink: sitemap.xml
---

A pug file has its front matter parsed as pug code, so it needs to be quoted (since YAML also uses the pipe | character) and then piped to tell pug to output the string.

This works to generate _site/sitemap.xml:

// sitemap.pug
---
permalink: "| sitemap.xml"
---

@shreyasminocha

You could use either of the below to achieve the same goal.
(I work with pug often and have gotten into the habit of using dynamicPermalink: false, which tells Eleventy not to treat the permalink as pug code.)

permalink: "| sitemap.xml"
---
permalink: sitemap.xml
dynamicPermalink: false
---
Was this page helpful?
0 / 5 - 0 ratings

Related issues

DirtyF picture DirtyF  Â·  3Comments

ndaidong picture ndaidong  Â·  4Comments

matt-auckland picture matt-auckland  Â·  3Comments

proog picture proog  Â·  3Comments

nebrelbug picture nebrelbug  Â·  3Comments