nuxt-community / sitemap-module

Sitemap Module for Nuxt 2
https://sitemap.nuxtjs.org
MIT License
690 stars 128 forks source link

Disable using `nuxt.options.sitemap = false` in nuxt hook does not work #183

Closed wlarch closed 3 years ago

wlarch commented 3 years ago

Disable using nuxt.options.sitemap = false in nuxt hook does not work :

this.nuxt.hook('ready', async nuxt => {
        nuxt.options.sitemap = false;
});

Unfortunately, the sitemap.xml is still generated :

ℹ Generating sitemaps ✔ Generated /sitemap.xml


Cannot create issue using suggested link because of required version field : image

NicoPennec commented 3 years ago

Hi @wlarch,

The sitemap module is initialized before the nuxt "ready" state, so you set the falsy option too late in the nuxt lifecycle.

wlarch commented 3 years ago

@NicoPennec Thanks, I have tried different hooks but can't programmatically disable the sitemap generation. Which Nuxt hook would be adequate ?

NicoPennec commented 3 years ago

@wlarch Why by Nuxt hook ? The sitemap configuration can be a function, so you can disable it programmatically:

  sitemap: () => {
    if (condition) {
      return false
    } else {
      return {
        hostname: 'https://example.com',
        gzip: true,
      }
    }
  },
wlarch commented 3 years ago

@NicoPennec We have built a custom module for generating static sites using incremental builds. Most of the time we generate full builds (all routes) with sitemap, but we also generate partial builds (single routes) without generating the sitemap. This is why I am trying to set sitemap : false as our condition to do so is not directly accessible from the nuxt.config.js file. It is provided using cli params, example : yarn generate --partial-build

I have tried using the modules:before hook with no luck.

Still I will try to disable it as you mention and see if it can work that way.

NicoPennec commented 3 years ago

@wlarch I have tried with modules:before hook, and it disables the sitemap module on nuxt generate:

  hooks: {
    modules: {
      before(moduleContainer, options) {
        console.log('hooks - modules.before')
        moduleContainer.options.sitemap = false
      },
    },
  },
wlarch commented 3 years ago

Thanks, I suppose this hook cannot be called within a custom module. I was trying the following :

this.nuxt.hook('modules:before', async (moduleContainer, options) = {
  console.log('modules:before called');
});

My custom generate.js module, overwrites Nuxt options, where I want to disable the sitemap module for partial deployments. I wonder if I can call modules:before hook anywhere but in nuxt.config.js ?

wlarch commented 3 years ago

With the good help of @danielroe, I was able to archive my goal by conditionally setting sitemap to false from nuxt.config.js file :

const config = {
    // The nuxt options/config object. (...)
};

// Conditionnally exclude sitemap generation if its a partial build
if (process.argv.indexOf('--routes') !== -1) {
    config.sitemap = false;
}

module.exports = config;

It also works great if done using the modules:before hook as stated above :

hooks: {
    modules: {
        before(moduleContainer) {
            // Conditionnally exclude sitemap generation if its a partial build
            if (process.argv.indexOf('--routes') !== -1) {
                console.info('Disabling sitemap generation for partial build.');
                moduleContainer.options.sitemap = false;
            }
        },
    },
},

As the name suggests I cannot call modules:before hook within a custom module as the ModuleContainer was already loaded.

NicoPennec commented 3 years ago

@wlarch a typo in your last snippet? moduleContainer.options.sitemap.sitemap => moduleContainer.options.sitemap

wlarch commented 3 years ago

@NicoPennec Corrected for future references :)