TL;DR for future visitors with the same problem, see my answer below
I am using algolia for search within my Nuxt app. As I would like users to enter a search term on the dashboard and be redirected to the "full" search page including results after initiating the search, I need to use algolia routing.
What should happen
- User enters search term in dashboard and presses the "search"-button.
- The app directs the user to
https://example.com/search?term=THE_SEARCH_TERM. - The page loads showing the results for the entered search term.
What's happening
The app directs the user to https://example.com/search?term=THE_SEARCH_TERM and immediately redirects to https://example.com/search, removing the search parameter and therefore not displaying the results for the query.
As mentioned I am using Nuxt, but the component is not rendered on the server side. The template simplified template would be:
<client-only>
<ais-instant-search
index-name="my_index"
:search-client="algoliaSearchClient"
:routing="algoliaRouting"
>
<ais-index />
<ais-configure v-bind="searchParameters" />
<ais-autocomplete>
<ais-search-box />
</ais-autocomplete>
</ais-instant-search>
</client-only>
I am initialising the search client as well as the routing in a global mixin like so:
data() {
return {
algoliaSearchClient: algoliasearch(
"xxx",
"xxxxx"
),
algoliaRouting: {
router: historyRouter({
parseURL(data: any) {
const { query = "" } = data.qsModule.parse(
data.location.search.slice(1)
)
return {
query,
}
},
}),
stateMapping: {
stateToRoute(uiState: any) {
const indexUiState = uiState[indexName]
return {
term: indexUiState.query,
categories:
indexUiState.menu &&
indexUiState.menu.categories,
brand:
indexUiState.refinementList &&
indexUiState.refinementList.brand,
page: indexUiState.page,
}
},
routeToState(routeState: any) {
const term = routeState?.term
return {
[indexName]: {
query: term,
},
}
},
},
},
}
}
Using the provided stateMapping and router constructors resulted in the same behaviour.
Remarks The behaviour is only observed when using the router to push to the url. If the url containing a search term is copied and entered manually, the query parameters are not removed.