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: ''

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 => {

then, in the sitemap config, I declare which array to use:

sitemap: {
  hostname: '',
  routes: routes

That's it.