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.
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
createInstantSearchRouterNext
.
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
React InstantSearch lets you enable URL synchronization by setting therouting
to true
.
React
- Query: “galaxy”
-
Menu:
categories
: “Cell Phones”
-
Refinement List:
brand
: “Apple”, “Samsung”
- Page: 2
Manually rewrite URLs
The default URLs that InstantSearch generates are comprehensive, but if you have many widgets, this can also generate noise. You may want to decide what goes in the URL and what doesn’t, or even rename the query parameters to something that makes more sense to you. Settingrouting
to true
is syntactic sugar for the following code:
React
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.
React
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
: convertsuiState
torouteState
.routeToState
: convertsrouteState
touiState
.
- 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.
React
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.React
history
router to explicitly set options on the default router mechanism. Notice the usage of both the router
and stateMapping
options to map uiState
to routeState
, and vice versa.
Using the routing
option as an object, you can configure:
-
windowTitle
: a method to map therouteState
object returned fromstateToRoute
to the window title. -
createURL
: a method called every time you need to create a URL. When:- You want to synchronize the
routeState
to the browser URL - You want to render
a
tags in themenu
widget - You call
createURL
in 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 easily 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
Next steps
You now have a good starting point to create an even more dynamic experience with React InstantSearch. Next up, you could improve this app by:- Checking the API reference to learn more about the router.
- Server-side rendering your app for increased performance.