Sitemap Module
Automatically generate or serve dynamic sitemap.xml for Nuxt.js projects!
📖 Release Notes
Features
- Module based on the awesome sitemap.js package ❤️
- Automatically add the static routes to the sitemap
- Works with all modes (universal, spa, generate)
- For Nuxt 1.x and higher
Setup
modules: [
'@nuxtjs/sitemap'
]
notice: If you use other modules (eg. nuxt-i18n
), always declare the sitemap module at end of array (eg. modules: ['nuxt-i18n', '@nuxtjs/sitemap']
)
{
modules: [
'@nuxtjs/sitemap'
],
sitemap: {
hostname: 'https://example.com',
gzip: true,
exclude: [
'/secret',
'/admin/**'
],
routes: [
'/page/1',
{
url: '/page/2',
changefreq: 'daily',
priority: 1,
lastmodISO: '2017-06-30T13:30:00.000Z'
}
]
}
Configuration
routes
The routes
parameter follows the same way than the generate
configuration.
See as well the routes examples below.
path
(optional)
The URL path of the generated sitemap.
hostname
(optional)
- Default:
sitemap.hostname
value from your nuxt.config.js
build.publicPath
value from your nuxt.config.js
os.hostname()
for generate or spa mode, or dynamically based on request URL (headers.host
) for universal mode
This value is mandatory for generation sitemap file, and you should explicitly provide it for generate or spa mode.
cacheTime
(optional)
- Default:
1000 * 60 * 15
(15 Minutes)
Defines how frequently should sitemap routes being updated.
Please note that after each invalidation, routes
will be evaluated again. (See routes section)
exclude
(optional)
The exclude
parameter is an array of glob patterns to exclude static routes from the generated sitemap.
filter
(optional)
If filter
option is set as a function, all routes will be filtered through it.
Examples:
{
sitemap: {
filter ({ routes, options }) {
if (options.hostname === 'example.com') {
return routes.filter(route => route.locale === 'en')
}
return routes.filter(route => route.locale === 'fr')
}
}
}
{
sitemap: {
filter ({ routes }) {
return routes.map(route => {
route.url = `${route.url}/`
return route
})
}
}
}
gzip
(optional)
Enable the creation of the .xml.gz
sitemap compressed with gzip.
xmlNs
(optional)
Set the XML namespaces by override all default xmlns
attributes in <urlset>
element.
{
sitemap: {
xmlNs: 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'
}
}
xslUrl
(optional)
The URL path of the XSL file to style the sitemap.
trailingSlash
(optional)
Add a trailing slash to each route URL (eg. /page/1
=> /page/1/
)
notice: To avoid duplicate content detection from crawlers, you have to configure an HTTP 301 redirect between the 2 URLs (see redirect-module or nuxt-trailingslash-module).
defaults
(optional)
The defaults
parameter set the default options for all routes.
{
sitemap: {
defaults: {
changefreq: 'daily',
priority: 1,
lastmod: new Date(),
lastmodrealtime: true
}
}
}
See available options: https://github.com/ekalinin/sitemap.js#usage
Routes
By default, the dynamic routes are ignored by the sitemap module.
Nuxt cannot automatically provide this type of complex routes.
Example:
-| pages/
---| index.vue --> static route
---| about.vue --> static route
---| users/
-----| _id.vue --> dynamic route
If you want the module to add any route with dynamic parameters, you have to set an array of dynamic routes.
eg. add routes for /users/:id
in the configuration:
{
sitemap: {
routes: [
'/users/1',
'/users/2',
'/users/3'
]
}
}
Function which returns a Promise
const axios = require('axios')
{
sitemap: {
routes () {
return axios.get('https://jsonplaceholder.typicode.com/users')
.then(res => res.data.map(user => '/users/' + user.username))
}
}
}
Function with a callback
This feature is deprecated. Use a promise-based approach instead.
const axios = require('axios')
{
sitemap: {
routes (callback) {
axios.get('https://jsonplaceholder.typicode.com/users')
.then(res => {
let routes = res.data.map(user => '/users/' + user.username)
callback(null, routes)
})
.catch(callback)
}
}
}
License
MIT License
Contributors