Home Generating sitemap with custom priority in Jekyll site
Post
Cancel

Generating sitemap with custom priority in Jekyll site

  1. Create a new file sitemap.xml in the root of your folder.

  2. Copy-&-paste the following content into the newly created file and fine tune it according to your needs. Here, we are using two for loops to render <url> , the first for the posts, and the second for the site’s pages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
---
layout: null
sitemap_omit: true
---
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {% for post in site.posts %}
    {% unless post.published == false %}
    <url>
      <loc>{{ site.url }}{{ post.url }}</loc>
      {% if post.lastmod %}
        <lastmod>{{ post.lastmod | date_to_xmlschema }}</lastmod>
      {% elsif post.date %}
        <lastmod>{{ post.date | date_to_xmlschema }}</lastmod>
      {% else %}
        <lastmod>{{ site.time | date_to_xmlschema }}</lastmod>
      {% endif %}
      {% if post.sitemap.changefreq %}
        <changefreq>{{ post.sitemap.changefreq }}</changefreq>
      {% else %}
        <changefreq>monthly</changefreq>
      {% endif %}
      {% if post.sitemap.priority %}
        <priority>{{ post.sitemap.priority }}</priority>
      {% else %}
        <priority>0.7</priority>
      {% endif %}
    </url>
    {% endunless %}
  {% endfor %}
  {% for page in site.pages %}
    {% unless page.sitemap_omit or page.name == "feed.xml" %}
    <url>
      <loc>{{ site.url }}{{ page.url | remove: "index.html" }}</loc>
      {% if page.lastmod %}
        <lastmod>{{ page.lastmod | date_to_xmlschema }}</lastmod>
      {% elsif page.date %}
        <lastmod>{{ page.date | date_to_xmlschema }}</lastmod>
      {% else %}
        <lastmod>{{ site.time | date_to_xmlschema }}</lastmod>
      {% endif %}
      {% if page.sitemap.changefreq %}
        <changefreq>{{ page.sitemap.changefreq }}</changefreq>
      {% else %}
        <changefreq>weekly</changefreq>
      {% endif %}
      {% if page.sitemap.priority %}
        <priority>{{ page.sitemap.priority }}</priority>
      {% else %}
        <priority>0.9</priority>
      {% endif %}
    </url>
    {% endunless %}
  {% endfor %}
</urlset>

  1. Now, for front-matter use
1
2
3
4
sitemap_omit: boolean
sitemap:
  priority: 0.1 - 1.0
  changefreq: 'daily' or 'weekly' or 'monthly' or 'yearly'
  1. Atlast, add new default scope in the _config.yml file to exclude all the js/css assets.
1
2
3
4
- scope:
    path: "assets/**"
  values:
    sitemap_omit: true
  1. Voilà 🎉

Verfify the changes by visiting basename/sitemap.xml

This post is licensed under CC BY 4.0 by the author.

Deploying MERN app on AWS with NGINX and PM2

Git Cheat Sheet