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

gridsome-plugin-algolia

v2.1.4

Published

A gridsome plugin to index objects to Algolia

Downloads

448

Readme

npm version

Gridsome plugin Algolia

A gridsome plugin to index objects to Algolia

Ported from gatsby-plugin-algolia

You can specify a list of collections to run and how to transform them into an array of objects to index. When you run gridsome 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 of gridsome new

BREAKING CHANGES FROM VERSION 1.x: Read Below

Install

  • yarn add gridsome-plugin-algolia
  • npm install gridsome-plugin-algolia -S

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

// gridsome-config.js

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
  },
];

module.exports = {
  plugins: [
    {
      use: `gridsome-plugin-algolia`,
      options: {
        appId: process.env.ALGOLIA_APP_ID,
        apiKey: process.env.ALGOLIA_ADMIN_KEY,
        collections,
        chunkSize: 10000, // default: 1000
        enablePartialUpdates: true, // default: false
      },
    },
  ],
};

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 and make sure matchFields is correct for every collection.

Migrating from Version 1 to Version 2

The contentTypeName field in collections has been replaced in favor of query and transformer. This is to allow greater control over what data you want to fetch from GraphQL before indexing to Algolia.

To migrate the least you should do is the following:

  1. Remove the contentTypeName property
  2. Add the query property containing a plain graphql query to fetch the data you need
  3. Add the transformer property with a function as value to map the result to a set of items. (Note: The itemFormatter function will still be called)

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
      }
    }