nuxt-community / sitemap-module

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

Ability to set the options.defaults.priority of all subpages to a specific value #192

Closed madebyfabian closed 3 years ago

madebyfabian commented 3 years ago

What problem does this feature solve?

Hello there, I thought of something that I think many users of this package would appreciate. I ran into this issue today.

What's the current problem?

If I use the sitemap package and configure it as following:

...
sitemap: {
  hostname: 'https://example.com'
}

all my pages don't have a <priority> entry in the sitemap.xml. If I do:

...
sitemap: {
  hostname: 'https://example.com',
  defaults: {
    priority: 1
  }
}

all pages get the priority 1, which is not the point of a priority itsself. What I could do now is to define every subpage myself and set the priority to something like 0.80. But then the whole point of the plugin is gone, since I could also just write & update the sitemap.xml myself.

What does the proposed changes look like?

What would be the solution

Maybe to add a config param like prioritySubpages that sets the priority of all subpages to a specific value. So this:

...
sitemap: {
  hostname: 'https://example.com',
  defaults: {
    priority: 1,
    prioritySubpages: 0.8
  }
}

would be parsed into:

...
<url>
  <loc>https://example.com/</loc>
  <priority>1.00</priority>
</url>
<url>
  <loc>https://example.com/subpage1</loc>
  <priority>0.80</priority>
</url>
<url>
  <loc>https://example.com/subpage2/subsubpage1</loc>
  <priority>0.80</priority>
</url>
...
This feature request is available on Nuxt community (#c136)
madebyfabian commented 3 years ago

Or - maybe even better - with a * glob value in the options.routes section, so that you can specify it even more detailled.

themeler commented 3 years ago

On the other hand, you can precisely control priorities using filter function.

Without i18n:

sitemap: {
    filter({ routes }) {
        return routes.map(route => {
            // object containing [routeName]: [priority] pairs
            const priorities: = {
                index: 1,
                about: 0.6,
            }

            // assign priority by route name or default (.5)
            return { ...route, priority: priorities[route.name] || 0.5 }
        })
    },
}

With i18n:

sitemap: {
    i18n: true,
    filter({ routes }) {
        return routes.map(route => {
            const routeName = route.name.replace(/___.{0,}/, '') // remove i18n locale suffix
            // object containing [routeName]: [priority] pairs
            const priorities = {
                index: 1,
                about: 0.6,
            }

            // assign priority by route name without locale suffix or default (.5)
            return { ...route, priority: priorities[routeName] || 0.5 }
        })
    },
}
madebyfabian commented 3 years ago

@themeler Thanks for pointing this out! This does work great! Haven't found this in the docs, but here is it: https://sitemap.nuxtjs.org/usage/sitemap-options#filter-optional---function

I'll close this issue now, thanks again.