Prepare the connection to Algolia
When sending the first network request to a domain, a security handshake must happen, consisting of several round trips between the client and the Algolia server. If the handshake first happened when users typed their first keystroke, the speed of that first request would be significantly slower. Use a preconnect link to carry out the handshake immediately after loading the page but before any user interaction. To do this, add a link tag with your Algolia domain in thehead
of your page.
HTML
Add a loading indicator
Consider a user accessing your app in a subway:- They type some characters
- Nothing happens
- They wait, but still, nothing happens
ais-search-box
, use the show-loading-indicator
option. The indicator will display slightly after the last query has been sent to Algolia. Change the duration of the delay with stalled-search-delay
(on the ais-instant-search
widget).
For example:
Vue
Make your own loading indicator
You can also use the loading indicator in custom widgets. The following example shows how to make a custom component that writesLoading...
when search stalls. If network conditions are optimal, users won’t see this message.
Vue
Debouncing
Another way of improving the perception of performance is by preventing lag. Although the default InstantSearch experience of generating one query per keystroke is usually desirable, this can lead to a lag in the worst network conditions because browsers can only make a limited number of parallel requests. By reducing the number of requests, you can prevent this lag. Debouncing prevents unnecessary requests by delaying them until a timeout period has passed. Implement debouncing at theais-search-box
level with the connectSearchBox
connector. For example:
Vue
This code has been specifically created for Vue 2. Some modifications may be required for it to work correctly in Vue 3.
Select a debounce delay
The optimal debouncing delay should match your audience’s typing speed (typically 30 words per minute (WPM) on mobile devices and 40 WPM on desktop devices). If the delay is too short, users still see flashes of content. If the delay is too long, the time between keypresses and results becomes too long. 200 ms is the preferred debounce delay. Delays of over 300 ms will start degrading the user experience.Optimize build size
InstantSearch supports dead code elimination through tree shaking, but you must follow a few rules for it to work:- Bundle your code using a module bundler that supports tree shaking with the
sideEffects
property inpackage.json
, such as Rollup or webpack 4+. - Ensure you pick the ES module build of InstantSearch by targeting the
module
field inpackage.json
(resolve.mainFields
option in webpack,mainFields
option in@rollup/plugin-node-resolve
). This is the default configuration in most popular bundlers: you only need to change something if you have a custom configuration. - Keep Babel or other transpilers from transpiling ES6 modules to CommonJS modules. Tree shaking is much less optimal on CommonJS modules, so it’s better to let your bundler handle modules itself.
babel-preset-env
not to process ES6 modules:
JavaScript
tsc
):
JSON
Vue.use(VueInstantSearch)
) plugin. Doing so imports all the widgets, even the ones you don’t use.
Instead, individually import and register each InstantSearch widget within components:
Troubleshooting
To check if tree shaking works, try to import InstantSearch into your project without using it.JavaScript
Caching
Caching by default (and how to turn it off)
By default, Algolia caches the search results of the queries, storing them locally in the cache. This cache only persists during the current page session, and as soon as the page reloads, the cache clears. If users type a search (or part of it) that’s already been entered, the results will be retrieved from the cache instead of requesting them from Algolia, making the app much faster. While it’s a convenient feature, sometimes you may want to clear the cache and make a new request to Algolia. For instance, when changes are made to some records in your index, you should update your app’s frontend to reflect that change (and avoid displaying stale results retrieved from the cache). Therefresh
function, available for custom connectors, lets you clear the cache and trigger a new search.
When to discard the cache
Consider discarding the cache when your app’s data is updated by:- Your users (for example, in a dashboard). In this case, refresh the cache based on an app state, such as the last user modification.
- Another process you don’t manage (for example, a cron job that updates users inside Algolia). In this case, you should refresh your app’s cache periodically.
Refresh the cache triggered by a user action
The following code triggers a refresh based on a user action (such as adding a new product or clicking a button).Vue
This code has been specifically created for Vue 2. Some modifications may be required for it to work correctly in Vue 3.
Refresh the cache periodically
You can set an interval to determine how often the app clears the cache. Use this approach if you can’t trigger cache clearance based on user actions.waitTask
to avoid refreshing the cache too early.
Turn off the cache
If you need the most current data and the performance impact of this isn’t an issue, turn off caching.JavaScript
Queries per second (QPS)
Search operations aren’t limited by a fixed “search quota”. Instead, they’re limited by your plan’s maximum QPS and operations limit. Every keystroke in InstantSearch using theais-search-box
counts as one operation.
Then, depending on the widgets you add to your search interface,
you may have more operations being counted on each keystroke.
For example, if you have a search interface with an ais-search-box
, an ais-menu
, and an ais-refinement-list
,
then every keystroke triggers one operation.
Upon each user change to an ais-menu
or ais-refinement-list
, a new operation is executed.
If you experience QPS limitations, consider implementing a debounced ais-search-box
.
For more information, see: