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
[module]
[[module.imports]]
path = 'github.com/davidsneighbour/hugo-modules/modules/sitemap'
module:
imports:
- path: github.com/davidsneighbour/hugo-modules/modules/sitemap
{
"module": {
"imports": [
{
"path": "github.com/davidsneighbour/hugo-modules/modules/sitemap"
}
]
}
}
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:
[config]
sitemap = false
config:
sitemap: false
{
"config": {
"sitemap": false
}
}
| 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
:
[params]
[params.dnb]
[params.dnb.sitemap]
enabled = true
params:
dnb:
sitemap:
enabled: true
{
"params": {
"dnb": {
"sitemap": {
"enabled": true
}
}
}
}
You can edit the following additional configuration parameters:
- full (boolean, default false) - show
priority
andchangefreq
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:
{{< sitemap >}}
Add the sitemap as shortcode {{< sitemap >}}
anywhere you want.
A sample implementation can be found on kollitsch.dev . The following configuration was used:
[params]
[params.dnb]
[params.dnb.sitemap]
[params.dnb.sitemap.htmlmap]
[[params.dnb.sitemap.htmlmap.item]]
label = 'Blog Posts'
section = 'blog'
type = '.Type'
[[params.dnb.sitemap.htmlmap.item]]
label = 'GoHugo Components by DNB'
section = 'components'
sortdirection = 'ASC'
sortvalue = '.Title'
type = '.Type'
[[params.dnb.sitemap.htmlmap.item]]
label = 'Tags'
section = 'tags'
selection = 'in-pages'
sortdirection = 'ASC'
sortvalue = '.Title'
type = '.Type'
[[params.dnb.sitemap.htmlmap.item]]
label = 'Other pages'
section = ['blog', 'tags', 'components']
selection = 'not-in'
sortdirection = 'ASC'
sortvalue = '.Title'
type = '.Type'
params:
dnb:
sitemap:
htmlmap:
item:
- label: Blog Posts
section: blog
type: .Type
- label: GoHugo Components by DNB
section: components
sortdirection: ASC
sortvalue: .Title
type: .Type
- label: Tags
section: tags
selection: in-pages
sortdirection: ASC
sortvalue: .Title
type: .Type
- label: Other pages
section:
- blog
- tags
- components
selection: not-in
sortdirection: ASC
sortvalue: .Title
type: .Type
{
"params": {
"dnb": {
"sitemap": {
"htmlmap": {
"item": [
{
"label": "Blog Posts",
"section": "blog",
"type": ".Type"
},
{
"label": "GoHugo Components by DNB",
"section": "components",
"sortdirection": "ASC",
"sortvalue": ".Title",
"type": ".Type"
},
{
"label": "Tags",
"section": "tags",
"selection": "in-pages",
"sortdirection": "ASC",
"sortvalue": ".Title",
"type": ".Type"
},
{
"label": "Other pages",
"section": [
"blog",
"tags",
"components"
],
"selection": "not-in",
"sortdirection": "ASC",
"sortvalue": ".Title",
"type": ".Type"
}
]
}
}
}
}
}
The parameters are as follows:
selection
- Type of page selection.in-regular
(default, just omit the parameter) - selects the pages from thesite.RegularPages
collection.in-pages
- selects fromsite.Pages
not-in
- selects all pages NOT insite.Pages
type
- field option for the page selectionsection
- value option for the page selectionlabel
- Label for the section headlinesortvalue
- if you wish to sort the selection set this to the field to sort bysortdirection
(defaultASC
) - direction to sort in,ASC
orDESC
Page selection:
Sample:
[dnb]
[dnb.sitemap]
[dnb.sitemap.htmlmap]
[[dnb.sitemap.htmlmap.item]]
section = 'tags'
selection = 'in-pages'
type = '.Type'
dnb:
sitemap:
htmlmap:
item:
- section: tags
selection: in-pages
type: .Type
{
"dnb": {
"sitemap": {
"htmlmap": {
"item": [
{
"section": "tags",
"selection": "in-pages",
"type": ".Type"
}
]
}
}
}
}
Results in the pages being selected via:
{{- $pages = (where site.Pages .Type "tags") -}}
You can add a sitemap also to any template as a partial:
{{- $config := site.Params.dnb.sitemap.htmlmap -}}
{{- $sitemapdata := (dict "config" $config) -}}
{{- 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 dict
ionary to fill your sitemap. This makes the sitemap also usable to just show a collection of pages anywhere:
{{ $config := dict "config" (dict "item" (dict
"type" ".Type"
"section" "components"
"label" "GoHugo Components by DNB"
"sortvalue" ".Title"
"sortdirection" "ASC"
)) }}
{{- partialCached "sitemap.html" $config $config -}}
This template would show all items in my content/components
section, sorted by title.