How I set up lastmod date in my 11ty xml sitemap

Compass on an old map.

Google's new policy has meant changes to the xml sitemap

Google have recently got a little more particular about the last modified date in your xml sitemap following thier decision to drop the ability to ping updates to thier endpoint..

In the post they make mention of the lastmod element and its need to be accurate. Many content management systems just output the date of the last time the file changed which is misleading and will mean that Google will ignore updates if it finds this is constantly happening on a site.

So I wanted to ensure I had a process to ensure I could manually update the lastmod in the sitemap as there is no programatic way to determine what Google considers to constitute a page change!

In my 11ty set up I have used the original published date as part of the url which means that I can easily pull that date into the xml sitemap. If I wanted to make a change to that article I needed a way to update the lastmod in the xml sitemap without changing the page url.

I did this by adding a field in my posts front matter and I called it dateUpdated.

Here is my 11ty post front matter:


title:

description:

image:

imageAlt:

tags:

topic:

sitemapIgnore:

dateUpdated:


If I make a significant update to the page content I can add the date updated in the front matter.

My xml sitemap template is now coded with an if else statement to pull in the updated lastmod date in place of the published date used for lastmod.

xml sitemap template.

The original code for creating the xml sitemap came from 11ty Recipes. and you can copy the original code from there.

In lastmod I have pulled in the DateUpdated data and used .toISOString() to get it in the correct required format and it the dateUpdated field is empty then it uses the page.date from the url - which is the original published date.

And that is it - a fully automated xml sitemap with the ability to manually edit it when articles significantly change.

Sitemap ignore

I also have a sitemapIgnore switch.. If I do not want a page to be in the sitemap I can set sitemapIgnore: True and it skips creating the url entry for the page.

Comments

If you have any suggestions or questions then please message me on Mastodon.

By Simon Cox |

Next post: Build your own 11ty starter

Previous post: Whitesands Quay replacement row of cottages

If you would like to keep up to date with my musings, I do have a handy rss feed!

Featured articles

Latest articles

Or all the articles

Latest Shorts

Or all the short articles