Skip to content

Commit

Permalink
fix(url-sync): let the user go back in history (fix #2171)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Stanislawski committed May 31, 2017
1 parent d6ff626 commit eff4532
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib/url-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ function getFullURL(relative) {
// IE <= 11 has no location.origin or buggy
function getLocationOrigin() {
// eslint-disable-next-line max-len
return `${window.location.protocol}//${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}`;
const port = window.location.port ? `:${window.location.port}` : '';
return `${window.location.protocol}//${window.location.hostname}${port}`;
}

// see InstantSearch.js file for urlSync options
Expand All @@ -102,6 +103,7 @@ class URLSync {
this.threshold = options.threshold || 700;
this.trackedParameters = options.trackedParameters || ['query', 'attribute:*', 'index', 'page', 'hitsPerPage'];
this.firstRender = true;
this.updateFromHistory = false;

this.searchParametersFromUrl = AlgoliaSearchHelper
.getConfigurationFromQueryString(
Expand Down Expand Up @@ -138,6 +140,7 @@ class URLSync {

if (isEqual(fullHelperState, fullState)) return;

this.updateFromHistory = true;
helper.overrideStateWithoutTriggeringChangeEvent(fullState).search();
}

Expand All @@ -158,9 +161,14 @@ class URLSync {
);

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

if (!this.updateFromHistory) {
this.urlUpdateTimeout = setTimeout(() => {
this.urlUtils.pushState(qs, {getHistoryState: this.getHistoryState});
}, this.threshold);
}

this.updateFromHistory = false;
}

// External API's
Expand Down

0 comments on commit eff4532

Please sign in to comment.