Add Algolia Search To Nuxt V3

Dec 28, 2022

Install packages

npm install vue-instantsearch algoliasearch instantsearch.css

Setup via plugins, create plugins/algolia.ts

import InstantSearch from 'vue-instantsearch/vue3/es';export default defineNuxtPlugin(nuxtApp => {  nuxtApp.vueApp.use(InstantSearch)})

Or you can import on specific page, e.g. pages/search.vue

import { AisInstantSearch, AisConfigure, AisSearchBox, AisRefinementList, AisHighlight, AisHits, AisPagination } from 'vue-instantsearch/vue3/es';

Create pages/search.vue

  • Support /search?q=android query string
  • Allow filtering of search result based on tags
  • CSS classes are from Tailwind CSS
<script setup>/*import InstantSearch from 'vue-instantsearch/vue3/es';const nuxtApp = useNuxtApp()nuxtApp.vueApp.use(InstantSearch) */// not necessary if you use the plugins methodimport { AisInstantSearch, AisConfigure, AisSearchBox, AisRefinementList, AisHighlight, AisHits, AisPagination } from 'vue-instantsearch/vue3/es';useHead({  title: 'Search'})const {  query: { q },} = useRoute();import algoliasearch from 'algoliasearch/lite';// import 'instantsearch.css/themes/algolia-min.css';import 'instantsearch.css/themes/satellite-min.css';// import your algolias settingsimport { APPLICATION_ID, SEARCH_API_KEY, INDEX_NAME } from '~/secret/algolia';const searchClient = algoliasearch(  APPLICATION_ID,  SEARCH_API_KEY)// perform search based on /search?q=android query stringconst initialUiState = {  [INDEX_NAME]: {      query: q,      page: 1  }}</script><template>  <div>    <client-only placeholder="Loading...">      <ais-instant-search        :search-client="searchClient"        :index-name="INDEX_NAME"        :stalled-search-delay="1000"        :initial-ui-state="initialUiState"      >        <div class="grid grid-cols-6 gap-4">          <div class="hidden md:block">            <div class="text-xl mb-2">Tags</div>            <ais-refinement-list attribute="tags" />            <ais-configure :hitsPerPage="10" />          </div>          <div class="col-span-6 sm:col-span-5">            <ais-search-box placeholder="Search ..." class="searchbox" />            <ais-hits class="my-3">              <template v-slot:item="{ item }">                <NuxtLink :to="item.path" class="text-xl block"><ais-highlight :hit="item" attribute="title" /></NuxtLink>                <div><ais-highlight :hit="item" attribute="description" class="text-grey" /></div>                <div>                  <ul class="inline-sep-list">                    <li>{{ new Date(item.lastmod) }}</li>                    <li v-for="tag in item.tags">{{ tag }} </li>                  </ul>                </div>              </template>            </ais-hits>            <div class="pagination"><ais-pagination /></div>          </div>        </div>      </ais-instant-search>    </client-only>  </div></template><style>.ais-Hits-item {  box-shadow: none;  display: block;  font-size: 1.2rem;  padding: 0.5rem 0;}  </style>

NOTE: You can also try algolia/nuxt which provide algolia APIs as well.

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.