routing option to synchronize UI state with the browser URL, so users can bookmark, share, and revisit the same search.
Don’t configure
initial-ui-state 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.
Routing examples
The examples in this section use Vue 2. If you use Vue 3, adapt them using the Vue 3 migration guide.Basic routing demo
Run and edit the basic routing example in CodeSandbox.
Basic routing source code
Browse the source code for the basic routing example.
SEO-friendly routing demo
Run and edit the SEO-friendly routing example in CodeSandbox.
SEO-friendly routing source code
Browse the source code for the SEO-friendly routing example.
Vue Router demo
Run and edit the Vue Router example in CodeSandbox.
Vue Router source code
Browse the source code for the Vue Router example.
Default URLs
The examples use the InstantSearch.js router.
Add
instantsearch.js to your project dependencies alongside vue-instantsearch.routing prop on <ais-instant-search> to synchronize UI state with the browser URL.
The default routing setup stores routing-compatible UI state in URL query parameters.
Vue
- 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
By default, routing writes only InstantSearch state to the URL. To preserve unrelated parameters, include them when implementingcreateURL.
The following example preserves URL parameters that start with utm_:
JavaScript
Change the name of a key in routing
To rename thequery route parameter to q,
return q from stateToRoute and
map it back to query in routeToState.
SEO-friendly URLs
This guide uses the router from InstantSearch.js.
Make sure you add
instantsearch.js to your project’s dependencies in addition to vue-instantsearch.Store categories in the URL path
This example stores the category in the path and the query, page, and brands as query parameters.JavaScript
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
Combine with Vue Router
The previous examples use the InstantSearch history router. If your search page reads the URL through Vue Router to render content outside InstantSearch, synchronize InstantSearch with Vue Router instead. Otherwise, keep the InstantSearch router. Create a router object instead of usinghistoryRouter.
The router property expects an object with these functions:
JavaScript
stateMapping.
Configure Vue Router to parse nested query parameters and serialize them into query strings in main.js first:
JavaScript
router in the routing object returned by data:
JavaScript
Combine with Nuxt
To enable routing in a Nuxt app, you can’t use thecreateServerRootMixin factory as a mixin as usual,
because you need to access Vue Router which is only available on the component instance.
Here’s the workaround:
- Use
createServerRootMixinindata, sothis.$routeris available. - Create an InstantSearch router that wraps Vue Router.
- Set up
provideas the root mixin would otherwise do. - Set up
findResultsStateinserverPrefetch. - Call
hydrateinbeforeMount.
renderToString function.
Vue
nuxt.config.js:
JavaScript
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