JavaScript
Upgrade templates
Starting from InstantSearch.js v4.46.0, string-based and Hogan.js templates are deprecated. If you’re using Hogan.js templates or HTML strings using string-based templates, you can replace them either with safe HTML strings using the providedhtml tagged template, or JSX templates.
These new templating options are safer against cross-site scripting (XSS) attacks,
perform better, and are more accessible,
because they reuse and patch the existing DOM instead of replacing it entirely at each render.
For more information, see:
Interpolation
You can interpolate dynamic values using template literals placeholders. All available dynamic values are exposed within the scope of the template function.Highlighting and snippeting
In bothhits and infiniteHits widgets,
templates expose a set of built-in components to handle highlighting and snippeting.
Loops
You can use plain JavaScript to build dynamic templates. For example, you can useArray.mapto loop over an array and display a list.
Conditional rendering
To conditionally render a part of your UI, you can use a short-circuit operator or a ternary. Since templates are functions, you can also use conditionalreturn statements to provide alternative templates.
Upgrade event tracking
Starting from v4.55.0, InstantSearch simplifies the event tracking process with theinsights option.
You no longer need to install the search-insights library or set up the insights middleware yourself.
Here are some benefits when using the insights option:
- It better handles the . Once you set it, all search and event tracking calls include the token.
- It automatically sends default events from built-in widgets such as
refinementList,menu, etc. You can also change the event payloads, or remove them altogether. - It lets you send custom events from your custom widgets.
- It simplifies forwarding events to third-party trackers.
search-insights or with the insights middleware, you should:
- Upgrade InstantSearch.js to v4.55.0 or greater
- Migrate from using the
insightsmiddleware to theinsightsoption - Either update or remove the
search-insightslibrary
Use the Insights option
Starting from v4.55.0, InstantSearch lets you enable event tracking with theinsights option. You no longer need to set up the insights middleware yourself.
JavaScript
insights middleware in your code, you can now remove it and move its configuration to the insights option.
JavaScript
Update search-insights
Starting from v4.55.0, InstantSearch can loadsearch-insights for you so the insightsClient option is no longer required.
If you prefer loading it yourself, make sure to update search-insights to at least v2.4.0 and forward the reference to insights.
If you’re using the UMD bundle with a <script> tag, make sure to update the full code snippet (not just the version):
If you’re using a package manager, you can upgrade it to the latest version.
Command line
insights option.
Remove search-insights
Starting from v4.55.0, InstantSearch loadssearch-insights for you from jsDelivr if not detected in the page. If you’ve installed search-insights, you can now remove it.
If you’re using the UMD bundle with a <script> tag, you can remove the snippet in any page that uses InstantSearch:
HTML
Command line
search-insights from your code:
InstantSearch loads
search-insights from the jsDelivr CDN, which requires that your site or app allows script execution from foreign resources. Check the security best practices for recommendations.No longer needed configuration
clickAnalytics: true
JavaScript
getInsightsAnonymousUserToken
JavaScript
insights middleware, it initially sets the anonymous userToken.
As soon as you set another userToken, it automatically syncs it between search and analytics calls.
insightsClient at InstantSearch
JavaScript
Insights middleware, you don’t need to pass aa to instantsearch() anymore.
hits and infiniteHits template
You might have templates in your hits or infiniteHits widgets that look like the following:
HTML
sendEvent function.
JavaScript
analytics widget
The analytics widget has been deprecated in favor of the insights middleware.
When the built-in widgets trigger default events, you can intercept them using the insights middleware’s onEvent hook and send them to third-party trackers.
JavaScript
uiState changes with the onStateChange function.
JavaScript
lodash.debounce to debounce the events you send.
JavaScript
Upgrade to Recommend
TherelatedProducts widget is now available instead of the previous experimental widget EXPERIMENTAL_relatedItems. To migrate, you change the widget name and update the props:
JavaScript
Upgrade from v3 to v4
The latest version of InstantSearch focuses on federated search and bundle size. This required some changes that might impact your app.Federated search (multi-index)
If you were already using federated search (for example by synchronizing twoinstantsearch instances with the searchFunction),
the implementation is now simpler. Instead, try the new index widget,
to which you can attach more widgets.
For example:
JavaScript
Routing
Even if you aren’t using multi-index search, the way in whichuiState is stored has changed.
It used to look like this:
JSON
JSON
simpleStateMapping) with the current version, you can replace it with singleIndexStateMapping('yourIndexName'). You have to change the code as followed:
JavaScript
index widget and add this extra level to the routeToState. You can check the source for a reference on how to implement this.
For example, a stateMapping that maps a subset of properties would change like this:
JavaScript
Configure
Theconfigure widget is now included in uiState.
If you want to exclude it from the URL you can use the default stateMappings or exclude it in your custom state mapping.
A good reason to exclude the configure widget from the UI state is to prevent users from adding any search parameters.
You must exclude this widget in both the stateToRoute, to keep it from appearing in the URL and routeToState, so that the URL doesn’t apply to the state.
Check the stateMapping source code for implementation details.
Algolia search helper
This release includes version 3 of thealgoliasearch-helper package. If you’re using the built-in widgets or connectors, nothing changes for you.
This version of algoliasearch-helper no longer includes Lodash, which reduces its bundle size (from 27.5 KB to 9.1 KB Gzipped).
If you’re using any methods from the helper, searchParameters or searchResults, refer to the package’s change log.
Search parameters
The optionsearchParameters was removed from the instantsearch widget.
You can replace it with the configure widget:
JavaScript
initialUiState to your InstantSearch widget. This overwrites specific searchParameters that are otherwise set during widget instantiation. initialUiState is only taken into account if a widget owning that “state” is mounted. A warning appears in development mode explaining which widget needs to be added for the UI state to have an effect.
An example is a refinementList widget:
JavaScript
addWidget and removeWidget
The search.addWidget function is deprecated in favor of search.addWidgets, which accepts an array of widgets.
Using arrays makes it clearer to see the structure of nested widgets.
search.removeWidget is deprecated in favor of search.removeWidgets, which also accepts an array of widgets.
Custom widgets
Because version 3 of thealgoliasearch-helper package is used,
you have to replace the getConfiguration lifecycle with getWidgetSearchParameters and getWidgetState.
This also means that your custom widget takes part in the routing.
You can exclude it from the URL with stateMapping.
JavaScript
JavaScript
connectAutocomplete
The indices option was removed in favor of index widgets (see the federated search section).
For example, the following code:
JavaScript
JavaScript
onHistoryChange
The onHistoryChange function is removed.
To subscribe to changes in the URL, create a custom router and attach to the write hook. For example:
JavaScript
router
The dispose function on the router interface is now required and its signature is updated (see the example below).
This change only impacts you if you use a custom router.
TypeScript
pagination
The noRefinementRoot CSS class gets added once there are no more possible refinements.
It no longer gets applied on the first page, which was the wrong behavior.
Upgrade from v2 to v3
InstantSearch v3 introduces some breaking changes in the widgets’ naming, options, and markup.What’s new
Search from your own search client
Since InstantSearch.js 2.8.0, it’s possible to search from your own backend using thesearchClient option.
This option is now the way to plug InstantSearch to either Algolia’s official JavaScript client or to yours. This means that the official search client isn’t bundled in InstantSearch, allowing for more flexibility. This results in a smaller bundle size when you’re not searching directly from the frontend, or that you use multiple instances of the search client.
Predictable DOM structure
InstantSearch.js v3 becomes more predictable by generating a DOM that follows Algolia’s InstantSearch.css specification. All flavors are now aligned, which makes it easier to migrate from one library flavor to another. The options and the DOM output are similar whether you use React InstantSearch, Vue InstantSearch, or InstantSearch.js.Optimize InstantSearch based on your environment
The new bundle comes in two forms:- Development bundle. This is a heavier bundle that helps developers better debug in the development phase of an InstantSearch app. It’s more verbose and gives clues and warnings about the library usage.
- Production bundle. This is a lighter bundle that doesn’t include any development warnings. Use this when deploying your app in a production environment.
Imports
UMD (Universal Module Definition)
New naming has been introduced for the UMD imports. This makes it clearer which InstantSearch.js bundle to use either in development or in production. The production bundle will get lighter over time as it won’t include the runtime warnings and documentation.Before
After
CommonJS (CJS)
Before
JavaScript
After
JavaScript
ES (ECMAScript)
No changes.InstantSearch
The variablesappId and apiKey are replaced by searchClient.
Earlier usage
-
Import
InstantSearch.js -
Initialize InstantSearch
JavaScript
New usage
-
Import
algoliasearch(prefer the lite version for search only) -
Import
InstantSearch.js -
Initialize InstantSearch with the
searchClientoptionJavaScript
transformData is replaced by transformItems
Since InstantSearch.js first public release, you can customize the values used in the widgets.
This method was letting you map 1-1 the values with other values. With React InstantSearch,
a slightly different API was implemented that lets you map over the list of values and to change their content.
Earlier usage
JavaScript
New usage
JavaScript
Highlights and snippets
Highlighting is a powerful tool for showing users why results match their queries. Earlier versions of InstantSearch used internal mechanisms or Hogan.js to implement highlighting within widget templates. Algolia now provideshtml tagged templates.
For more information, see:
Earlier usage
New usage
JavaScript
urlSync is dropped
If you were using the urlSync option,
you should now migrate to the new routing feature.
Here are the elements you need to migrate:
urlSync: truebecomesrouting: truethresholdbecomesrouting: { router: instantsearch.routers.history({ writeDelay: 400 }) }mappingandtrackedParametersare replaced withstateMapping. Read “User friendly URLs” to know how to configure ituseHashis removed but can be achieved using an advanced configuration of the history routergetHistoryStateis removed but can be achieved using an advanced configuration of the history router
collapsible is dropped
collapsible is replaced by the collapsed option in the panel widget.
autoHideContainer is dropped
autoHideContainer is replaced by the hidden option in the panel widget.
createAlgoliaClient is dropped
createAlgoliaClient is replaced by searchClient.
createQueryString is dropped
URL synchronization is done with Routing alone.
Widgets
Breadcrumb
CSS classes
Markup
HTML
ClearRefinements (formerly ClearAll)
Options
CSS classes
Markup
HTML
CurrentRefinements (formerly CurrentRefinedValues)
Options
clearsQuerycan be replaced byexcludedAtributes: [].
CSS classes
Markup
Default
HTML
With includesQuery and a query
HTML
Geographical search
Options
Since
paddingBoundingBox conflicted with the routing option it was removed to support URLSync for the GeoSearch widget.
enableGeolocationWithIP
Before:
JavaScript
JavaScript
position
Before:
JavaScript
JavaScript
radius
Before:
JavaScript
JavaScript
precision
Before:
JavaScript
JavaScript
CSS classes
Markup
With the control element:HTML
HTML
Hits
Options
escapeHTMLbecomestrueby default.allItemstemplate has been removed in favor ofconnectHits
CSS classes
Markup
HTML
HitPerPage (formerly HitsPerPageSelector)
Options
CSS classes
Markup
HTML
Infinite hits
Options
escapeHTMLdefaults totrue
CSS classes
Markup
HTML
Hierarchical menu
Options
CSS classes
Markup
Default
HTML
Show more disabled
HTML
Menu
Options
showMoreis now a boolean option (showMore.templatesare now intemplates)sortBydefaults to['isRefined', 'name:asc']- An object containing
isShowingMoreis passed toshowMoreTexttemplate to toggle between the two states:
JavaScript
CSS classes
Markup
Default
HTML
Show more disabled
HTML
Menu select
Options
CSS classes
Markup
HTML
NumericMenu (formerly NumericRefinementListe)
Options
The item
name attribute is now named label.
CSS classes
Markup
HTML
NumericSelector
Widget removed.
Pagination
Options
CSS classes
Markup
HTML
PriceRanges
Widget removed.
RangeInput
Options
CSS classes
Markup
HTML
RangeSlider
Options
CSS classes
Markup
HTML
RatingMenu (formerly StarRating)
Options
The value for the label
andUp is now inline inside templates.item.
CSS classes
Markup
HTML
RefinementList
Options
searchablePlaceholderdefaults to"Search..."searchableEscapeFacetValuesdefaults totruesearchableIsAlwaysActivedefaults totrueshowMoreis now a boolean option (searchForFacetValues.templatesandshowMore.templatesare now intemplates)- An object containing
isShowingMoreis passed toshowMoreTexttemplate to toggle between the two states:
JavaScript
CSS classes
Markup
Default
HTML
Show more disabled
HTML
With search and no results
HTML
SearchBox
Options
With the drop of
wrapInput, it was decided not to accept inputs as containers anymore.
If you want complete control over the rendering, use the connectSearchBox connector.
The search box doesn’t support poweredBy.
Algolia requires that you use this widget if you’re on a community plan (open source, not-for-profit, or DocSearch).
Configuration options for reset, submit and loadingIndicator have been moved to templates and cssClasses. For example, for reset:
reset.template=>templates.resetreset.cssClasses.root=>cssClasses.reset
autofocus is now set to false by default and doesn’t support the "auto" value anymore.
CSS classes
Markup
HTML
SortBy
Options
- A
sortByitem value is nowvalueinstead ofname:
JavaScript
CSS classes
Markup
HTML
Stats
CSS classes
Markup
HTML
ToggleRefinement (formerly Toggle)
Options
collapsible and autoHideContainer options have been removed. These options are now implemented as part of the Panel widget wrapper.
The label options has been moved into the templates.labelText template to make it consistent with the templates parameters of other widgets and the item template was removed.
Data that was provided to templates.item is now provided to templates.labelText.
If your attribute is called free_shipping, the default template displays “free_shipping”.
To rename it, change templates.labelText to “Free shipping”.
CSS classes
Markup
HTML
Connectors
connectAutocomplete
Options
escapeHTMLbecomestrueby default.
connectBreadcrumb
- The BreadcrumbItem
nameproperty is renamed tolabel.
connectGeoSearch
Options
Since
paddingBoundingBox conflicted with the routing option it was removed to support URLSync for the GeoSearch widget.
enableGeolocationWithIP
Before:
JavaScript
JavaScript
position
Before:
JavaScript
JavaScript
radius
Before:
JavaScript
JavaScript
precision
Before:
JavaScript
JavaScript
connectRange
Options
connectRangeSlider
Connector removed (use connectRange instead).
connectClearRefinements (formerly connectClearAll)
Options
connectNumericMenu (formerly connectNumericRefinementList)
Options
namebecomeslabel
connectRefinementList
Options
escapeFacetValuesdefaults totrue
connectCurrentRefinements (used to be connectCurrentRefinedValues)
Options
items (used to be refinements )
refinements used to be a flat list, now it’s called items (one per attribute), which each has its own list of refinements
Upgrade from v1 to v2
This guide has all the major changes that introduced in v2 with the description on how to migrate from v1.The searchBox widget has new default options
To identify the input as a search box, a magnifying glass icon was placed at the start, and a reset button displayed as a cross at the end.
You can customize the result with these two options:
- reset
boolan|{template?: string|Function, cssClasses?: {root: string}}Display a reset button in the input when there is a query. - magnifier
boolan|{template?: string|Function, cssClasses?: {root: string}}Display a magnifier should at beginning of the input.
JavaScript
searchBox API reference.
No more hitsPerPage in hits and infiniteHits
This option was removed from those two widgets. To configure
this option of the engine, there are still three ways:
- Use the dashboard or the client, to change the setting at the index level.
- Use the
hitsPerPagewidget. - Use the configuration option of
instantsearch:
JavaScript
The items aren’t sorted like before in the refinementList / menu
The default sort order of those widgets has changed. This might have affected your implementation if you didn’t specify them originally. To change back the order use thesortBy configuration
key.
Here are examples of usage of sortBy using the previous sorting scheme:
JavaScript
sortBy option of menu or
refinementList
Some variables have been changed
Internally all the widgets are now using the connectors. The aim was to ensure the API was as close to the one offered byreact-instantsearch connectors.
This then affected the name of some variables in the templates and the API.
Changes in templates:
- In the item template of the hierarchicalMenu and menu widgets,
namebecomeslabel - In the item template of the refinementList widget,
namebecomesvalue.
- In
hitsPerPageSelector, theoptionswas renamed toitems.
React components can’t be used as templates
When InstantSearch.js was created it was built using React and it wasn’t known that react-instantsearch would be built.react-instantsearch is now the recommended solution if your app uses React.
That’s why support for the React based templates was dropped in this release.
Algolia considers the engine used to build the widgets in InstantSearch.js to be an implementation detail. Since it isn’t exposed anymore, Algolia can change it and use the best solution for each release.
rangeSlider widget is using Rheostat as Slider Component
Slider components are hard to implement and that’s why Algolia relies on an external
component for that. Algolia is taking the opportunity of this new version
to switch to the current ‘state of the art’ of sliders: Rheostat.
If you want to customize the style, some CSS classes have changed.
The look is still similar as the one in the V1.
searchFunction can be used to change parameters
Introduced in 1.3.0, searchFunction was originally meant as a way to change
the timing of the search. However it was realized that it was a good way to
alter the search state before making the actual state.
This is what’s required to force the query string:
JavaScript
JavaScript
JavaScript