How do I also generate a sitemap for my facets?

I am using algolia-sitemap to generate a loc for each of my record types, and this is seemingly working just fine.

My site also supports faceted search which can be specified with query string parameter, and I would like to be able to instruct the Google Bot on these URL’s as well. The instructions at https://github.com/algolia/algolia-sitemap seem to suggest this is possible.

An example of a faceted search URL for my site looks like:

https://www.controversiesofscience.com/?facets=Controversy-Cards

I have all of these facet strings saved in an array, which I can easily map over. The doc’s seem to suggest that I can do this as so:

algoliaSitemap({
	algoliaConfig,
	sitemapLoc: 'https://www.controversiesofscience.com/sitemaps',
	outputFolder: 'sitemaps',
	params: {
		facets: facets.map(category =>
			getUrlFromCategoryLabel(category.text))
	},
	hitToParams

}).then(() => {

However, when I run this script, the final sitemap does not include any of these facets query string parameters. There is no script error, but also no results. It’s basically ignored.

My first question is: What am I doing wrong here?

And related to that is: Do I need to generate a 2nd sitemap? I am seeing that Google supports this, but it’s not clear from the Algolia doc’s how to add in a 2nd sitemap for the same site.

Hey there!

Could you add the complete code snippet, it seems like it’s missing parts :slight_smile:

The example here is more how to only add one per category from the Algolia data. In the example we didn’t consider yet what would happen if you want both a configuration for all hits, but also for all categories. What you can do is this:

const algoliaSitemap = require('algolia-sitemap');

const algoliaConfig = {
  appId: 'APP_ID',
  apiKey: 'API_KEY',
  indexName: 'NAME',
};

const alreadyAdded = {};

function getCategories({ category }) {
  if (!category || !category.length || category.some(c => alreadyAdded[c])) {
    return false;
  }
  category.forEach(c => (alreadyAdded[c] = c));
  return category.map(c => ({
    loc: `https://yoursite.com/category/${c}`,
  }));
}

const getDetail = ({ objectID }) => ({
  loc: `https://yoursite.com/detail/${objectID}`,
  lastmod: new Date().toISOString(),
  priority: Math.random(),
});

function hitToParams(hit) {
  const categories = getCategories(hit);
  const detail = getDetail(hit);
  return categories.concat(detail);
}

algoliaSitemap({
  algoliaConfig,
  sitemapLoc: 'https://yoursite.com/sitemaps',
  outputFolder: 'sitemaps',
  hitToParams,
})
  .then(() => {
    console.log('Done generating sitemaps'); // eslint-disable-line no-console
  })
  .catch(console.error); // eslint-disable-line no-console

Does that help you?