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.
routing option to synchronize UI state with the browser URL, so users can bookmark, share, and revisit the same search.
This guide covers enabling default routing, customizing URLs, and creating SEO-friendly URLs.
If you’re using server-side rendering, follow the server-side rendering guide.
If you’re using Next.js, see createInstantSearchRouterNext.
Don’t configure
initialUiState and routing together.- Use
initialUiStateto set the UI state when the search first loads, such as for a default query or filter. - Use
routingto keep the UI state synchronized with the URL, so users can bookmark or share a search.
Default URLs
Setrouting to true to use the default routing configuration:
a history router with simple state mapping.
This stores routing-compatible UI state in URL query parameters.
React
routing to true is equivalent to this configuration:
React
- Query: “galaxy”
-
Menu:
categories: “Cell Phones”
-
Refinement List:
brand: “Apple”, “Samsung”
- Page: 2
Customize URL routing
You can customize which values appear and rename the URL parameters. ThestateMapping option maps between InstantSearch’s uiState and the routeState used by the router.
Use it to rename parameters or omit values that you don’t want to include in the URL.
React
uiState.
The following example maps that state to shorter URL parameters.
The state contains information about the user’s search, including the query,
the selection,
the page being viewed,
and the widget hierarchy.
uiState only stores modified widget values, not defaults.
To persist this state in the URL,
InstantSearch 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:
React
SEO-friendly URLs
To create more descriptive URLs, move search state from query parameters into the URL path. This is a common pattern for ecommerce category and search pages.Store categories in the URL path
This example stores the category in the path and the query, page, and brands as query parameters.React
history router.
The router reads and writes URLs, while stateMapping maps uiState to routeState and back.
When you configure the history router, you can customize these functions:
windowTitle: returns the browser window title for arouteState.createURL: creates a URL fromrouteState. InstantSearch calls it when synchronizing the browser URL, rendering links in themenuwidget, or when a connector callscreateURL.parseURL: createsrouteStatefrom the URL when users load or reload the page or use the browser’s back or forward navigation.
Make URLs more discoverable
Shorter category URLs can be more readable and memorable. Use a mapping object to map category names to shorter URL slugs. Given the dataset in this guide, you can make some categories more discoverable:- “Cameras and camcorders” →
/Cameras - “Car electronics and GPS” →
/Cars
https://example.org/search/Cameras, InstantSearch selects the “Cameras and camcorders” category.
Define mappings between category names and URL slugs:
JavaScript
About SEO
For your search results to be part of a public search engine’s results, you must be selective. Trying to index too many search results pages could be considered spam. To do that, create arobots.txt and host it at https://example.org/robots.txt.
Here’s an example based on the URL scheme you created.
robots.txt
Basic routing demo
Explore basic InstantSearch routing in CodeSandbox.
SEO-friendly routing demo
Explore SEO-friendly routing in CodeSandbox.