Skip to main content
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.
Synchronizing your UI with the browser URL is considered good practice. It lets your users take one of your results pages, copy the URL, and share it. It also improves the user experience by enabling the use of the back and next browser buttons to keep track of previous searches. InstantSearch provides the necessary API entries to let you synchronize the state of your search UI (your refined widgets and current ) with any kind of storage. This is possible with the 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.
Vue InstantSearch provides a basic way to activate the browser URL synchronization with the routing option.
Vue
Assume the following search UI state:
  • Query: “galaxy”
  • Menu:
    • categories: “Cell Phones”
  • Refinement List:
    • brand: “Apple”, “Samsung”
  • Page: 2
The resulting URL in your browser’s URL bar will look like this:
This URL is accurate and can be translated back to a search UI state. However, this isn’t the most human-readable, or optimized for search engines. The next section shows how to make it more SEO-friendly.

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. The stateMapping 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
InstantSearch manages 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: converts uiState to routeState.
  • routeToState: converts routeState to uiState.
Assume the following search UI state:
  • Query: “galaxy”
  • Menu:
    • categories: “Cell Phones”
  • Refinement List:
    • brand: “Apple” and “Samsung”
  • Page: 2
This translates into the following uiState:
JSON
Implement 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 the stateMapping functions to:
  1. In stateToRoute, return an object containing “q” for the query
  2. 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.
URLs are more than query parameters. Another important part is the path. Manipulating the URL path is a common ecommerce pattern that better references your page results. In this section, you’ll learn how to create this kind of URLs:
This URL is composed of the path which now includes /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
The basic routing example uses the history router with no extra customization. Here you’re also using the history router but explicitly setting configuration options. The 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 the routeState object returned from stateToRoute to 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 routeState to the browser URL,
    • You want to render a tags in the menu widget,
    • You call createURL in one of your connectors’ rendering methods.
  • 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
In this example, whenever users visit https://example.org/search/Cameras, it pre-selects the “Cameras and camcorders” filter. This is achieved with a dictionary:
JavaScript
You can build these dictionaries from your Algolia . With such a solution, you have full control over what categories are discoverable from the URL.

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 a robots.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 using historyRouter, a new one is written from scratch. The router key expects an object with the following keys as values:
JavaScript
For simplicity, in this example, a 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
Fill in the 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 implementing createURL. 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 the createServerRootMixin 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:
  1. Use createServerRootMixin in data, so this.$router is available.
  2. Create an InstantSearch router that wraps Vue Router.
  3. Set up provide as the root mixin would otherwise do.
  4. Set up findResultsState in serverPrefetch.
  5. Call hydrate in beforeMount.
Set up a custom renderToString function.
Wrap the Vue router for usage with Vue InstantSearch.
Vue
As in Vue Router, you must set up Nuxt to write deep query strings. In Nuxt, you do this in nuxt.config.js:
JavaScript
For more information, see 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
You could create an additional attribute and use it for faceting:
JSON
Or you could list the individual colors and their groups so you can use them both for faceting:
JSON
Last modified on July 10, 2026