DEV Community

Cover image for #3 Building Headless Commerce - Adding Search Engine with Algolia
Jakub Andrzejewski
Jakub Andrzejewski

Posted on • Updated on

#3 Building Headless Commerce - Adding Search Engine with Algolia

When I first heard about Algolia, I was a bit sceptical about it. Why would I need a search engine in my web application? I can search through my own database myself, right? Of course, that is correct. But Algolia is just super fast in terms of delivering search results to your website. And this is exactly what you want to deliver to your customers and users, speed!

Make sure to check out Algolia module for Nuxt as we recently released a 1.0.0 version of it ->

I have written already some articles about Algolia but this time, I decided to record a video about it!

In this video we will be adding a Search Engine Algolia to our Headless Commerce application we have created in our previous video. If you have not seen this video yet (here is the link ->, I highly encourage you to do that as it explains really well the concepts of e-commerce and its development.

We will dynamically fetch search results from Algolia to our Nuxt 3 application.

If you get lost at some point here is the github repo -> and branch feat/algolia.


In order to integrate Algolia with your Nuxt application, I would highly recommend to try out the offical Nuxt module. I am also a core maintainer of it :)

First, let's install the module:

yarn add @nuxtjs/algolia # npm install --save @nuxtjs/algolia
Enter fullscreen mode Exit fullscreen mode

Next, let's register it in the modules array in our nuxt.config.ts file and add required configuration, in this case apiKey and applicationId:

  modules: ["@nuxtjs/tailwindcss", "nuxt-graphql-client", "@nuxtjs/algolia"], // <- here

  algolia: {
    apiKey: process.env.ALGOLIA_SEARCH_API_KEY,
    applicationId: process.env.ALGOLIA_APPLICATION_ID,
Enter fullscreen mode Exit fullscreen mode

And this point, also remember to add the environment variables to the .env file:

Enter fullscreen mode Exit fullscreen mode

Furthermore, in our TheHeader.vue component let's add a script tag where we will call the useAlgoliaSearch composable. Please keep in mind that in the video, I have used the deprecated useSearch composable but the proper one is the one below.

<script lang="ts" setup>
const { result, search } = useAlgoliaSearch("headless_commerce");
const hits = ref([]);
const fetchSearchResults = async (e) => {
  await search({ query: });
  hits.value = result.value.hits;
Enter fullscreen mode Exit fullscreen mode

Let's stop for a second here. In the script tag, we have called a useAlgoliaSearch and passed an index name as a parameter. Under the hood, this composable will call the init index method to initialize the Algolia index.

Next, we have registered a ref with an empty array that is called hits. This array will be populated with a result from Algolia.

Finally, we have registered a method that will be triggered after input change to fetch the search results based on the query provided by the user. Under the hood this method will call the search method from the useAlgoliaSearch composable and assign the result to the hits. In the production application, please remember to implement some debouncing mechanism to not generate too much traffic to Algolia.

Going further into the TheHeader.vue component improvements, we have also created an input that will react to the query change, and also the search results in a form of a list.

<div class="px-6 flex items-center">
      <NuxtLink to="/">
        <img class="h-20 w-20 mr-16" src="/logo.svg" alt="Store logo" />
      <div class="flex justify-center relative">
        <div class="mb-3 w-96">
            class="block w-full px-3 py-1 bg-white bg-clip-padding border border-solid border-gray-300 rounded"
            placeholder="Search for products"
          class="flex justify-center absolute top-10 z-10"
          <ul class="bg-white border border-gray-200 w-96 text-gray-900">
              v-for="hit in hits"
              class="px-6 py-2 border-b border-gray-200 w-full"
              @click="hits = []"
                class="flex items-center"
                <img class="w-20" :src="hit['Image Src']" />
                <p class="ml-4">{{ hit.Title }}</p>
Enter fullscreen mode Exit fullscreen mode

There is a lot of code here but the general idea is the input that triggers fetching the search results, and a list of results displayed after fetching.

And that's it!


Well done! You have just added Algolia Search Engine to your Headless Commerce application. Keep in mind that this is just the beginning of the searching capabilities so for more, I highly encourage you to visit both and

Top comments (0)