Sitemap

Sitemap v1.1.1

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 has additional setup options per page.

Add this module
[[module.imports]]
path = "github.com/davidsneighbour/hugo-sitemap"
disable = false
ignoreConfig = false
ignoreImports = false
Latest Version v1.1.1 (2022-08-23)
Fix to this version
hugo mod get github.com/davidsneighbour/[email protected]

Usage

There is no need to configure anything without having any special needs. Add the module to your repository structure and run it. Once you ran hugo you will find a 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 will have a pointer to the sitemap file as well.

Exclude page from sitemap

Add frontmatter to individual pages with the following setup:

1[config]
2  sitemap = true
1config:
2  sitemap: true
1{
2   "config": {
3      "sitemap": true
4   }
5}

sitemap (boolean): include this page in the sitemap

Add/edit global defaults in config.toml > params or config/_defaults/params.toml:

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

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

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

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

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.