Add sitemap to Nuxt with export
Posted on July 10, 2020
If you are using the new export
function of Nuxt with the @nuxt/content
module, which generates automatically all the routes with an internal crawler, maybe you're facing an issue in case you'd like to add the sitemap
to your generated website.
Previously, with the generate
command, you were forced to create the routes array by hand, therefore, the @nuxtjs/sitemap
configuration were already set with the default settings because it uses the generate
property to know which route to consider:
generate: {
routes: myRoutes
},
sitemap: {
hostname: 'https://www.example.com'
}
With the new export
, the generate
field is not set anymore, thus, here my solution.
Basically, I populate the routes array using a hook of the content
module:
hooks: {
'content:file:beforeInsert': d => {
routes.push(d.path)
}
}
then, in the sitemap config, I declare which array to use:
sitemap: {
hostname: 'https://www.example.com',
routes: routes
}
That's it.