This is the React InstantSearch v7 documentation.
If you’re upgrading from v6, see the upgrade guide.
If you were using React InstantSearch Hooks,
this v7 documentation applies—just check for necessary changes.
To continue using v6, you can find the archived documentation.
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
Customize the loading indicator
By default, theSearchBox
displays a loading indicator when the search is stalled. When the network is slow, this visual cue tells users that something is happening
You can change the loading icon with the
loadingIconComponent
prop.
React
Turn off search-as-you-type
Algolia is designed to deliver a search-as-you-type experience. Yet, it can also lead to lag in slow network conditions because browsers can only run a limited number of parallel requests to the same domain. Reducing requests can help prevent further lag. Debouncing helps you limit requests and avoid processing non-necessary ones by only sending requests once users have stopped typing. Implement debouncing at theSearchBox
level with the queryHook
prop. For example:
React
The Network Information API is unavailable on some browsers.
React
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.Server-side rendering
Server-side rendering (SSR) lets you generate HTML from InstantSearch components on the server. Before loading the page for the first time, a backend server makes an initial request to Algolia, renders it as HTML, and sends it to the browser. SSR is beneficial in slow network conditions because the browser directly loads an HTML document containing search results. There’s no need to wait for all the JavaScript assets to load before seeing the search results.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:
babel.config.js
tsc
):
tsconfig.json
Troubleshooting
To check if tree shaking works, try to import InstantSearch into your project without using it.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 theSearchBox
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, you can expect additional network requests with DynamicWidgets
.
If you experience QPS limitations, consider implementing a debounced SearchBox
.
For more information, see:
- SearchBox widget
- DynamicWidgets widget
- All React InstantSearch widgets