Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(table): use stable sort algorithm to prevent SSR issues (#1399)
* Create stable-sort.js * add stable sort to utils * [b-table] use stableSort
- Loading branch information
1 parent
4ccd6cc
commit 21b33f2
Showing
3 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Consitant and stable sort function across JavsaScript platforms | ||
* | ||
* Inconsistant sorts can cause SSR problems between client and server | ||
* such as in <b-table> if sortBy is applied to the data on server side render. | ||
* Chrome and V8 native sorts are inconsistant/unstable | ||
* | ||
* This function uses native sort with fallback to index compare when the a and b | ||
* compare returns 0 | ||
* | ||
* Algorithm bsaed on: | ||
* https://stackoverflow.com/questions/1427608/fast-stable-sorting-algorithm-implementation-in-javascript/45422645#45422645 | ||
* | ||
* @param {array} array to sort | ||
* @param {function} sortcompare function | ||
* @return {array} | ||
*/ | ||
|
||
export default function stableSort (array, compareFn) { | ||
// Using `.bind(compareFn)` on the wrapped anonymous function improves | ||
// performance by avoiding the function call setup. We don't use an arrow | ||
// function here as it binds `this` to the `stableSort` context rather than | ||
// the `compareFn` context, which wouldn't give us the performance increase. | ||
return array | ||
.map((a, index) => [ index, a ]) | ||
.sort(function (a, b) { return this(a[1], b[1]) || a[0] - b[0] }.bind(compareFn)) | ||
.map(e => e[1]) | ||
} |