Skip to content

Commit

Permalink
fix(requestBuilder): sort facet refinements in a non-mutating manner (#…
Browse files Browse the repository at this point in the history
…6012)

introduced in #5764, but wasn't an issue until merge/setQueryParameters doesn't make a new copy constantly of the parameters.

Essentially what's going on:
- when you build the requests, it's sorting the facet values
- this then gets updated in the parameters as well as it's the same object
- with this fix, the sorting is done non-mutating (similar to .toSorted) to avoid the sorting of the parameters for cache to have a potential impact on the UI

Co-authored-by: Haroen Viaene <hello@haroen.me>
  • Loading branch information
dhayab and Haroenv committed Jan 22, 2024
1 parent d9491e1 commit 023128c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 12 additions & 6 deletions packages/algoliasearch-helper/src/requestBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,12 @@ var requestBuilder = {
.sort()
.forEach(function (facetName) {
var facetValues = facetsRefinements[facetName] || [];
facetValues.sort().forEach(function (facetValue) {
facetFilters.push(facetName + ':' + facetValue);
});
facetValues
.slice()
.sort()
.forEach(function (facetValue) {
facetFilters.push(facetName + ':' + facetValue);
});
});

var facetsExcludes = state.facetsExcludes || {};
Expand All @@ -302,9 +305,12 @@ var requestBuilder = {
}
var orFilters = [];

facetValues.sort().forEach(function (facetValue) {
orFilters.push(facetName + ':' + facetValue);
});
facetValues
.slice()
.sort()
.forEach(function (facetValue) {
orFilters.push(facetName + ':' + facetValue);
});

facetFilters.push(orFilters);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ function getData() {
index: 'test_hotels-node',
disjunctiveFacets: ['city'],
disjunctiveFacetsRefinements: {
city: ['New York', 'Paris'],
// Note: these are in the same order as the refinements above, not sorted
city: ['Paris', 'New York'],
},
});

Expand Down

0 comments on commit 023128c

Please sign in to comment.