npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

algolia-sitemap

v2.2.2

Published

This is a node library allowing you to generate sitemaps from an Algolia index.

Downloads

38,212

Readme

Algolia sitemap generator

This is a node library allowing you to generate sitemaps from an Algolia index.

Requires node v6+ (make an issue if this is a problem for you).

It will create sitemaps, and a sitemap index in a folder of your choosing (for example /sitemaps). Then you can upload /sitemaps/sitemap-index.xml to Google for good indexing of your pages!

How does it work?

  1. Browse over all entries in an Algolia index
  2. Per 50 000 links, a sitemap.n.xml is generated in the chosen folder (where n is the index)
  3. Once all Algolia data has been browsed over, a final sitemap-index.xml is being generated
  4. Let search engines know about sitemap-index.xml either by letting them know or putting it in robots.txt

This process is a script that should be ran periodically to keep the sitemaps up to date, no "watch" feature has been put in place (yet?)

How to use

First install the module from npm (or with yarn):

$ npm install algolia-sitemap --save[-dev]
$ yarn add algolia-sitemap [--dev]
// import the dependency
const algoliaSitemap = require('algolia-sitemap');

algoliaSitemap({
  algoliaConfig,
  sitemapLoc: 'https://yoursite.com/sitemaps',
  outputFolder: 'sitemaps',
  hitToParams,
});

Where algoliaConfig holds the setup for your index. Make sure that the API key you use has the "browse" capability

// set up your API keys
const algoliaConfig = {
  appId: 'XXXXX',
  apiKey: 'xxxxxx', // make sure the key has "browse" capability
  indexName: 'xxxxxx',
};

And hitToParams is a function that transforms a hit into a parameters object. This function can return an object of type Param, an array of Params or false.

function hitToParams({ objectID, modified, downloadsRatio }) {
  const url = ({ lang, objectID }) =>
    `https://${lang}.yoursite.com/${lang}/detail/${objectID}`;
  const loc = url({ lang: 'en', objectID });
  const lastmod = new Date().toISOString();
  const priority = Math.random();
  return {
    loc,
    lastmod,
    priority,
    alternates: {
      languages: ['fr', 'pt-BR', 'zh-Hans'],
      hitToURL: lang => url({ lang, objectID }),
    },
  };
}

These parameters mean:

/**
 * @typedef {Object} Params
 * @property {string} loc the link of this hit
 * @property {string} [lastmod] the last time this link was modified (ISO8601)
 * @property {number} [priority] the priority you give to this link (between 0 and 1)
 * @property {Object} [alternates] alternative versions of this link (useful for multi-language)
 * @property {Array} [alternates.languages] list of languages that are enabled
 * @property {Array} [images] list of images links related to the hit
 * @property {function} [alternates.hitToURL] function to transform a language into a url of this object
 */

Image Sitemaps

If you want your sitemap to include Google image extensions, return an array for each hit containing objects with the following keys:

/**
 * @typedef {Object} Image
 * @property {string} loc the link of this image
 * @property {string} [title] image title
 * @property {string} [caption] image caption
 * @property {string} [geo_location] geographic location (e.g. 'Limerick, Ireland')
 * @property {string} [license] the link to the image's license
 */

For example:

function hitToParams({
  objectID,
  modified,
  downloadsRatio,
  profilePic,
  coverPhoto,
  name,
}) {
  const url = ({ lang, objectID }) =>
    `https://${lang}.yoursite.com/${lang}/detail/${objectID}`;
  const loc = url({ lang: 'en', objectID });
  const lastmod = new Date().toISOString();
  const priority = Math.random();
  return {
    loc,
    lastmod,
    priority,
    images: [
      {
        loc: `https://media.yoursite.com/images/${profilePic}`,
        title: name,
      },
      {
        loc: `https://media.yoursite.com/images/${coverPhoto}`,
        title: name,
      },
    ],
    alternates: {
      languages: ['fr', 'pt-BR', 'zh-Hans'],
      hitToURL: lang => url({ lang, objectID }),
    },
  };
}

For more information, see https://support.google.com/webmasters/answer/178636?hl=en

Custom queries

You can pass a params parameter to algoliaSitemap. This allows you to narrow down the returned results. For instance, in order to have hitToParams called for every products in the phone category, we could do:

algoliaSitemap({
  algoliaConfig,
  sitemapLoc: 'https://yoursite.com/sitemaps',
  outputFolder: 'sitemaps',
  params: {
    filters: 'category: phone',
  },
  hitToParams,
});

Note that a query can also be passed to params.

Examples

You can also take a look at examples folder for how it works.

License

MIT