UI libraries / Vue InstantSearch / Widgets
Signature
<ais-infinite-hits
  // Optional parameters
  :escapeHTML="boolean"
  :show-previous="boolean"
  :class-names="object"
  :transform-items="function"
  :cache="object"
/>
Import
1
2
3
4
5
6
7
8
9
import { AisInfiniteHits } from 'vue-instantsearch';
// Use 'vue-instantsearch/vue3/es' for Vue 3

export default {
  components: {
    AisInfiniteHits
  },
  // ...
};

1. Follow additional steps in Optimize build size to ensure your code is correctly bundled.
2. This imports all the widgets, even the ones you don’t use. Read the Getting started guide for more information.

About this widget

The ais-infinite-hits widget displays a list of results with a “Show more” button at the bottom of the list. As an alternative to this approach, the infinite scroll guide describes how to create an automatically-scrolling infinite hits experience.

To configure the number of hits to show, use the ais-hits-per-page or the ais-configure widget.

If there are no hits, you should display a message to users and clear filters so they can start over.

Examples

1
<ais-infinite-hits />

Props

Parameter Description
escapeHTML
type: boolean
default: true
Optional

Whether to escape the raw HTML in the hits.

1
<ais-infinite-hits :escapeHTML="false" />
show-previous
type: boolean
default: false
Optional

Enable the button to load previous results. The button is only displayed if the routing option is enabled in ais-instant-search and users aren’t on the first page. Override this behavior with slots.

1
<ais-infinite-hits :show-previous="true" />
class-names
type: object
default: {}
Optional

The CSS classes you can override:

  • ais-InfiniteHits: the root element of the widget.
  • ais-InfiniteHits-list: the list of results.
  • ais-InfiniteHits-item: the list items.
  • ais-InfiniteHits-loadPrevious: the button to display previous results.
  • ais-InfiniteHits-loadMore: the button to display more results.
  • ais-InfiniteHits-loadPrevious--disabled: the disabled button to display previous results.
  • ais-InfiniteHits-loadMore--disabled: the disabled button to display more results.
1
2
3
4
5
6
7
<ais-infinite-hits
  :class-names="{
    'ais-InfiniteHits': 'MyCustomInfiniteHits',
    'ais-InfiniteHits-list': 'MyCustomInfiniteHitsList',
    // ...
  }"
/>
transform-items
type: function
default: items => items
Optional

Receives the items and is called before displaying them. It returns a new array with the same “shape” as the original. This is helpful when transforming, removing, or reordering items.

The complete results data is also available, including all regular response parameters and helper parameters (for example, disjunctiveFacetsRefinements).

If you’re transforming an attribute with the ais-highlight widget, you must transform item._highlightResult[attribute].value.

When using an array, take steps to avoid creating infinite loops. When you use an array as a prop, it causes the widget to re-register on every render, and this can sometimes cause these infinite loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<template>
  <!-- ... -->
  <ais-infinite-hits :transform-items="transformItems" />
</template>

<script>
  export default {
    methods: {
      transformItems(items) {
        return items.map(item => ({
          ...item,
          name: item.name.toUpperCase(),
        }));
      },

      /* or, combined with results */
      transformItems(items, { results }) {
        return items.map((item, index) => ({
          ...item,
          position: { index, page: results.page },
        }));
      },
    },
  };
</script>
cache
type: object
default: in-memory cache object
Optional

The widget caches all loaded hits. By default, it uses its own internal in-memory cache implementation. Alternatively, use sessionStorage to retain the cache even if users reload the page.

You can also implement your own cache object with read and write methods. This can be handy if you need to persist the data across sessions or if you expect the cached data to grow larger than the browser’s 5 MB allowed storage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<template>
  <ais-infinite-hits :cache="cache" />
</template>

<script>
import {
  createInfiniteHitsSessionStorageCache
} from 'instantsearch.js/es/lib/infiniteHitsCache'

export default {
  data() {
    return {
      cache: createInfiniteHitsSessionStorageCache(),
    }
  }
}
</script>

Customize the UI

Parameter Description
default

The slot to override the complete DOM output of the widget.

Note that when you implement this slot, none of the other slots will change the output, as the default slot surrounds them.

Scope

  • items: object[]: the records that matched the search.
  • refinePrevious: () => void: the function to load previous results.
  • refineNext: () => void: the function to load more results.
  • isLastPage: boolean: whether it’s the last page.
  • sendEvent: (eventType, hit, eventName) => void: the function to send click or conversion events. The view event is automatically sent when this connector renders hits. YLearn more about these events in the insights middleware documentation.
    • eventType: 'click' | 'conversion'
    • hit: Hit | Hit[]
    • eventName: string
  • insights: (method: string, payload: object) => void: (Deprecated) Sends Insights events.
    • method: string: the Insights method to be called. Only search-related methods are supported: 'clickedObjectIDsAfterSearch', 'convertedObjectIDsAfterSearch'.
    • payload: object: the payload to be sent.
      • eventName: string: the name of the event.
      • objectIDs: string[]: a list of objectIDs.
      • index?: string: the name of the index related to the click.
      • queryID?: string: the Algolia queryID found in the search response when clickAnalytics: true.
      • userToken?: string: a user identifier.
      • positions?: number[]: the position of the click in the list of Algolia search results. When not provided, index, queryID, and positions are inferred by the InstantSearch context and the passed objectIDs:
      • index by default is the name of index that returned the passed objectIDs.
      • queryID by default is the ID of the query that returned the passed objectIDs.
      • positions by default is the absolute positions of the passed objectIDs. Each element of items has the following read-only properties:
    • __queryID: the query ID (only if clickAnalytics is true).
    • __position: the absolute position of the item.

    For more details on the constraints of each payload property, refer to the Insights client documentation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<ais-infinite-hits>
  <template v-slot="{
    items,
    refinePrevious,
    refineNext,
    isLastPage,
    sendEvent,
  }">
    <ul>
      <li>
        <button @click="refinePrevious">
          Show previous results
        </button>
      </li>
      <li v-for="item in items" :key="item.objectID">
        <h1>{{ item.name }}</h1>
        <button
          type="button"
          @click="sendEvent('click', item, 'Item Added')"
        >
          Add to cart
        </button>
      </li>
      <li v-if="!isLastPage">
        <button @click="refineNext">
          Show more results
        </button>
      </li>
    </ul>
  </template>
