Skip to content

Commit

Permalink
fix(base href): always create absolute URLS in widgets
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vvo committed Apr 28, 2016
1 parent 885e4d8 commit ae6dbf6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/lib/InstantSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ Usage: instantsearch({
if (this.urlSync) {
let syncWidget = urlSyncWidget(this.urlSync);
this._createURL = syncWidget.createURL.bind(syncWidget);
this._createAbsoluteURL = relative => this._createURL(relative, {absolute: true});
this._onHistoryChange = syncWidget.onHistoryChange.bind(syncWidget);
this.widgets.push(syncWidget);
} else {
this._createURL = defaultCreateURL;
this._createAbsoluteURL = defaultCreateURL;
this._onHistoryChange = function() {};
}

Expand Down Expand Up @@ -160,7 +162,7 @@ Usage: instantsearch({
results,
state,
helper,
createURL: this._createURL
createURL: this._createAbsoluteURL
});
}, this);
this.emit('render');
Expand All @@ -174,7 +176,7 @@ Usage: instantsearch({
state,
helper,
templatesConfig,
createURL: this._createURL,
createURL: this._createAbsoluteURL,
onHistoryChange: _onHistoryChange
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/__tests__/InstantSearch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('InstantSearch lifecycle', () => {
expect(widget.render.calledOnce).toBe(true, 'widget.render called once');
expect(widget.render.args[0])
.toEqual([{
createURL: search._createURL,
createURL: search._createAbsoluteURL,
results,
state: helper.state,
helper,
Expand Down
7 changes: 4 additions & 3 deletions src/lib/url-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function getFullURL(relative) {
return getLocationOrigin() + window.location.pathname + relative;
}

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

// External API's

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

return this.urlUtils.createURL(algoliasearchHelper.url.getQueryStringFromState(filteredState, {mapping: this.mapping}));
return absolute ? getFullURL(relative) : relative;
}

onHistoryChange(fn) {
Expand Down

0 comments on commit ae6dbf6

Please sign in to comment.