nuxt-community / sitemap-module

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

Give the possibility to make async call in the filter function #235

Open Dobby85 opened 2 years ago

Dobby85 commented 2 years ago

Hi,

I though it could be nice to make async call in the filter function. For example, in my case, I have one nuxt.config.js for multiple websites. I also use i18n. Each one of my website can decide in which language they would like to be translated. The problem is that now in my sitemap, all static routes are with every locale.

Wht I would like to do is to make an API call in the filter function to get the real used locale for a website and keep only the wanted locale in the sitemap.

I succeed to to it by updating the builder to get it working. I would like to do a pull requests but when I clone the repository and run tests, a lot of eslint and tests errors are displayed. Do you have a guide on how we could collaborate on the project ?

How can I check that my modification does not impact other features ?

Thanks,

maarten2424 commented 2 years ago

Hey Dobby85.

You could overwrite the filter function by registering to the sitemap:generate:before event. There you could attach an asynchronous function to receive data before executing the filter function

  hooks: {
    sitemap: {
      generate: {
        async before(nuxt, sitemapOptions) {
          const excludedRoutes = await getExcludedRoutes()
          sitemapOptions[0].filter = ({ routes }) => {
            return routes.filter((route) => !excludedRoutes.includes(route.url))
          }
        }
      }
    }
  }