Guides / Building Search UI

How to install Vue InstantSearch

Install Vue InstantSearch with a packaging system or with a direct link in your web page.

With a packaging system

If you have a JavaScript build tool (for example if you’ve created your project using the Vue command-line tool), you can install Vue InstantSearch from npm:

1
npm install algoliasearch vue-instantsearch

Then in your main js file, you can load the package as Vue plugin:

1
2
3
4
5
6
7
import { createApp } from 'vue';
import App from './App.vue';
import InstantSearch from 'vue-instantsearch/vue3/es';

const app = createApp(App);
app.use(InstantSearch);
app.mount('#app');

This lets you use all the widgets directly in your components.

Directly in your page

Vue works when you’re embedding it directly in an HTML page too. First add the two script tags:

1
2
3
4
5
6
7
<script src="https://cdn.jsdelivr.net/npm/algoliasearch@4.24.0/dist/algoliasearch-lite.umd.js" integrity="sha256-b2n6oSgG4C1stMT/yc/ChGszs9EY/Mhs6oltEjQbFCQ=" crossorigin="anonymous"></script>

<!-- For Vue 2: -->
<script src="https://cdn.jsdelivr.net/npm/vue-instantsearch@4.20.2/vue2/umd/index.js" integrity="sha256-UcpauFiuPCWpN/yWNqkKeOr66sR4nzf15vPY5Mzdr4w=" crossorigin="anonymous"></script>

<!-- For Vue 3: -->
<script src="https://cdn.jsdelivr.net/npm/vue-instantsearch@4.20.2/vue3/umd/index.js" integrity="sha256-oaOCtktnMQiFh0LDmJcWN6ULMhtKhlISOeLaloalY5A=" crossorigin="anonymous"></script>

Then you can write your Vue app within your HTML, for example in a #app div.

1
2
3
4
<!-- Replace INDEX_NAME with the name of your index. -->
<ais-instant-search index-name="INDEX_NAME" v-bind:search-client="searchClient">
  <!-- Regular Vue InstantSearch app here -->
</ais-instant-search>

Finally, add a script tag with the instantiation of the plugin and the app:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<script>
  const { createApp } = Vue;

  const app = createApp({
    data() {
      return {
        // Initialize the search client
        
        //  If you're logged into the Algolia dashboard, the following values for
        //  YourApplicationID and YourSearchOnlyAPIKey are auto-selected from
        //  the currently selected Algolia application.
        searchClient: algoliasearch(
          'YourApplicationID',
          'YourSearchOnlyAPIKey'
        ),
      };
    },
  });
  app.use(VueInstantSearch);
  app.mount('#app');
</script>

ais-instant-search is the root Vue InstantSearch component. All widgets must be wrapped within it

In a production environment, secure your API keys as environment variables.

Send click and conversion events

Capturing real-time user interactions as events gives you actionable insights via click and conversion metrics, and they help you increase your customer engagement and conversions. Events are used to activate Algolia features and products like NeuralSearch, Dynamic Re-Ranking, Query Categorization, Recommend, and Personalization.

To send click and conversion events when users interact with your search UI, set the insights option to true.

Optimize your build with tree shaking

Tree shaking can be done with modern build tools. The goal is to have code that’s actually used in your built site. In the context of Vue InstantSearch this means not including the widgets that aren’t used. You can do this by using the components directly, rather than the Vue.use(InstantSearch) call.

In every component you’re using InstantSearch widgets, you can import and instantiate them like this:

1
2
3
4
5
6
7
8
import { AisInstantSearch, AisSearchBox } from 'vue-instantsearch/vue3/es';

export default {
  components: {
    AisInstantSearch,
    AisSearchBox,
  },
};

The build tool you’re using will then be able to only include the widgets that you’re actually using.

Browser support

Algolia supports the last two versions of the major browsers: Chrome, Edge, Firefox, Safari.

To support Internet Explorer 11 you need polyfills for: Array.prototype.find, Array.prototype.includes, Promise, Object.entries, and Object.assign.

The code samples in this documentation use a JavaScript syntax that isn’t natively supported by older browsers like Internet Explorer 11. If your site needs to support older browsers, use a tool like Babel to make your code work in the browsers you target.

Did you find this page helpful?