Skip to content

Commit 989856c

Browse files
author
vvo
committed
feat(urlSync): allow overriding replaceState(state)/pushState(state)
Also see #988
1 parent 0ad0935 commit 989856c

File tree

2 files changed

+11
-28
lines changed

2 files changed

+11
-28
lines changed

src/lib/InstantSearch.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ function defaultCreateURL() { return '#'; }
3838
* @param {boolean} [options.urlSync.useHash] If set to true, the url will be
3939
* hash based. Otherwise, it'll use the query parameters using the modern
4040
* history API.
41+
* @param {function} [options.urlSync.getHistoryState] Pass this function to override the
42+
* default history API state we set to `null`. For example this could be used to force passing
43+
* {turbolinks: true} to the history API every time we update it.
4144
* @return {Object} the instantsearch instance
4245
*/
4346
class InstantSearch extends EventEmitter {

src/lib/url-sync.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ let modernUrlUtils = {
5858
onpopstate: function(cb) {
5959
window.addEventListener('popstate', cb);
6060
},
61-
pushState: function(qs, options = {}) {
62-
window.history.pushState(options.pushStateObject, '', getFullURL(this.createURL(qs)));
61+
pushState: function(qs, {getHistoryState}) {
62+
window.history.pushState(getHistoryState(), '', getFullURL(this.createURL(qs)));
6363
},
64-
replaceState: function(qs, options = {}) {
65-
window.history.replaceState(options.pushStateObject, '', getFullURL(this.createURL(qs)));
64+
replaceState: function(qs, {getHistoryState}) {
65+
window.history.replaceState(getHistoryState(), '', getFullURL(this.createURL(qs)));
6666
},
6767
createURL: function(qs) {
6868
return this.character + qs + document.location.hash;
@@ -84,34 +84,14 @@ function getLocationOrigin() {
8484
return `${window.location.protocol}//${window.location.hostname}${window.location.port ? ':' + window.location.port : ''}`;
8585
}
8686

87-
/**
88-
* Instanciate a url sync widget. This widget let you synchronize the search
89-
* parameters with the URL. It can operate with legacy API and hash or it can use
90-
* the modern history API. By default, it will use the modern API, but if you are
91-
* looking for compatibility with IE8 and IE9, then you should set 'useHash' to
92-
* true.
93-
* @class
94-
* @param {UrlUtil} urlUtils an object containing the function to read, watch the changes
95-
* and update the URL.
96-
* @param {object} options may contain the following keys :
97-
* - threshold:number time in ms after which a new state is created in the browser
98-
* history. The default value is 700.
99-
* - trackedParameters:string[] parameters that will be synchronized in the
100-
* URL. By default, it will track the query, all the refinable attribute (facets and numeric
101-
* filters), the index and the page.
102-
* - useHash:boolean if set to true, the url will be hash based. Otherwise,
103-
* it'll use the query parameters using the modern history API.
104-
* - pushStateObject:object allows you to give a custom state object to `pushState`
105-
* (for example, to make this library compatible with Turbolinks, you will
106-
* need to set your state object to `{ turbolinks: true }`)
107-
*/
87+
// see InstantSearch.js file for urlSync options
10888
class URLSync {
10989
constructor(urlUtils, options) {
11090
this.urlUtils = urlUtils;
11191
this.originalConfig = null;
11292
this.timer = timerMaker(Date.now());
11393
this.mapping = options.mapping || {};
114-
this.pushStateObject = options.pushStateObject || null;
94+
this.getHistoryState = options.getHistoryState || (() => null);
11595
this.threshold = options.threshold || 700;
11696
this.trackedParameters = options.trackedParameters || ['query', 'attribute:*', 'index', 'page', 'hitsPerPage'];
11797
}
@@ -154,9 +134,9 @@ class URLSync {
154134
);
155135

156136
if (this.timer() < this.threshold) {
157-
this.urlUtils.replaceState(qs, {pushStateObject: this.pushStateObject});
137+
this.urlUtils.replaceState(qs, {getHistoryState: this.getHistoryState});
158138
} else {
159-
this.urlUtils.pushState(qs, {pushStateObject: this.pushStateObject});
139+
this.urlUtils.pushState(qs, {getHistoryState: this.getHistoryState});
160140
}
161141
}
162142

0 commit comments

Comments
 (0)