routing option to synchronize UI state with the browser URL, so users can bookmark, share, and revisit a search.
This guide covers enabling default routing, customizing URLs, and creating SEO-friendly URLs.
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.
JavaScript
routing to true is equivalent to this configuration:
JavaScript
- 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.
JavaScript
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:
JavaScript
Keep unrelated URL parameters
If you enable InstantSearch routing, the URL includes only parameters from widgets. 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
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.JavaScript
instantsearch.routers.history to configure the default 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.
Group facet values
To group facet values such as “turquoise”, “ocean”, and “sky” under “blue”, add the group at indexing time. Either add a separate grouping attribute or store both the individual value and group value in the same attribute. For example, with the following dataset:JSON
colorGroup to each record:
JSON
color attribute:
JSON