Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Duplicate facets inside the request #663

Closed
msassa opened this issue Apr 17, 2019 · 11 comments · Fixed by #705
Closed

Duplicate facets inside the request #663

msassa opened this issue Apr 17, 2019 · 11 comments · Fixed by #705
Labels

Comments

@msassa
Copy link

msassa commented Apr 17, 2019

Bug 🐞

What is the current behavior?

I posted a comment here algolia/instantsearch#3278 (comment)
And a post here https://discourse.algolia.com/t/ais-configure-cause-multiple-hit-to-algolia-api-doubling-the-facet-filter/7565

Simple showing or hidding a div cause multiple request to algolia api, and in every request the facets are duplicated.

Make a sandbox with the current behavior

Edit Check extra request

What is the expected behavior?

No making extra request, and NOT duplicate de facet

Does this happen only in specific situations?

I think is only when I add the ais-configure, without this component doesn't happen

What is the version you are using?

    "algoliasearch": "^3.32.1",
    "instantsearch.js": "^3.4.0",
    "vue-instantsearch": "^2.0.1",
@francoischalifour
Copy link
Member

Thanks for reporting the issue. As mentioned in your message, this is tracked in algolia/instantsearch#3278 and comes from the InstantSearch.js connectors. We're aware on this problem and will try to make some time to fix this issue.

In the mean time, did you try this solution?

@msassa
Copy link
Author

msassa commented Apr 19, 2019

Sorry, but I do not understand the proposed solution.
In addition, there is the problem of multiple request. Each time I click on the button to show / hide the menu, it makes several requests to Algolia, and executes the write method of the router, where I send information to the analytics.
Those additional requests, should also be a bug, since I'm only hiding a div, it does not make sense to make a new request to Algolia.

@samouss
Copy link
Contributor

samouss commented May 6, 2019

Hi @msassa, sorry about the delayed answer. We didn't find a proper solution to fix the issue yet inside Vue InstantSearch. The root of the issue come from an internal function of InstantSearch.js. It happens because the ais-configure widget triggers a useless re-render. Most of the time, this is because a value is inlined inside the template. Here it's not the case. It happens because we use $attrs to allow any props provided to the widget to be used as parameters. The downside of this is that the reference change between render which triggers an internal update for Vue InstantSearch.

The only workaround we've found is to re-implement the ais-configure widget with the mixin that we expose with Vue InstantSearch. With this solution, you can expose a prop on the widget that takes an object which contains a list of search parameters to apply on the request. Here is the implementation (see below). You can find a complete example on CodeSandbox.

import { createWidgetMixin } from 'vue-instantsearch';
import { connectConfigure } from 'instantsearch.js/es/connectors';

export default {
  name: 'CustomConfigure',
  mixins: [createWidgetMixin({ connector: connectConfigure })],
  props: {
    parameters: {
      type: Object,
      required: true,
    },
  },
  computed: {
    widgetParams() {
      return {
        searchParameters: this.parameters,
      };
    },
  },
  render() {
    return null;
  },
};

You can use it like this:

<template>
  <!-- ... -->
  <custom-configure :parameters="parameters" />
</template>

<script>
export default {
  data() {
    return {
      //...
      parameters: {
        hitsPerPage: 4
      }
    };
  }
};
</script>

Hope that helps, let us know if it fixes your issue or if you have any questions.

@samouss samouss changed the title Multiple hit to algolia api, duplicate facets inside the request Duplicate facets inside the requests May 6, 2019
@samouss samouss changed the title Duplicate facets inside the requests Duplicate facets inside the request May 6, 2019
@samouss samouss added the bug label May 6, 2019
@msassa
Copy link
Author

msassa commented May 6, 2019

Hi @samouss thank you so much for this help.
I can apply this fix, and actually works.
I found a similar issue with ais-current-refinements and like @Haroenv say in the Algolia forum was an array prop I'm passing to that component to use as exclude-attributes. I remove that prop, and now works fine.
Thanks again.

