Skip to content

Invalidate client cache #790

@lyubchev

Description

@lyubchev

Description

Currently meilisearch's client allows preserving the facets with count of 0 thanks to the keepZeroFacets option which can be provided to the constructor of the client. Looks like the client is using a caching system in order to achieve that.

Sometimes this results in unexpected behavior. For example, if I have page A, which displays a list of categories, and when a user clicks on that category the user is redirected to page B where more refinements could be provided based on it.
For this example, let's say a user clicks on the "Shoes" category, and after he is redirected to page B he would also like to refine the results by size. Normally you would expect to see sizes related to the "Shoes" category, such as "38", "39", "40", and if you are searching for "T-Shirts" you expect sizes such as "S", "M", "L".
The way I achieve this is using the

<VirtualMenu attribute="category" defaultRefinement={parameterFromPageA} />

component on page B which on mount refines the search with the parameter that you provided from page A (in this case "Shoes").

The problem is that the facets that you get for sizes are cached, and once they are cached if you change your category to T-Shirts you would also see the sizes for Shoes, which is counterintuitive for a user.
For now, I solved this problem by creating a new client initially when page B loads, but I am not happy with this. a better solution would be if you could somehow invalidate the current cache.

This is what my current solution looks like:

const searchClient = useMemo(createMeiliSearchClient, [])

Basic example

The way I would use this new feature would be as following:

useEffect(() => {
  searchClient.invalidateCache()
}, [])

Other
I would also be happy if you share your opinion on the way I achieve this result and if there is an intended way of doing such stuff by the API which I am missing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions