mkdocs-git-revision-date-localized-plugin icon indicating copy to clipboard operation
mkdocs-git-revision-date-localized-plugin copied to clipboard

correctly populate `lastmod` of `sitemap.xml`

Open thesuperzapper opened this issue 2 years ago • 2 comments

I am currently using this plugin to fix the lastmod dates on the sitemap.xml because MkDocs does not correctly generate the lastmod, as it uses the build time of the site for ALL pages.

Having correct lastmod is important for SEO, as it indicates to Search engines when to re-index pages, see this blog from Bing.

With git-revision-date-localized-plugin, I am able to use the git_revision_date_localized_raw_iso_datetime variable (with a few string formatting filters) to give a correct timestamp that actually reflects the last content update of each page.

Here is the sitemap.xml theme override I am using:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for file in pages -%}
    {% if not file.page.is_link and (file.page.abs_url or file.page.canonical_url) %}
    <url>
         <loc>{% if file.page.canonical_url %}{{ file.page.canonical_url|e }}{% else %}{{ file.page.abs_url|e }}{% endif %}</loc>
         {#- NOTE: we exclude `lastmod` for pages using a template, as their update time is not correctly detected #}
         {%- if not file.page.meta.template and file.page.meta.git_revision_date_localized_raw_iso_datetime %}
         <lastmod>{{ (file.page.meta.git_revision_date_localized_raw_iso_datetime + "+00:00") | replace(" ", "T") }}</lastmod>
         {%- endif %}
         <changefreq>daily</changefreq>
    </url>
    {%- endif -%}
{% endfor %}
</urlset>

NOTE: if @timvink wants to make this a first-party part of the plugin, please go ahead, but I recommend getting the user's timezone setting, instead of assuming its in UTC, like above.

thesuperzapper avatar Oct 02 '23 18:10 thesuperzapper

It's a cool trick, thanks for sharing!

I don't feel that plugin should also do (automatic) overrides. I also don't want to mess with the sitemap, as mkdocs is responsible for generating it & I would need to read tests for making sure things don't break in the future.

I will add your example to the documentation however (with reference).

timvink avatar Oct 15 '23 19:10 timvink

@timvink go ahead, I think it will help lots of people!

Just remember to warn people that if the user has set the plugin's timezone it might need to be slightly different.

thesuperzapper avatar Oct 17 '23 06:10 thesuperzapper