Skip to content

Commit ae6dbf6

Browse files
author
vvo
committed
fix(base href): always create absolute URLS in widgets
Every time we generate a link, much like every time we update the url bar, we should update with an absolute url to avoid the <base href> being added. fixes #970
1 parent 885e4d8 commit ae6dbf6

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/lib/InstantSearch.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ Usage: instantsearch({
108108
if (this.urlSync) {
109109
let syncWidget = urlSyncWidget(this.urlSync);
110110
this._createURL = syncWidget.createURL.bind(syncWidget);
111+
this._createAbsoluteURL = relative => this._createURL(relative, {absolute: true});
111112
this._onHistoryChange = syncWidget.onHistoryChange.bind(syncWidget);
112113
this.widgets.push(syncWidget);
113114
} else {
114115
this._createURL = defaultCreateURL;
116+
this._createAbsoluteURL = defaultCreateURL;
115117
this._onHistoryChange = function() {};
116118
}
117119

@@ -160,7 +162,7 @@ Usage: instantsearch({
160162
results,
161163
state,
162164
helper,
163-
createURL: this._createURL
165+
createURL: this._createAbsoluteURL
164166
});
165167
}, this);
166168
this.emit('render');
@@ -174,7 +176,7 @@ Usage: instantsearch({
174176
state,
175177
helper,
176178
templatesConfig,
177-
createURL: this._createURL,
179+
createURL: this._createAbsoluteURL,
178180
onHistoryChange: _onHistoryChange
179181
});
180182
}

src/lib/__tests__/InstantSearch-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ describe('InstantSearch lifecycle', () => {
195195
expect(widget.render.calledOnce).toBe(true, 'widget.render called once');
196196
expect(widget.render.args[0])
197197
.toEqual([{
198-
createURL: search._createURL,
198+
createURL: search._createAbsoluteURL,
199199
results,
200200
state: helper.state,
201201
helper,

src/lib/url-sync.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function getFullURL(relative) {
8080
return getLocationOrigin() + window.location.pathname + relative;
8181
}
8282

83-
// IE <= 11 has no location.origin
83+
// IE <= 11 has no location.origin or buggy
8484
function getLocationOrigin() {
8585
return `${window.location.protocol}//${window.location.hostname}${window.location.port ? ':' + window.location.port : ''}`;
8686
}
@@ -147,14 +147,15 @@ class URLSync {
147147

148148
// External API's
149149

150-
createURL(state) {
150+
createURL(state, {absolute}) {
151151
let currentQueryString = this.urlUtils.readUrl();
152152
let filteredState = state.filter(this.trackedParameters);
153153
let foreignConfig = algoliasearchHelper.url.getUnrecognizedParametersInQueryString(currentQueryString, {mapping: this.mapping});
154154
// Add instantsearch version to reconciliate old url with newer versions
155155
foreignConfig.is_v = majorVersionNumber;
156+
const relative = this.urlUtils.createURL(algoliasearchHelper.url.getQueryStringFromState(filteredState, {mapping: this.mapping}));
156157

157-
return this.urlUtils.createURL(algoliasearchHelper.url.getQueryStringFromState(filteredState, {mapping: this.mapping}));
158+
return absolute ? getFullURL(relative) : relative;
158159
}
159160

160161
onHistoryChange(fn) {

0 commit comments

Comments
 (0)