Automatically generate or serve dynamic sitemap.xml for Nuxt.js projects!
Module based on the awesome sitemap package ❤️
- Add
@nuxtjs/sitemap
dependency using yarn or npm to your project - Add
@nuxtjs/sitemap
module tonuxt.config.js
modules: [
'@nuxtjs/sitemap'
]
- Add additional options to
sitemap
section ofnuxt.config.js
to override defaults
sitemap: {
path: '/sitemap.xml',
hostname: 'https://example.com',
cacheTime: 1000 * 60 * 15,
gzip: true,
generate: false, // Enable me when using nuxt generate
exclude: [
'/secret',
'/admin/**'
],
routes: [
'/page/1',
{
url: '/page/2',
changefreq: 'daily',
priority: 1,
lastmodISO: '2017-06-30T13:30:00.000Z'
}
]
}
The exclude
parameter is an array of glob patterns to exclude static routes from the generated sitemap.
The routes
parameter follows the same way than the generate
configuration.
See as well the routes examples below.
- Default:
/sitemap.xml
Where serve/generate sitemap file
- Default:
hostname()
for generate mode- Dynamically based on request url for middleware mode
This values is mandatory for generation sitemap file, and you should explicitly provide it for generate mode.
- Default:
false
Generates static sitemap file during build/generate instead of serving using middleware.
- Default:
1000 * 60 * 15
(15 Minutes)
Defines how frequently should sitemap routes being updated.
This option is only effective when generate
is false
.
Please note that after each invalidation, routes
will be evaluated again. (See routes section)
- Default:
false
Enable the creation of the .xml.gz
sitemap compressed with gzip.
Dynamic routes are ignored by the sitemap module.
Example:
-| pages/
---| index.vue
---| users/
-----| _id.vue
If you want the module to add routes with dynamic params, you need to set an array of dynamic routes.
We add routes for /users/:id
in nuxt.config.js
:
sitemap: {
routes: [
'/users/1',
'/users/2',
'/users/3'
]
}
nuxt.config.js
const axios = require('axios')
module.exports = {
sitemap: {
routes () {
return axios.get('https://jsonplaceholder.typicode.com/users')
.then(res => res.data.map(user => '/users/' + user.username))
}
}
}
nuxt.config.js
const axios = require('axios')
module.exports = {
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)
}
}
}