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-index-update

v0.2.1

Published

Uploads an Algolia index from a local folder

Downloads

5

Readme

Algolia Index Update

A command-line utility for web sites using Algolia (for site indexing and search) to merge an updated index with an Algolia project data source. The module is based on the process outlined in Adding Algolia Search to Eleventy and Netlify and uses the Algolia-indexing module as described in the article.

In the article, @cfjedimaster shows how to use generate the Algolia index from an Eleventy site and use Netlify Functions to merge the updated index and upload it to Algolia during a Netlify build process. In the article he also mentioned a 10 minute limit for executing Cloud Functions in Netlify, and that scared me a little bit. Since I'm comfortable using a local generation and deployment process for my site's Algolia index, I decided to build this command-line module to handle the task for me.

The module in action

Here's a link to the NPM project:

Note: You don't have to use Eleventy to use this module, all it cares about is local access to an Algolia site index and the Algolia project and account API keys needed to merge the local index with the Algolia project data source.

To use the module, you must have:

  • An Algolia Application project and data source defined in Algolia.
  • The Algolia Application ID for your site's Algolia project.
  • The Admin API keys for your Algolia account.
  • The name of the Algolia project's index.

Installation

To install the module as a dependency in the application project, open a terminal window or command prompt and execute the following command:

npm install algolia-index-update --save-dev 

This adds an algolia-idxup command you can execute through the standard npm run build process at the command-line or in the project's package.json file.

To install the module globally, open a terminal window or command prompt and execute the following command:

npm install algolia-index-update -g

This adds an algolia-idxup command you can execute from any terminal window or command prompt on the system.

Usage

To use the module, you must first define the environment variables listed in the following table:

| Environment Variable Name | Algolia Project Key or Value | | ------------------------- | ------------------------------ | | ALGOLIA_API_KEY | Algolia account Admin API key | | ALGOLIA_APP_ID | Algolia project Application ID | | ALGOLIA_IDX_NAME | Algolia project Index name |

The module reads these environment variables and uses their values when merging the updated index with the Algolia cloud project. It uses environment variables to enable hiding the values from the project's source code in GitHub (or some other source code repository) and allows the module to run in cloud hosting environments like Netlify.

If you use Algolia for multiple sites, you need a way to distinguish the app id and index names, so you can create a second set of credentials and append a prefix to the environment variables. For example, for my Random Errors site, I use a prefix of RE_ and I setup my variable names like this:

| Environment Variable Name | Algolia Project Key or Value | | ------------------------- | ------------------------------ | | RE_ALGOLIA_API_KEY | Algolia account Admin API key | | RE_ALGOLIA_APP_ID | Algolia project Application ID | | RE_ALGOLIA_IDX_NAME | Algolia project Index name |

There are two options for executing the module:

algolia-idxup <path_to_the_algolia_index>

or

algolia-idxup <path_to_the_algolia_index> [environment_variable_prefix]

Lets see some examples:

To process the Algolia index at _site/_data with no prefix on the environment variable names, use the following:

algolia-idxup _site/algolia.json

To use the prefixed environment variables described in the second table, use the following

algolia-idxup _site/algolia.json RE_

Alternatively, when you have more than one Algolia project, you can provide the Algolia credentials via a JSON file using the -f or --file flag coupled with the path to the JSON file:

algolia-idxup _site/algolia.json RE_ -f ..\algolia-credentials.json

The algolia-credentials.json file looks like this:

{
  "ALGOLIA_APP_ID": "Root_Algolia_App_ID",
  "ALGOLIA_API_KEY": "Root_Algolia_API_Key",
  "ALGOLIA_IDX_NAME": "Root_Algolia_Idx_name",
  "RE_ALGOLIA_APP_ID": "Random_Errors_App_ID",
  "RE_ALGOLIA_API_KEY": "Random =_Errors_API_Key",
  "RE_ALGOLIA_IDX_NAME": "Random_Errors_Index_Name",
}

Replacing, of course, the properties with the correct values for your different Algolia projects.

The module validates the required environment variables, then confirms that it can locate the index file. With those two components in place, the module clicks and whirs for a while as it completes the index update/merge as shown below.

D:\dev\11ty\random-errors-static>algolia-idxup --default
┌──────────────────────────┐
│                          │
│   Algolia Index Update   │
│                          │
└──────────────────────────┘
Index: : D:\dev\node\algolia-index-update\_site\algolia.json
Processing index for 17 articles.
√ Copying random-errors-main to random-errors-main_tmp
√ Updating settings of random-errors-main_tmp
√ Getting all records from random-errors-main_tmp
√ Moving random-errors-main_tmp to random-errors-main
√ Configuring replicas of random-errors-main
Processing completed in 43.687 seconds

D:\dev\node\algolia-index-update>

If you want to see more output as the command runs, add a -d to the command line:

algolia-idxup _site/algolia.json -d

That's pretty much it. Enjoy!


You can find information on many different topics on my personal blog. Learn about all of my publications at John Wargo Books.

If you find this code useful and feel like thanking me for providing it, please consider <a href="https://www.buymeacoffee.com/johnwargo" target="_blank">Buying Me a Coffee, or making a purchase from my Amazon Wish List.