[question] Is it possible to create paginated sitemap indexes?

samicodesit
5
samicodesit
commented 3 months ago

The project I'm working on is a website that has several stores, and each store has multiple products

I want to create:

  1. a basic /sitemap.xml for static routes (about us, privacy policy, etc..)
  2. sitemap index(es) to generate a sitemap for each store. The website contains a lot of stores, so here I need something like
    /sitemap_index.xml then /sitemap_index-2.xml and so on
  3. sitemap for each store to list urls of its products /:store/sitemap.xml which surprisingly won't need to be paginated due to some limits we apply to product quantities for stores

Now 1 and 3 were easily doable, but for 2, I had to fetch all stores in the system into one sitemap index, which I suppose is not desirable.

Here's my configuration:

sitemap: getSitemapConfig();

function getSitemapConfig() {
  const stores = await getStores({ limit: 8000 });

  return [
    {
      path: '/sitemap.xml',
      i18n: true,
    },
    {
      path: 'sitemap_index.xml',
      sitemaps: stores.map(store => ({
        path: `/${store.name}/sitemap.xml`,
        exclude: ['/**'],
        routes: async () => {
          const { products } = await getProducts();
          return products.map(product => `/${store.name}/${product.sku}`);
        },
      })),
    },
  ];
}

Is there any possible way to achieve such paginated indexes? Thank you lots.

0
NicoPennec
2.2k
NicoPennec
commented 3 months ago

hi @samicodesit

A SitemapIndex can include up to 50,000 Sitemaps and a Sitemaps 50,000 URLs.

So if i understood well, you can do that:

const chunk = require('lodash.chunk')


// ...

function getSitemapConfig() {
  const sitemap = {
    path: "/sitemap.xml",
    i18n: true,
  };

  const limit = 100; // limit of store by sitemapindex

  const sitemapindexes = chunk(stores, limit).maps((page, index) => ({
    path: `sitemap_index_${index}.xml`,
    sitemaps: page.map((store) => ({
      path: `/${store.name}/sitemap.xml`,
      exclude: ["/**"],
      routes: async () => {
        const { products } = await getProducts();
        return products.map((product) => `/${store.name}/${product.sku}`);
      },
    })),
  }));

  return [sitemap, ...sitemapindexes];
}

In addition, currently the sitemap module cannot auto-split a large sitemap (see feature request https://github.com/nuxt-community/sitemap-module/issues/79)

1
samicodesit
5
samicodesit
commented 3 months ago

Awesome take @NicoPennec and thank you for the quick reply!

1
Informations
QuestionUnresolved
#c123 - Created 3 months ago