Skip to content

Instantly share code, notes, and snippets.

@wdzajicek
Last active June 15, 2021 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wdzajicek/aa4753f89eb7ac1e339358340a9c7982 to your computer and use it in GitHub Desktop.
Save wdzajicek/aa4753f89eb7ac1e339358340a9c7982 to your computer and use it in GitHub Desktop.
Create a sitemap in a Jekyll project with 2 files
# Save this file in your Jekyll `_plugins/` dir
Jekyll::Hooks.register :pages, :pre_render do |page|
# get the page's last modified time
modification_time = File.mtime( page.path )
# inject modification_time into page's data.
page.data['last-mod-time'] = modification_time
end
---
layout: null
---
{% comment %}
THIS FILE REQUIRES A PLUGIN FILE:
Save the following code as `_plugins/last-mod-time.rb`:
Jekyll::Hooks.register :pages, :pre_render do |page|
# get the page's last modified time
modification_time = File.mtime( page.path )
# inject modification_time into page's data.
page.data['last-mod-time'] = modification_time
end
{% endcomment %}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
{%- for page in site.pages -%}
{% unless page.url contains '.xml' %}
<url>
<loc>{{ site.url }}{{ page.url }}</loc>
<lastmod>{% if page.last-mod-time %}{{ page.last-mod-time | date: '%Y-%m-%dT%H:%M:%S.%LZ' }}{% else %}{{ 'today' | date: '%Y-%m-%dT%H:%M:%S.%LZ' }}{% endif %}</lastmod>
</url>
{%- endunless -%}
{%- endfor -%}
{%- for file in site.static_files -%}
{% if file.extname == '.pdf' %}
<url>
<loc>{{ site.url }}{{ file.path }}</loc>
<lastmod>{{ file.modified_time | date: '%Y-%m-%dT%H:%M:%S.%LZ' }}</lastmod>
</url>
{%- endif -%}
{% endfor %}
</urlset>
@wdzajicek
Copy link
Author

Save _plugins/last-mod-time.rb and sitemap.xml in your Jekyll project and run a build—that's it!

last-mod-time.rb is a custom Jekyll plugin (a hook specifically) that get's each page's last-modification-time and stores it to that page's data.

sitemap.xml is a simple sitemap template that creates an XML file with <url>, <loc>, and <lastmod> tags for each page and PDF file in the site. The <lastmod> time is populated with {{ page.last-mod-time }} which was created by the last-mod-time.rb plugin.

To leave PDF files out of your sitemap file omit the static_files forloop and everything in it ({% for file in site.static_files %}...{% endfor %}.)

@wdzajicek
Copy link
Author

For some reason last-mod-time.rb can't get the File.mtime( page.path ) for some of the pages. Consequently, the sitemap.xml template checks for page.last-mod-time first and uses it if it exists. If {% if page. last-mod-data %} returns false (it doesn't exist,) then the current date/time are used instead as a backup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment