next-algolia-server
TypeScript icon, indicating that this package has built-in type declarations

0.2.7 • Public • Published

next-algolia-server

Install

yarn add algoliasearch next-algolia-server

Usage

Basic configuration

import AlgoliaServer from "next-algolia-server";

// Create your instance with your algoliaId and algoliaKey
// Get yours https://www.algolia.com/
const algolia = AlgoliaServer.getInstance({ algoliaId, algoliaKey });

// Set your index
algolia.setIndex("blog");

// Set your searchableAttributes
// Read more about this https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
algolia.setSearchableAttributes([
  "title,description",
  "content",
  "categories.name",
]);

Then you would be able to use this algolia instance to handle your records.

// I did it inside my `getStaticProps`
export async function getStaticProps() {
  // ... functions to get my posts...
  const posts = [];
  // Read more about this https://www.algolia.com/doc/api-reference/api-methods/save-objects/
  // Every record most have a unique `objectID` param
  // Or let algolia generate it with algoliaServer.saveRecords(posts, { autoGenerateObjectIDIfNotExist: true })
  // Be careful if you let algolia generate it you could duplicate your records
  algoliaServer.saveRecords(posts);

  return posts;
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Package Sidebar

Install

npm i next-algolia-server

Weekly Downloads

3

Version

0.2.7

License

MIT

Unpacked Size

17.4 kB

Total Files

6

Last publish

Collaborators

  • enbonnet