Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including strapi-hook-algolia with all npm packages installed. Try it out:

var strapiHookAlgolia = require("strapi-hook-algolia")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

strapi-hook-algolia v3.2.4

A hook to setup a Algolia service for the Strapi framework

strapi-hook-algolia

NPM Version Monthly download on NPM

This hook allows you to use Algolia as a service in Strapi strapi.services.algolia. Algolia is a hosted search engine capable of delivering real-time results from the first keystroke. Algolia's powerful API lets you quickly and seamlessly implement search within your websites, mobile, and voice applications.

Supported Strapi versions:

  • v3.2.x (recommended)
  • v3.x

Older version may work with the beta version of this hook, but are not supported.

Installation

# using yarn
yarn add strapi-hook-algolia

# using npm
npm install strapi-hook-algolia --save

Usage

  1. Create a Algolia account
  2. Edit your config, add your own Application ID & Admin API Key
  3. Use the algolia service in the Lifecycle callbacks of your ContentType

saveObject() should be used in the Lifecycle callback afterCreate, fired after an insert, and afterUpdate, fired after an update.

  afterCreate(result, data) {
    strapi.services.algolia.saveObject(result, 'index');
  }
  afterUpdate(result, params, data) {
    strapi.services.algolia.saveObject(result, 'index');
  }

deleteObject() should be used in the Lifecycle callback afterDelete, fired after a delete query.

  afterDelete(result, params) {
    strapi.services.algolia.deleteObject(result.id, 'index');
  }

Full example

const index = 'post';

module.exports = {
  lifecycles: {
    afterCreate(result, data) {
      strapi.services.algolia.saveObject(result, index);
    },
    afterUpdate(result, params, data) {
      strapi.services.algolia.saveObject(result, index);
    },
    afterDelete(result, params) {
      strapi.services.algolia.deleteObject(result.id, index);
    },
  },
};

Draft & publish example
Here is an example on how to only index published entries, for when using the draft and publish feature.

const index = 'post';

module.exports = {
  lifecycles: {
    afterUpdate(result, params, data) {
      if (result.published_at) {
        strapi.services.algolia.saveObject(result, index);
      } else {
        strapi.services.algolia.deleteObject(result.id, index);
      }
    },
    afterDelete(result, params) {
      strapi.services.algolia.deleteObject(result.id, index);
    },
  },
};

Hook config

To activate and configure the hook, you need to create or update the file ./config/hook.js in your strapi app.

  module.exports = {
    settings: {
      // ...
      algolia: {
        enabled: true,
        applicationId: 'ABCDEFGHIJ',
        apiKey: 'secure_algolia_admin_api_key',
        debug: true,              // default: false
        prefix: 'my_own_prefix'   // default: Strapi environment (strapi.config.environment)
      },
    }
  };

Support

Resources

License

  • Copyright (c) 2020-2021 Mattias van den Belt & Strapi Solutions (MIT License).
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free