@msassa
Copy link
Author

msassa commented Jul 30, 2019

Hi everyone, after add some new filters, like distance and zipcode, the problem appears again.
I make a ticket here: https://discourse.algolia.com/t/vue-instant-search-keep-duplicating-the-facets-in-request/8210
If someone can help me to solve this, please, I will be very grateful.
Thanks

@Haroenv
Copy link
Contributor

Haroenv commented Jul 30, 2019

We are very close to solving this issue. You can help us try it out if you build (yarn; yarn build; yarn pack) in InstantSearch.js and then install the created gz in your Vue InstantSearch project. Sorry that this has taken long.

@Haroenv
Copy link
Contributor

Haroenv commented Jul 30, 2019

I have just released InstantSearch.js 3.6.0 with this fix.

Haroenv added a commit that referenced this issue Jul 30, 2019
fixes #663

Confirmed this in: https://codesandbox.io/s/check-extra-request-higuj (you need to clone it, CodeSandbox doesn't deduplicate the dependency as expected)
Haroenv added a commit that referenced this issue Jul 30, 2019
* fix(core): prevent duplicating facet values

fixes #663

Confirmed this in: https://codesandbox.io/s/check-extra-request-higuj (you need to clone it, CodeSandbox doesn't deduplicate the dependency as expected)

* chore: update size
@Haroenv
Copy link
Contributor

Haroenv commented Jul 30, 2019

Vue InstantSearch 2.3.0 has this fix: https://codesandbox.io/s/check-extra-request-higuj

@msassa
Copy link
Author

msassa commented Jul 30, 2019

Thank you so much @Haroenv
This was an issue for so long to my app, and now updating the packages is solved.
Thanks !!!

@profhouse
Copy link

profhouse commented Dec 20, 2020

Hi @msassa, sorry about the delayed answer. We didn't find a proper solution to fix the issue yet inside Vue InstantSearch. The root of the issue come from an internal function of InstantSearch.js. It happens because the ais-configure widget triggers a useless re-render. Most of the time, this is because a value is inlined inside the template. Here it's not the case. It happens because we use $attrs to allow any props provided to the widget to be used as parameters. The downside of this is that the reference change between render which triggers an internal update for Vue InstantSearch.

The only workaround we've found is to re-implement the ais-configure widget with the mixin that we expose with Vue InstantSearch. With this solution, you can expose a prop on the widget that takes an object which contains a list of search parameters to apply on the request. Here is the implementation (see below). You can find a complete example on CodeSandbox.

import { createWidgetMixin } from 'vue-instantsearch';
import { connectConfigure } from 'instantsearch.js/es/connectors';

export default {
  name: 'CustomConfigure',
  mixins: [createWidgetMixin({ connector: connectConfigure })],
  props: {
    parameters: {
      type: Object,
      required: true,
    },
  },
  computed: {
    widgetParams() {
      return {
        searchParameters: this.parameters,
      };
    },
  },
  render() {
    return null;
  },
};

You can use it like this:

<template>
  <!-- ... -->
  <custom-configure :parameters="parameters" />
</template>

<script>
export default {
  data() {
    return {
      //...
      parameters: {
        hitsPerPage: 4
      }
    };
  }
};
</script>

Hope that helps, let us know if it fixes your issue or if you have any questions.

Issue with duplicated requests when ais-configure component is used still exists :(. Workaround with custom configuration component don't work :/.

"vue-instantsearch": "^3.4.2",

@wildabeast
Copy link

This fixed it for me! https://www.algolia.com/doc/guides/building-search-ui/going-further/conditional-requests/js/#overview

Haroenv added a commit to algolia/instantsearch that referenced this issue Dec 28, 2022
…h#705)

* fix(core): prevent duplicating facet values

fixes algolia/vue-instantsearch#663

Confirmed this in: https://codesandbox.io/s/check-extra-request-higuj (you need to clone it, CodeSandbox doesn't deduplicate the dependency as expected)

* chore: update size
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants