svelte-algolia
This package was inspired by the official gatsby-plugin-algolia
.
Usage
-
Install with yarn
or npm
yarn add -D svelte-algolia
npm i -D svelte-algolia
-
Create an algoliaConfig
:
import 'dotenv/config'
const algoliaConfig = {
appId: process.env.algoliaAppId,
apiKey: process.env.algoliaAdminKey,
indices: [
{ name: `pokedex`, getData: pokemonDataLoader },
{ name: `hitchHikersGuide`, getData: guideGetter },
],
}
The getData
function is expected to return an array of objects containing the data you wish to index to Algolia (a product catalog, blog posts, documentation pages, pokémons or whatever). Each object in the data array should have a key named either id
or objectID
for Algolia to recognize it and overwrite existing data.
-
Pass your config to indexAlgolia
:
import { indexAlgolia } from 'svelte-algolia'
indexAlgolia(algoliaConfig)
You can call this function whenever you like to update your indices. Typically, you would include in every production build of your app.
Config Options
const defaultConfig = {
verbosity: 1,
partialUpdates: false,
matchFields: [],
settings: {},
}
As a rollup
plugin
To use this package as part of a rollup
build (e.g. in a Svelte or Sapper app), simply include it in your plugins
array:
import { indexAlgolia } from 'svelte-algolia'
const algoliaConfig = {
}
const dev = process.env.NODE_ENV === `development`
export default {
input: './src/index.js',
output: {
file: './build/bundle.js',
format: 'iife',
name: 'bundle'
plugins: [
!dev && indexAlgolia(algoliaConfig),
]
}
}
Contribute
PRs are welcome but best open an issue first to discuss any changes.