</ais-infinite-hits>
loadPrevious

The slot to override the DOM output of the “Show previous” button.

Scope

  • page: number: the value of the current page.
  • isFirstPage: boolean: whether it’s the first page.
  • refinePrevious: () => void: the function to load the previous page.
1
2
3
4
5
6
7
8
9
10
<ais-infinite-hits :show-previous="true">
  <template v-slot:loadPrevious="{ page, isFirstPage, refinePrevious }">
    <button
      :disabled="isFirstPage"
      @click="refinePrevious"
    >
      Show previous results (page: {{ page + 1 }})
    </button>
  </template>
</ais-infinite-hits>
item

The slot to override the DOM output of the item.

Scope

  • items: object: a single hit with all its attributes.
  • index: number: the relative position of the hit in the list.
  • insights: (method: string, payload: object) => void: (Deprecated) sends Insights events.
    • method: string: the Insights method to be called. Only search-related methods are supported: 'clickedObjectIDsAfterSearch', 'convertedObjectIDsAfterSearch'.
    • payload: object: the payload to be sent.
      • eventName: string: the name of the event.
      • objectIDs: string[]: a list of objectIDs.
      • index?: string: the name of the index related to the click.
      • queryID?: string: the Algolia queryID found in the search response when clickAnalytics: true.
      • userToken?: string: a user identifier.
      • positions?: number[]: the position of the click in the list of Algolia search results. When not provided, index, queryID, and positions are inferred by the InstantSearch context and the passed objectIDs:
      • index by default is the name of index that returned the passed objectIDs.
      • queryID by default is the ID of the query that returned the passed objectIDs.
      • positions by default is the absolute positions of the passed objectIDs. Each element of items has the following read-only properties:
    • __queryID: the query ID (only if clickAnalytics is true).
    • __position: the absolute position of the item.

    For more details on the constraints of each payload property, refer to the Insights client documentation.

1
2
3
4
5
<ais-infinite-hits>
  <template v-slot:item="{ item, index, insights }">
    {{ index }} - {{ item.name }}
  </template>
</ais-infinite-hits>
loadMore

The slot to override the DOM output of the “Show more” button.

Scope

  • page: number: the value of the current page.
  • isLastPage: boolean: whether it’s the last page.
  • refineNext: () => void: the function to load the next page.
1
2
3
4
5
6
7
8
9
10
<ais-infinite-hits>
  <template v-slot:loadMore="{ page, isLastPage, refineNext }">
    <button
      :disabled="isLastPage"
      @click="refineNext"
    >
      Show more results (page: {{ page + 1 }})
    </button>
  </template>
</ais-infinite-hits>

HTML output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<div class="ais-InfiniteHits">
  <button class="ais-InfiniteHits-loadPrevious">
    Show previous results
  </button>
  <ol class="ais-InfiniteHits-list">
    <li class="ais-InfiniteHits-item">
      ...
    </li>
    <li class="ais-InfiniteHits-item">
      ...
    </li>
    <li class="ais-InfiniteHits-item">
      ...
    </li>
  </ol>
  <button class="ais-InfiniteHits-loadMore">
    Show more results
  </button>
</div>

Click and conversion events

If the insights option is true, the ais-infinite-hits widget automatically sends a click event with the following “shape” to the Insights API when a user clicks on a hit.

1
2
3
4
5
6
7
8
9
{
  eventType: 'click',
  insightsMethod: 'clickedObjectIDsAfterSearch',
  payload: {
    eventName: 'Hit Clicked',
    // 
  },
  widgetType: 'ais.infiniteHits',
}

To customize this event, use the sendEvent function in your item slot and send a custom click event.

1
2
3
4
5
6
7
8
9
10
<ais-infinite-hits>
  <template v-slot:item="{ item, sendEvent }">
    <div @click="sendEvent('click', item, 'Product Clicked')">
      <h2>
        <ais-highlight attribute="name" :hit="item" />
      </h2>
      <p>{{ item.description }}</p>
    </div>
  </template>
</ais-infinite-hits>

The sendEvent function also accepts an object as a fourth argument to send directly to the Insights API. You can use it, for example, to send special conversion events with a subtype.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<ais-infinite-hits>
  <template v-slot:item="{ item, sendEvent }">
    <div>
      <h2>
        <ais-highlight attribute="name" :hit="item" />
      </h2>
      <p>{{ item.description }}</p>
      <button
        @click="sendEvent('conversion', hit, 'Added To Cart', {
          // Special subtype
          eventSubtype: 'addToCart',
          // An array of objects representing each item added to the cart
          objectData: [
            {
              // The discount value for this item, if applicable
              discount: item.discount || 0,
              // The price value for this item (minus the discount)
              price: item.price,
              // How many of this item were added
              quantity: 2,
            },
          ],
          // The total value of all items
          value: item.price * 2,
          // The currency code
          currency: 'USD',
        })"
      >
        Add to cart
      </button>
    </div>
  </template>
</ais-hits>

Fields representing monetary values accept both numbers and strings, in major currency units (for example, 5.45 or '5.45'). To prevent floating-point math issues, use strings, especially if you’re performing calculations.

Did you find this page helpful?