Giter VIP home page Giter VIP logo

gridsome-plugin-algolia's Introduction

npm version

Gridsome plugin Algolia

A gridsome search 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
      }
    }

gridsome-plugin-algolia's People

Contributors

u12206050 avatar cprestoncowart avatar darrenmothersele avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.