Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
strapi-hook-algolia
Advanced tools
Strapi v4.x support, see @mattie-bundle/strapi-plugin-search
at https://mattie-bundle.mattiebelt.com/search/plugin.
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:
Older version may work with the beta version of this hook, but are not supported.
# using yarn
yarn add strapi-hook-algolia
# using npm
npm install strapi-hook-algolia --save
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);
},
},
};
Using the algoliasearch client
You can access the algolia javascript client, read the official documentation to know more.
// api/my-model/controllers/my-model.js
module.exports = {
async myController(ctx) {
// https://www.algolia.com/doc/api-reference/api-methods/
const { client } = strapi.services.algolia;
await client.listIndices();
// ...
},
}
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)
},
}
};
The default prefix uses strapi.config.environment
, resulting in for example: 'development_post'.
A custom prefix can be configured with the prefix
option in the hook config.
If you don't want to use any prefix at all, you can configure it with: prefix: false
.
FAQs
A hook to setup an Algolia service for the Strapi framework
The npm package strapi-hook-algolia receives a total of 41 weekly downloads. As such, strapi-hook-algolia popularity was classified as not popular.
We found that strapi-hook-algolia demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.