Vue InstantSearch
Vue InstantSearch is a front-end library built by Algolia, offering a comprehensive set of UI components to integrate Algolia's lightning-fast search capabilities into Vue.js applications. It provides a declarative, component-based approach for building search UIs, abstracting away much of the complexity of the Algolia API. The current stable version is 4.24.4, with releases typically following the monorepo's synchronized cadence, often with minor version bumps or 'Note: Version bump only' entries across multiple InstantSearch packages. Its key differentiators include deep integration with the Algolia ecosystem, robust support for both Vue 2 (2.6.0+) and Vue 3 (3.0.0+), and a rich collection of pre-built, customizable widgets for common search features like search boxes, hit lists, facets, and pagination. This allows developers to quickly construct powerful search experiences with minimal effort, focusing on customization rather than boilerplate.
Common errors
-
[Vue warn]: Component <AisInstantSearch> is missing template or render function.
cause An InstantSearch widget component (e.g., `AisInstantSearch`, `AisSearchBox`) was not correctly imported or registered in your Vue application.fixEnsure that components are imported as named exports (e.g., `import { InstantSearch } from 'vue-instantsearch';`) and then either registered globally (`app.component('AisInstantSearch', InstantSearch)` for Vue 3, or `Vue.component('AisInstantSearch', InstantSearch)` for Vue 2) or locally within your component's `components` option. -
TypeError: Cannot read properties of undefined (reading 'init') at VueComponent.mounted
cause The `searchClient` prop passed to `<ais-instant-search>` is `undefined` or not a valid Algolia search client instance.fixVerify that `algoliasearch/lite` is imported, correctly initialized with valid Algolia credentials (App ID and Search API Key), and then properly passed to the `<ais-instant-search>` component via the `:search-client` prop. Ensure it's not the Admin API Key. -
Uncaught (in promise) Error: You must provide an 'indexName' to the InstantSearch component.
cause The required `indexName` prop is missing or empty on the `<ais-instant-search>` root component.fixAdd the `:index-name="'your_algolia_index_name'"` prop to your `<ais-instant-search>` component, providing a valid Algolia index name that you wish to search.
Warnings
- breaking Migrating a Vue InstantSearch project from Vue 2 to Vue 3 can involve several breaking changes, particularly concerning component registration, slot syntax, and reactivity system differences. While `vue-instantsearch` supports both, the underlying Vue.js changes require attention.
- gotcha Using an `algoliasearch` client version outside the specified peer dependency range (`>= 3.32.0 < 6`) can lead to runtime errors or unexpected behavior due to API inconsistencies with the underlying Algolia client.
- gotcha Server-side rendering (SSR) with Vue InstantSearch requires specific setup, including proper integration of `@vue/server-renderer` (Vue 3) or `vue-server-renderer` (Vue 2), and careful state hydration. Incorrect configuration often results in non-hydrated content or runtime errors.
- breaking Breaking changes in the underlying `instantsearch.js` library (e.g., from v3 to v4) directly affect `vue-instantsearch`'s behavior and API, even if `vue-instantsearch`'s major version does not increment simultaneously. Developers should review `instantsearch.js` changelogs.
- deprecated The `search-insights` library and the `insights` middleware for event tracking have been deprecated in favor of a simpler `insights` option on the `ais-instant-search` component. Continued use of the old middleware is discouraged.
Install
-
npm install vue-instantsearch -
yarn add vue-instantsearch -
pnpm add vue-instantsearch
Imports
- InstantSearch
import InstantSearch from 'vue-instantsearch';
import { InstantSearch } from 'vue-instantsearch'; - SearchBox
import { AisSearchBox } from 'vue-instantsearch';import { SearchBox } from 'vue-instantsearch'; - connectSearchBox
import { connectSearchBox } from 'instantsearch.js/es/connectors';import { connectSearchBox } from 'vue-instantsearch';
Quickstart
import { createApp, h } from 'vue';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, SearchBox, Hits, Configure } from 'vue-instantsearch';
import 'instantsearch.css/themes/satellite-min.css'; // Or another theme, e.g., algolia-min.css
const searchClient = algoliasearch(
process.env.VUE_APP_ALGOLIA_APP_ID ?? 'YOUR_APP_ID', // Replace with your Algolia Application ID
process.env.VUE_APP_ALGOLIA_SEARCH_API_KEY ?? 'YOUR_SEARCH_API_KEY' // Replace with your Algolia Search-only API Key
);
const App = {
components: {
'ais-instant-search': InstantSearch,
'ais-search-box': SearchBox,
'ais-hits': Hits,
'ais-configure': Configure,
},
data() {
return {
searchClient,
indexName: 'instant_search', // Replace with your Algolia index name
};
},
template: `
<div id="app">
<ais-instant-search :search-client="searchClient" :index-name="indexName">
<ais-configure :hits-per-page="8" />
<header>
<h1>Vue InstantSearch Demo</h1>
<ais-search-box placeholder="Search products..." />
</header>
<main>
<ais-hits>
<template v-slot:item="{ item }">
<article>
<h2>{{ item.name }}</h2>
<p>{{ item.description }}</p>
<p>Price: $\${{ item.price }}</p>
</article>
</template>
</ais-hits>
</main>
</ais-instant-search>
</div>
`,
};
createApp(App).mount('#app');