Skip to content

Commit 01ecdaa

Browse files
author
vvo
committed
fix(rootpath): remember rootpath option on 'back' button
Before this commit, when using the rootpath option and clicking on backbutton after switching a page would result in the rootpath option seeming "forgotten" This was triggered because rootpath is a "magical" (BAD!!) option that is triggering a refinement internally in the helper's SearchParameters constructor. Thus, in the urlSync widget, to determine the "original configuration" (= all widgets getConfiguration before url sync) we need to go through the SearchParameters constructor. Also note that we originally wanted to understand the url sync "original config" as being widget's getConfiguration + widget's init but that's no doable because the way we implemented url sync. Indeed there's no way to differentiate the state of the url from the state of the widget's init action after all widget.init are called. Thus when clicking back, we are not able to tell what are the parameters we want to go back to. In a V2 of instantsearch.js we should be able to have init({helper, state, urlState}). Or maybe there's another way. I will need to discuss this with @bobylito :)
1 parent 20fc4aa commit 01ecdaa

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

src/lib/InstantSearch.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,14 @@ Usage: instantsearch({
105105
start() {
106106
if (!this.widgets) throw new Error('No widgets were added to instantsearch.js');
107107

108-
let syncWidget;
109-
110108
if (this.urlSync) {
111-
syncWidget = urlSyncWidget(this.urlSync);
109+
let syncWidget = urlSyncWidget(this.urlSync);
112110
this._createURL = syncWidget.createURL.bind(syncWidget);
113111
this._onHistoryChange = syncWidget.onHistoryChange.bind(syncWidget);
112+
this.widgets.push(syncWidget);
114113
} else {
115114
this._createURL = defaultCreateURL;
116-
this._onHistoryChange = () => {};
115+
this._onHistoryChange = function() {};
117116
}
118117

119118
this.searchParameters = this.widgets.reduce(enhanceConfiguration, this.searchParameters);
@@ -133,11 +132,6 @@ Usage: instantsearch({
133132

134133
this._init(helper.state, helper);
135134

136-
if (this.urlSync) {
137-
helper.setState(enhanceConfiguration(helper.state, syncWidget));
138-
this.widgets.push(syncWidget);
139-
}
140-
141135
helper.on('result', this._render.bind(this, helper));
142136
helper.search();
143137
}

src/lib/__tests__/InstantSearch-test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,8 @@ describe('InstantSearch lifecycle', () => {
175175

176176
it('calls urlSync.getConfiguration after every widget', () => {
177177
expect(urlSync.getConfiguration.calledOnce).toBe(true, 'urlSync.getConfiguration called once');
178-
expect(widget.init.calledAfter(widget.getConfiguration))
178+
expect(urlSync.getConfiguration.calledAfter(widget.getConfiguration))
179179
.toBe(true, 'urlSync.getConfiguration was called after widget.init');
180-
expect(urlSync.getConfiguration.getCall(0).args[0].sendMeToUrlSync)
181-
.toBe(true, 'state modifications done in widget.init should be sent to urlSync.getConfiguration');
182180
});
183181

184182
it('does not call widget.render', () => {

src/lib/url-sync.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ class URLSync {
9898
}
9999

100100
getConfiguration(currentConfiguration) {
101-
this.originalConfig = currentConfiguration;
101+
// we need to create a REAL helper to then get its state. Because some parameters
102+
// like hierarchicalFacet.rootPath are then triggering a default refinement that would
103+
// be not present if it was not going trough the SearchParameters constructor
104+
this.originalConfig = algoliasearchHelper({}, currentConfiguration.index, currentConfiguration).state;
102105
let queryString = this.urlUtils.readUrl();
103106
let config = AlgoliaSearchHelper.getConfigurationFromQueryString(queryString, {mapping: this.mapping});
104107
return config;

0 commit comments

Comments
 (0)