2

So I'm currently implementing sitemaps for a website that features multiple store fronts.

I used nuxt sitemap module to generate a /sitemap/sitemap.xml file for the static pages, like homepage, terms and conditions and privacy policy.

Now the website also contains dynamic routes for every store front, for example: mysite.com/store1 & mysite.com/store2

The current task is to create a /sitemap.xml for each store, so the end result is something like: mysite.com/store1/sitemap.xml

This sitemap will contain everything related to the store, including dynamic subroutes of each product.

I'm currently not aware of any possible way to do this, and I've searched a lot but I couldn't find anything on this. Any ideas?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Sammi
  • 286
  • 1
  • 3
  • 11

2 Answers2

3

You can create a sitemap index as follows:

{
  sitemap: {
    hostname: 'https://example.com',
    path: '/sitemap.xml',
    sitemaps: [
      {
        path: '/sitemap/sitemap.xml',
      },
      {
        path: '/store1/sitemap.xml',
        exclude: ['/**'],
        routes: () => { /* return array of url for store #1 */ }
      },
      {
        path: '/store2/sitemap.xml',
        exclude: ['/**'],
        routes: () => { /* return array of url for store #2 */ }
      }
    ]
  }
}

You will have this result:

  • /sitemap.xml => the sitemap index (the entry point for crawlers)
  • /sitemap/sitemap.xml => all static routes
  • /store1/sitemap.xml => only dynamic routes for store #1
  • /store2/sitemap.xml => only dynamic routes for store #2
Nicolas Pennec
  • 7,533
  • 29
  • 43
  • 4
    Looks great, but the main issue is, the number of store entries (store1, store2, myStore3...) is unknown and they increase/decrease frequently, with different random names as well. @Nicolas Pennec – Sammi Jun 11 '20 at 11:14
  • 1
    Be sure to add this: exclude: ['/**'], because static routes are merged with dynamic paths... – Hosein Mar 18 '22 at 02:18
3

Try this, it worked for me

  sitemap: {
    path: '/sitemap.xml',
    hostname: process.env.BASE_URL,
    cacheTime: 1000 * 60 * 15,
    gzip: true,
    generate: false,
    sitemaps: [
      {
        path: '/sitemap/sitemap.xml',
      },
      {
        path: '/store1/sitemap.xml',
        exclude: [],
        routes: async () => {
            let apiUrl = 'your site url' // or API url
            const { data } = await axios.get(`${apiUrl}store1`)
            return data.data.map(v => `/${v.id}`)
          }
      },
      {
        path: '/store2/sitemap.xml',
        exclude: [],
        routes: async () => {
            let apiUrl = 'your site url' // or API url
            const { data } = await axios.get(`${apiUrl}store2`)
            return data.data.map(v => `/${v.id}`)
          }
      },
      {
        path: '/store3/sitemap.xml',
        exclude: [],
        routes: async () => {
            let apiUrl = 'your site url' // or API url
            const { data } = await axios.get(`${apiUrl}store3`)
            return data.data.map(v => `/${v.id}`)
          }
      },
    ]
  }
Jimmy Kiarie
  • 51
  • 1
  • 9
  • 2
    Thank you @Jimmy Kiarie but what if I don't know how many stores are there and what their names are? There are lots of stores with dynamic naming (not store1, store2, store3 - this was an example sorry) – Sammi Nov 18 '20 at 22:30