[Hexo] Use hexo-generator-sitemap plugin to generate sitemap.xml automatically in Hexo

hexo-generator-sitemap

Hexo is a fast, simple & powerful blog framework. It use plugins to provide features powerful APIs for limitless extensibility. Various plugins are available to support most template engines (EJS, Pug, Nunjucks, and many others). Easily integrate with existing NPM packages (Babel, PostCSS, Less/Sass, etc).

hexo-generator-sitemap plugin will generate sitemap files for Hexo pages, posts, categories, tags.

Installation

Use npm to install hexo-generator-sitemap package.

1
2
3
$ npm install hexo-generator-sitemap --save

$ hexo clean

Configuration

It will generate sitemap.xml in the public folder by default after hexo-generator-sitemap installation.

You can configure this plugin in ./_config.yml.

1
2
3
4
5
6
sitemap:
path: sitemap.xml
template: ./sitemap_template.xml
rel: false
tags: true
categories: true

The follow is the options:

  • path - Sitemap path. (Default: sitemap.xml)

  • template - Custom template path. This file will be used to generate sitemap.xml. (See default template)

  • rel - Add rel-sitemap to the site’s header. (Default: false)

  • tags - Add site’s tags. (Default: true)

  • categories - Add site’s categories. (Default: true)

Exclude Posts/Pages

Add sitemap: false to the post/page’s front matter in order to exclude from sitemap.

1
2
3
4
5
---
title: lorem ipsum
date: 2020-01-02
sitemap: false
---

Finally, you can visit <Your Site Url>/sitemap.xml in the browser, and check whether the sitemap.xml works correctly after Hexo restarted or regenerated.

FAQs

no such file or directory, open ‘./sitemap_template.xml’

1
2
3
4
5
6
$ hexo g
...
INFO Start processing
FATAL {
err: Error: ENOENT: no such file or directory, open './sitemap_template.xml'
...

Check hexo and hexo-generator-sitemap version:

1
2
hexo 6.0.0
hexo-generator-sitemap 3.0.1

Comment template: ./sitemap_template.xml within ./_config.yml, and run hexo g again.

1
2
3
4
5
6
sitemap:
path: sitemap.xml
- template: ./sitemap_template.xml
rel: false
tags: true
categories: true

References

[1] GitHub - hexojs/hexo-generator-sitemap: Sitemap generator for Hexo. - https://github.com/hexojs/hexo-generator-sitemap

[2] Build and submit a sitemap  |  Google Search Central  |  Google Developers - https://developers.google.com/search/docs/advanced/sitemaps/build-sitemap

[3] Hexo - https://hexo.io/