Skip to content

Commit

Permalink
[state] don't make extra $location.replace() calls (#9954)
Browse files Browse the repository at this point in the history
Backports PR #8179

**Commit 1:**
[state] don't make extra $location.replace() calls

Issue:
In order to ensure BWC in the new state-hashing changes, rison encoded query string parameters are automatically converted into hashes and placed back into the URL via `$location.search().replace()`. This ensures that extra history entries don't get created, but this is still happening when hashing is disabled (which is now the default). This causes every state-caused history change to become a replacement, which mutilates the history stack.

Fix:
Added a `#isHashingEnabled()` method to the state objects that is called before trying to convert rison encoded query string states and replacing them in the URL.

* Original sha: 9c52032
* Authored by spalger <email@spalger.com> on 2016-09-07T17:29:12Z
  • Loading branch information
elastic-jasper authored and spalger committed Feb 1, 2017
1 parent fef23c8 commit f0ee72d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/ui/public/state_management/__tests__/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ describe('State Management', function () {
stateObj = state.toObject();
expect(stateObj).to.eql({});
});

it('does not replace the state value on read', () => {
const { state } = setup();
sinon.stub($location, 'search', (newSearch) => {
if (newSearch) {
return $location;
} else {
return {
[state.getQueryParamName()]: '(a:1)'
};
}
});
const replaceStub = sinon.stub($location, 'replace').returns($location);

state.fetch();
sinon.assert.notCalled(replaceStub);
});
});

describe('Hashing', () => {
Expand Down
18 changes: 14 additions & 4 deletions src/ui/public/state_management/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ export default function StateProvider(Private, $rootScope, $location, config) {
$location.search(search).replace();
}

if (risonEncoded) {
if (!risonEncoded) {
return null;
}

if (this.isHashingEnabled()) {
// RISON can find its way into the URL any number of ways, including the navbar links or
// shared urls with the entire state embedded. These values need to be translated into
// hashes and replaced in the browser history when state-hashing is enabled
search[this._urlParam] = this.toQueryParam(risonEncoded);
$location.search(search).replace();
return risonEncoded;
}

return null;
return risonEncoded;
};

/**
Expand Down Expand Up @@ -209,13 +215,17 @@ export default function StateProvider(Private, $rootScope, $location, config) {
return rison.encode(this._parseQueryParamValue(hash));
};

State.prototype.isHashingEnabled = function () {
return !!config.get('state:storeInSessionStorage');
};

/**
* Produce the hash version of the state in it's current position
*
* @return {string}
*/
State.prototype.toQueryParam = function (state = this.toObject()) {
if (!config.get('state:storeInSessionStorage')) {
if (!this.isHashingEnabled()) {
return rison.encode(state);
}

Expand Down

0 comments on commit f0ee72d

Please sign in to comment.