GithubHelp home page GithubHelp 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

cprestoncowart avatar darrenmothersele avatar shayaulman avatar u12206050 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gridsome-plugin-algolia's Issues

Do you have any examples?

Do you have any examples of using this on a Gridsome blog somewhere? I am a little confused on the fields. Do you have an id in front matter for every single blog post? I don't generate IDs because they aren't being saved anywhere so I wonder if the slug is good enough.

Also, I don't have a modified date but it seems like I am going to need one because I don't want to regenerate this on every build. Do you have the modified field on every single blog post or just when one changes?

Thanks for creating this plugin!

Error: Algolia failed: report is not defined

Hi there!

Thank you for taking time to port this plugin and contributing to Gridsome's plugin ecosystem. I was following your instruction of how to setup this plugin with my project. When I build the project however, I'm getting the following error:

Algolia collection #0: Executing query
Error: Algolia failed: report is not defined

I've searched the internet for a meaning of this error, found none! Can you please let me know what am I doing wrong?

Thank you, once again!

Environment Variables

I have a .evn.development with the following variables defined

ALGOLIA_APP_ID=123456789
ALGOLIA_API_KEY=123456789
ALGOLIA_INDEX_NAME=blog_posts

I know your instructions have an example where you require dotenv but it's already built into Gridsome. If you look at the docs it says that we should be able to use these values in our configuration https://gridsome.org/docs/environment-variables

When I use the actual values everything works fine but when I try to use the environment variables like so

    {
      use: `gridsome-plugin-algolia`,
      options: {
        appId: process.env.ALGOLIA_APP_ID,
        apiKey: process.env.ALGOLIA_API_KEY,
        collections,
        chunkSize: 10000, // default: 1000
        enablePartialUpdates: false, // default: false
      },
    },

I get the following error:

AlgoliaSearchError: Please provide an application ID. Usage: algoliasearch(applicationID, apiKey, opts)
    at AlgoliaSearchNodeJS.AlgoliaSearchCore (/Users/vega/dev/gridsome/danvega-dev/node_modules/algoliasearch/src/AlgoliaSearchCore.js:50:11)
    at AlgoliaSearchNodeJS.AlgoliaSearch (/Users/vega/dev/gridsome/danvega-dev/node_modules/algoliasearch/src/AlgoliaSearch.js:11:21)
    at AlgoliaSearchNodeJS.AlgoliaSearchServer (/Users/vega/dev/gridsome/danvega-dev/node_modules/algoliasearch/src/server/builds/AlgoliaSearchServer.js:17:17)
    at new AlgoliaSearchNodeJS (/Users/vega/dev/gridsome/danvega-dev/node_modules/algoliasearch/src/server/builds/node.js:79:23)
    at algoliasearch (/Users/vega/dev/gridsome/danvega-dev/node_modules/algoliasearch/src/server/builds/node.js:68:10)
    at afterBuild (/Users/vega/dev/gridsome/danvega-dev/node_modules/gridsome-plugin-algolia/index.js:58:20)
    at Promise.all.events.(anonymous function).map (/Users/vega/dev/gridsome/danvega-dev/node_modules/gridsome/lib/app/App.js:144:41)
    at Array.map (<anonymous>)
    at App.dispatch (/Users/vega/dev/gridsome/danvega-dev/node_modules/gridsome/lib/app/App.js:143:47)
    at module.exports (/Users/vega/dev/gridsome/danvega-dev/node_modules/gridsome/lib/build.js:53:13)

This only happens on the build so these values should be injected. Any idea why this wouldn't work? I got a basic search working but until I can extract these values to env variables I can't push this code. Thanks again for the plugin!

Avoid using api.store directly

While running gridsome develop I got the following warning:

 WARNING  Deprecation notices

Avoid using api.store directly. Use the actions in api.loadSource() instead.
./node_modules/gridsome-plugin-algolia/index.js:2:17   

Gridsome version: 0.7.9
Plugin Version: 2.1.2

Error: Algolia failed: ID is not defined

Hello, thanks a lot for this plugin, I recently just added a modified field in my frontmatter so i could use partial updates in the plugin, but when I try to build, I get this error:

Algolia collection #0: Executing query
Algolia collection #0: items in collection 5
Algolia collection #0: starting Partial updates
Algolia collection #0: found 6 existing items
Error: Algolia failed: ID is not defined at afterBuild (/home/edmund/projects/projects/my own/NinjaBlog/node_modules/gridsome-plugin-algolia/index.js:184:13)

This is how my config looks:

const collections = [
  {
    query: `{
      allPost {
        edges {
          node {
            id
            title
            path
            modified
          }
        }
      }
    }`,
    transformer: ({ data }) => data.allPost.edges.map(({ node }) => node),
    indexName: process.env.ALGOLIA_INDEX_NAME, // Algolia index name
    itemFormatter: item => {
      return {
        objectID: item.id,
        title: item.title,
        slug: item.path,
        modified: String(item.modified)
      };
    }, // optional
    matchFields: ['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
      }
]
  }

I have my .env file in place and all keys provided there.

I'm currently using the version 2.1.2 of the plugin.

Error: Algolia failed: Cannot read property 'map' of undefined

Hello!

I'm getting this error when running gridsome build. Indeed, the result from the GraphQL query is null, but running the same query in the Gridsome GraphQL playground returns the expected result, with actual posts.

From index.js, line 130, if I add a console.log

const result = await graphql(query);
console.log(result);

I end up with

{
  data: [Object: null prototype] {
    allDocPage: [Object: null prototype] { edges: [Array] }
  }
}
Error: Algolia failed: Cannot read property 'map' of undefined

Any idea what could be happening here?

Getting an error

Thanks for putting this together. When I run buildin gridsome, I'm getting this error:

Error: Algolia failed: report is not defined
at afterBuild (/Users/chrisemery/Sites/tbs-gridsome-2019/node_modules/gridsome-plugin-algolia/index.js:174:13)
at process._tickCallback (internal/process/next_tick.js:68:7)

Any suggestions you might have to fix it would be greatly appreciated. Thanks.

Algolia: Response not successful: Received status code 429

Hello, I have a problem with rate limitations and Algolia. Netlify gives me the following error: Algolia: Response not successful: Received status code 429. Do you have any tips on how we can avoid this using your plugins in Gridsome?

Thanks

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.