Skip to content

Commit

Permalink
fix(urlSync): update url only after threshold (#1917)
Browse files Browse the repository at this point in the history
Before this commit, our default implementation tried to update
the url at every character. This led to many slowdown reports from users
of the library.

Now we only update the url after the threshold

fixes #1856
  • Loading branch information
vvo authored Jan 27, 2017
1 parent 19756c3 commit b0f0cf1
Showing 1 changed file with 0 additions and 19 deletions.
19 changes: 0 additions & 19 deletions src/lib/url-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function timerMaker(t0) {
* @property {string} character the character used in the url
* @property {function} onpopstate add an event listener for the URL change
* @property {function} pushState creates a new entry in the browser history
* @property {function} replaceState update the current entry of the browser history
* @property {function} readUrl reads the query string of the parameters
*/

Expand All @@ -39,9 +38,6 @@ const hashUrlUtils = {
pushState(qs) {
window.location.assign(getFullURL(this.createURL(qs)));
},
replaceState(qs) {
window.location.replace(getFullURL(this.createURL(qs)));
},
createURL(qs) {
return window.location.search + this.character + qs;
},
Expand All @@ -62,9 +58,6 @@ const modernUrlUtils = {
pushState(qs, {getHistoryState}) {
window.history.pushState(getHistoryState(), '', getFullURL(this.createURL(qs)));
},
replaceState(qs, {getHistoryState}) {
window.history.replaceState(getHistoryState(), '', getFullURL(this.createURL(qs)));
},
createURL(qs) {
return this.character + qs + document.location.hash;
},
Expand Down Expand Up @@ -95,9 +88,6 @@ class URLSync {
this.mapping = options.mapping || {};
this.getHistoryState = options.getHistoryState || (() => null);
this.threshold = options.threshold || 700;
this.updateOnEveryKeyStroke = options.updateOnEveryKeyStroke !== undefined ?
options.updateOnEveryKeyStroke :
true;
this.trackedParameters = options.trackedParameters || ['query', 'attribute:*', 'index', 'page', 'hitsPerPage'];

this.searchParametersFromUrl = AlgoliaSearchHelper
Expand Down Expand Up @@ -154,15 +144,6 @@ class URLSync {
}
);

if (this.updateOnEveryKeyStroke === true) {
if (this.timer() < this.threshold) {
this.urlUtils.replaceState(qs, {getHistoryState: this.getHistoryState});
} else {
this.urlUtils.pushState(qs, {getHistoryState: this.getHistoryState});
}
return;
}

clearTimeout(this.urlUpdateTimeout);
this.urlUpdateTimeout = setTimeout(() => {
this.urlUtils.pushState(qs, {getHistoryState: this.getHistoryState});
Expand Down

0 comments on commit b0f0cf1

Please sign in to comment.