Skip to content

Instantly share code, notes, and snippets.

@vivi90
Created August 24, 2021 13:18
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 vivi90/a8563f3ccbf4e90961540b6dc98224f4 to your computer and use it in GitHub Desktop.
Save vivi90/a8563f3ccbf4e90961540b6dc98224f4 to your computer and use it in GitHub Desktop.
Global multilingual sitemap for Jekyll based websites
---
# Global multilingual sitemap for Jekyll based websites
#
# Version: 1.0.0
# Author: Vivien Richter <vivien-richter@outlook.de>
# License: CC-BY-4.0
#
# Based on: https://sylvaindurand.org/making-jekyll-multilingual/#with-a-sitemaps-file
#
# See also:
# - https://developers.google.com/search/docs/advanced/crawling/localized-versions
# - https://www.sitemaps.org/de/protocol.html
layout: null
---
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- Posts -->
{%- for post in site.posts -%}
{%- unless post.published == false or post.sitemap == false %}
<url>
<loc>{{ site.url }}{{ post.url }}</loc>
{%- assign translations = site.posts | where:"ref", post.ref -%}
{%- for translation in translations %}
<xhtml:link
rel="alternate"
hreflang="{{ translation.locale }}"
href="{{ site.url }}{{ translation.url }}"/>
{%- endfor %}
<xhtml:link
rel="alternate"
hreflang="x-default"
href="{{ site.url }}"/>
<lastmod>{{ post.date | date_to_xmlschema }}</lastmod>
<changefreq>{{ post.changefreq }}</changefreq>
<priority>{{ post.priority }}</priority>
</url>
{%- endunless -%}
{%- endfor %}
<!-- Pages -->
{%- assign pages = site.pages | where: "sitemap", true -%}
{%- for page in pages %}
<url>
<loc>{{ site.url }}{{ page.url }}</loc>
{%- assign translations = pages | where:"ref", page.ref -%}
{%- for translation in translations %}
<xhtml:link
rel="alternate"
hreflang="{{ translation.locale }}"
href="{{ site.url }}{{ translation.url }}"/>
{%- endfor %}
<xhtml:link
rel="alternate"
hreflang="x-default"
href="{{ site.url }}"/>
<lastmod>{{ page.last_modified_at | date_to_xmlschema }}</lastmod>
<changefreq>{{ page.changefreq }}</changefreq>
</url>
{%- endfor %}
</urlset>
@vivi90
Copy link
Author

vivi90 commented Aug 24, 2021

Required front matter YAML:

Example for pages:

ref : about # Please use the same reference for different translations of the same post
locale : en-us
sitemap : true # Visibility in the global sitemap
priority : 0.5 # Between 0.0 and 1.0
changefreq : "yearly" # Possible values: always, hourly, daily, weekly, monthly, yearly, never

Example for posts:

ref : about # Please use the same reference for different translations of the same page
locale : en-us
sitemap : true # Visibility in the global sitemap

Required Jekyll plugins:

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