Vue
You’re reading the documentation for Vue InstantSearch v4.
Read the migration guide to learn how to upgrade from v3 to v4.
You can still find the v3 documentation for this page.
routing option.
This guide focuses on storing the UI state in the browser URL.
When you are using routing, you can’t use initial-ui-state
because the two options override each other.
Simple and static use cases can be more straightforward using initial-ui-state, but anything dynamic or complex should use routing.
Working examples
This code has been specifically created for Vue 2. Some modifications may be required for it to work correctly in Vue 3.Basic routing
SEO-friendly routing
Vue router
Basic 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.routing option.
Vue
- Query: “galaxy”
-
Menu:
categories: “Cell Phones”
-
Refinement List:
brand: “Apple”, “Samsung”
- Page: 2
Rewriting URLs manually
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. ThestateMapping defines how to go from InstantSearch’s internal state to a URL, and vice versa.
You can override it to rename query parameters and choose what to include in the URL.
Vue
uiState.
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:
Vue
Change the name of a key in routing
Applies to Vue InstantSearch v2 and later. If you want to change, for example, “query into “q” in routing, use thestateMapping functions to:
- In
stateToRoute, return an object containing “q” for the query - In
routeToState, replace that “q” with “query”.
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./search, then /Cell+Phones for the category. The query parameters are also simplified by only using the attribute name as the key. This requires you to have only one widget per attribute.
Implementation example
In the following SEO-friendly example the brand is stored in the path, and the query and page as query parameters.JavaScript
router and stateMapping options are used to map uiState to routeState, and vice versa.
Using the routing option as an object, configure:
-
windowTitle: a method to map therouteStateobject returned fromstateToRouteto the window title. -
createURL: a method called every time you need to create a URL. This should be done 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 apps, you might want to make specific categories easier to access by associating them with readable and memorable URLs. Given the 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.
This is achieved with a dictionary:
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 were using the InstantSearch router. This is fine in almost all of the use cases, but if you plan on using Vue Router too, and also plan on reading the URL with Vue Router in the search page to show something outside of the InstantSearch life cycle, you can choose to synchronize with Vue Router. This isn’t necessary if you are using Vue Router and aren’t planning to read from the URL. Instead of usinghistoryRouter, a new one is written from scratch. The router key expects an object with the following keys as values:
JavaScript
stateMapping won’t be completed. The default configuration of Vue Router doesn’t allow deeply nested URLs, so that must be implemented first in main.js:
JavaScript
router key on the routing object in the data function:
JavaScript
Remove unrelated URL parameters from the URL
Applies to Vue InstantSearch v2 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
Combine with Nuxt.js
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
routing.
Group facet values
Applies to Vue InstantSearch 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