You are currently reading the documentation for InstantSearch.js v4.
Read the migration guide to learn how to upgrade from v3 to v4.
You can still access the V3 documentation for this page.
uiState) with any kind of storage. This is possible with the routing option. This guide focuses on storing the UI state in the browser URL.
This guide goes through different ways to handle routing with your search UI:
- Enabling routing with no extra configuration
- Manually rewriting URLs to tailor it to your needs
- Crafting SEO-friendly URLs
When you are using routing, you can’t use
initialUiState as the two options override each other. Simple and static use cases can be more straightforward using initialUiState, but anything dynamic or complex should use routing.Basic URLs
InstantSearch.js lets you enable URL synchronization by setting therouting to true.
JavaScript
- Query: “galaxy”
-
Menu:
categories: “Cell Phones”
-
Refinement List:
brand: “Apple”, “Samsung”
- Page: 2
Manually rewrite URLs
All examples in this guide assume you’ve included InstantSearch.js in your web page from a CDN.
If you use a package manager instead, adjust how you import InstantSearch.js and its widgets.
routing to true is syntactic sugar for the following code:
JavaScript
stateMapping option defines how to go from InstantSearch’s internal state to a URL, and vice versa.
You can use it to rename query parameters and choose what to include in the URL.
JavaScript
uiState.
It contains information about the user’s search, including the query, applied filters, the current page being viewed, and the widget hierarchy.
uiState only stores modified widget values, not defaults.
To persist this state in the URL, InstantSearch first converts the uiState into an object called routeState. This routeState then becomes a URL. Conversely, when InstantSearch reads the URL and applies it to the search, it converts routeState into uiState. This logic lives in two functions:
stateToRoute: convertsuiStatetorouteState.routeToState: convertsrouteStatetouiState.
- Query: “galaxy”
-
Menu:
categories: “Cell Phones”
-
Refinement List:
brand: “Apple” and “Samsung”
- Page: 2
uiState:
JSON
stateToRoute to flatten this object into a URL,
and routeToState to restore the URL into a UI state.
JavaScript
SEO-friendly URLs
URLs are more than query parameters. Another important part is the path. Manipulating the URL path is a common ecommerce pattern that lets you better reference your result pages.Example implementation
Here’s an example that stores the brand in the path, and the query and page as query parameters.JavaScript
instantsearch.routers.history to explicitly set options on the default router mechanism. The router and stateMapping options are used to map uiState to routeState, and vice versa.
Using the routing option as an object, you can configure:
-
windowTitle: a method to map therouteStateobject returned fromstateToRouteto the window title. -
createURL: a method called every time you need to create a URL. When:- You want to synchronize the
routeStateto the browser URL - You want to render
atags in themenuwidget - You call
createURLin one of your connectors’ rendering methods.
- You want to synchronize the
-
parseURL: a method called every time users load or reload the page, or click the browser’s back or next buttons.
Make URLs more discoverable
In real-life applications, you might want to make some categories more accessible, with a URL that’s easier to read and to remember. Given your dataset, you can make some categories more discoverable:- “Cameras and camcorders” →
/Cameras - “Car electronics and GPS” →
/Cars
https://example.org/search/Cameras, it pre-selects the “Cameras and camcorders” filter.
You can achieve this with a dictionary.
JavaScript
About SEO
For your search results to be part of search engines results, you have to be selective. Adding too many search results inside search engines could be considered as spam. To do that, you can create arobots.txt to allow or disallow URLs from being crawled by search engines.
Here’s an example based on the previously created URL scheme.
robots.txt
Remove unrelated URL parameters from the URL
Applies to InstantSearch.js v3 and later. If you enable InstantSearch routing, only the parameters coming from widgets are included in the URL. To keep other parameters unrelated to InstantSearch, add them when implementingcreateURL.
For example, to keep all URL parameters that start with “utm_”, use the following code:
JavaScript
Group facet values
Applies to InstantSearch.js v1 and later. If you want to group, for example, “turquoise”, “ocean” and “sky” under “blue”, the recommended solution is to group them at indexing time. You can either add the group name as a separate attribute to globally filter on, or add both values in an array to make both the group and the individual value show up in the list. For example, with the following dataset:JSON
JSON
JSON