Skip to main content
GOHUGO
Sitemap

Sitemap

This is a Hugo theme component with layouts to add a configurable sitemap to your website. Hugo itself has internal templates that add sitemaps, but this component extends this by providing setup options per page and keeping up-to-date with current SEO practices.

Installation

Add the following lines to your hugo.toml:

1[module]
2[[module.imports]]
3    path = 'github.com/davidsneighbour/hugo-modules/modules/sitemap'
1module:
2  imports:
3  - path: github.com/davidsneighbour/hugo-modules/modules/sitemap
1{
2   "module": {
3      "imports": [
4         {
5            "path": "github.com/davidsneighbour/hugo-modules/modules/sitemap"
6         }
7      ]
8   }
9}

Usage

This module works out of the box and there is no need for any configuration. Once you ran hugo you can find the file sitemap.xml in your public directory. This is the file you want to submit to search engines.

If you are using the Robots component, then your resulting robots.txt includes a pointer to the sitemap file as well.

Exclude a page from sitemap

To exclude a specific page from all sitemaps add it to the config variable in the frontmatter of that page:

1[config]
2  sitemap = false
1config:
2  sitemap: false
1{
2   "config": {
3      "sitemap": false
4   }
5}
Options
Option Type Default Description
sitemap boolean true include this page in the sitemap

Without any configuration the default is true, meaning to include any page into the sitemap.

Global sitemap configuration

Add/edit global defaults in hugo.toml:

1[params]
2  [params.dnb]
3    [params.dnb.sitemap]
4      enabled = true
1params:
2  dnb:
3    sitemap:
4      enabled: true
1{
2   "params": {
3      "dnb": {
4         "sitemap": {
5            "enabled": true
6         }
7      }
8   }
9}

You can edit the following additional configuration parameters:

  • full (boolean, default false) - show priority and changefreq tags (ignored by Google)
  • format (string, default “2006-01-02”) - date format for lastmod tag

DEPRECATED: Frontmatter robotsdisallow from earlier hugo-robots versions did result in the page being omitted from the sitemap. This is deprecated, but currently still supported. The module will echo a note on CLI about this.

HTML Sitemap

This module also provides an HTML sitemap, that you can include via shortcode:

1{{< sitemap >}}

Add the sitemap as shortcode {{< sitemap >}} anywhere you want.

A sample implementation can be found on kollitsch.dev. The following configuration was used:

 1[params]
 2  [params.dnb]
 3    [params.dnb.sitemap]
 4      [params.dnb.sitemap.htmlmap]
 5[[params.dnb.sitemap.htmlmap.item]]
 6          label = 'Blog Posts'
 7          section = 'blog'
 8          type = '.Type'
 9[[params.dnb.sitemap.htmlmap.item]]
10          label = 'GoHugo Components by DNB'
11          section = 'components'
12          sortdirection = 'ASC'
13          sortvalue = '.Title'
14          type = '.Type'
15[[params.dnb.sitemap.htmlmap.item]]
16          label = 'Tags'
17          section = 'tags'
18          selection = 'in-pages'
19          sortdirection = 'ASC'
20          sortvalue = '.Title'
21          type = '.Type'
22[[params.dnb.sitemap.htmlmap.item]]
23          label = 'Other pages'
24          section = ['blog', 'tags', 'components']
25          selection = 'not-in'
26          sortdirection = 'ASC'
27          sortvalue = '.Title'
28          type = '.Type'
 1params:
 2  dnb:
 3    sitemap:
 4      htmlmap:
 5        item:
 6        - label: Blog Posts
 7          section: blog
 8          type: .Type
 9        - label: GoHugo Components by DNB
10          section: components
11          sortdirection: ASC
12          sortvalue: .Title
13          type: .Type
14        - label: Tags
15          section: tags
16          selection: in-pages
17          sortdirection: ASC
18          sortvalue: .Title
19          type: .Type
20        - label: Other pages
21          section:
22          - blog
23          - tags
24          - components
25          selection: not-in
26          sortdirection: ASC
27          sortvalue: .Title
28          type: .Type
 1{
 2   "params": {
 3      "dnb": {
 4         "sitemap": {
 5            "htmlmap": {
 6               "item": [
 7                  {
 8                     "label": "Blog Posts",
 9                     "section": "blog",
10                     "type": ".Type"
11                  },
12                  {
13                     "label": "GoHugo Components by DNB",
14                     "section": "components",
15                     "sortdirection": "ASC",
16                     "sortvalue": ".Title",
17                     "type": ".Type"
18                  },
19                  {
20                     "label": "Tags",
21                     "section": "tags",
22                     "selection": "in-pages",
23                     "sortdirection": "ASC",
24                     "sortvalue": ".Title",
25                     "type": ".Type"
26                  },
27                  {
28                     "label": "Other pages",
29                     "section": [
30                        "blog",
31                        "tags",
32                        "components"
33                     ],
34                     "selection": "not-in",
35                     "sortdirection": "ASC",
36                     "sortvalue": ".Title",
37                     "type": ".Type"
38                  }
39               ]
40            }
41         }
42      }
43   }
44}

The parameters are as follows:

  • selection - Type of page selection.
    • in-regular (default, just omit the parameter) - selects the pages from the site.RegularPages collection.
    • in-pages - selects from site.Pages
    • not-in - selects all pages NOT in site.Pages
  • type - field option for the page selection
  • section - value option for the page selection
  • label - Label for the section headline
  • sortvalue - if you wish to sort the selection set this to the field to sort by
  • sortdirection (default ASC) - direction to sort in, ASC or DESC

Page selection:

Sample:

1[dnb]
2  [dnb.sitemap]
3    [dnb.sitemap.htmlmap]
4[[dnb.sitemap.htmlmap.item]]
5        section = 'tags'
6        selection = 'in-pages'
7        type = '.Type'
1dnb:
2  sitemap:
3    htmlmap:
4      item:
5      - section: tags
6        selection: in-pages
7        type: .Type
 1{
 2   "dnb": {
 3      "sitemap": {
 4         "htmlmap": {
 5            "item": [
 6               {
 7                  "section": "tags",
 8                  "selection": "in-pages",
 9                  "type": ".Type"
10               }
11            ]
12         }
13      }
14   }
15}

Results in the pages being selected via:

1{{- $pages = (where site.Pages .Type "tags") -}}

You can add a sitemap also to any template as a partial:

1{{- $config := site.Params.dnb.sitemap.htmlmap -}}
2{{- $sitemapdata := (dict "config" $config) -}}
3{{- partialCached "sitemap.html" $sitemapdata $sitemapdata -}}

The above is the current shortcode, ehm, code. You can just use the global configuration or build your own configuration dictionary to fill your sitemap. This makes the sitemap also usable to just show a collection of pages anywhere:

1{{ $config := dict "config" (dict "item" (dict
2    "type" ".Type"
3    "section"  "components"
4    "label" "GoHugo Components by DNB"
5    "sortvalue" ".Title"
6    "sortdirection" "ASC"
7)) }}
8{{- partialCached "sitemap.html" $config $config -}}

This template would show all items in my content/components section, sorted by title.

Back to top
Back Forward