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

netlify-plugin-graphql-algolia

v1.0.4

Published

Netlify Build plugin - An Advanced Algolia Indexer that uses GraphQL queries and transformers to customize one or many indexes and optionally upserting only changed records or full indexes

Downloads

6

Readme

Netlify Plugin - GraphQL Algolia

A versatile Algolia search plugin to index objects to Algolia as part of the Netlify Build process

A ported version of gridsome-plugin-algolia which is originally forked from gatsby-plugin-algolia

This script has been converted from a Gridsome plugin to an ES6 Module to be run as a Netlify postbuild process

The algoliasearch API methods in this script have been updated to conform to the Version 4 library and has removed the deprecated methods which are contained in the original plugin. Also Promise.allSettled methods have been used to ensure all jobs are completed before processing which would previously cause intermittant errors

You can specify a list of collections to run and how to transform them into an array of objects to index. When you run netlify build, it will publish those to Algolia.

Here we have an example with some data that might not be very relevant, but will work with the default configuration.

Install

  • npm install netlify-plugin-graphql-algolia -S

Add it to your netlify.toml file.


[[plugins]]

package = "netlify-plugin-graphql-algolia"
  
# All inputs are optional, so you can omit this section.

# Defaults are shown below.

[plugins.inputs]

# Specifies whether to enable partial updates for the Algolia index. 
# When set to `true`, only modified or new items are updated in the index, 
# enhancing efficiency and reducing operational load.

enablePartialUpdates = true

# Determines the size of each chunk of data sent to Algolia for indexing. 
# A larger chunk size can improve indexing efficiency but may require more 
# memory. The optimal value depends on the size and complexity of your data.

chunkSize = 1000

You also require a mandatory algolia.config.js in the root of your project which exports a collections array. See the Setup section on the syntax of this configuration file.

Setup

First add credentials to a .env file, which you won't commit. If you track this in your file, and especially if the site is open source, you will leak your admin API key. This would mean anyone is able to change anything on your Algolia index.

// DEVELOPING: .env.development
// BUILDING: .env.production

ALGOLIA_APP_ID=XXX
ALGOLIA_ADMIN_KEY=XXX
ALGOLIA_INDEX_NAME=XXX

Usage

// algolia-config.js

export const collections = [
  {
    query: `{
      allBlogPost {
        edges {
          node {
            id
            title
            slug
            modified
          }
        }
      }
    }`,
    transformer: ({ data }) => data.allBlogPost.edges.map(({ node }) => node),
    indexName: process.env.ALGOLIA_INDEX_NAME || 'posts', // Algolia index name
    itemFormatter: (item) => {
      return {
        objectID: item.id,
        title: item.title,
        slug: item.slug,
        modified: String(item.modified)
      }
    }, // optional
    matchFields: ['slug', 'modified'], // Array<String> required with PartialUpdates
  },
];

Partial Updates

By default all items will be reindexed on every build. To enable only indexing new, changed and deleted items, set enablePartialUpdates to true in netlify.toml and make sure matchFields in the collections is correct for every collection.

QnA

Q Partial updates not working? All items being reindexed everytime.

A

  • Make sure that the fields you use to compare are either Strings or Numbers. Dates for example are converted to String when pushed to Algolia so they won't match unless you first convert the Date to a string eg.
  • Make sure each object has a unique id that you map to objectID
    itemFormatter: (item) => {
      return {
        objectID: item.id, // Unique id
        title: item.title,
        slug: item.slug,
        modified: String(item.modified) // Date converted to string
      }
    }