timvink / mkdocs-git-revision-date-localized-plugin

MkDocs plugin to add a last updated date to your site pages
https://timvink.github.io/mkdocs-git-revision-date-localized-plugin/index.html
MIT License
193 stars 39 forks source link

correctly populate `lastmod` of `sitemap.xml` #120

Open thesuperzapper opened 11 months ago

thesuperzapper commented 11 months ago

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.

timvink commented 10 months ago

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).

thesuperzapper commented 10 months ago

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