From d63f82c8da19a81bef8afd55601b1570404c3d23 Mon Sep 17 00:00:00 2001 From: Aaron Sikes Date: Wed, 10 May 2017 18:16:19 -0400 Subject: [PATCH] [BUGFIX beta] Preserve current history state --- .../lib/location/history_location.js | 9 +++++++- .../tests/location/history_location_test.js | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/ember-routing/lib/location/history_location.js b/packages/ember-routing/lib/location/history_location.js index 1834aad836a..51c734f6827 100644 --- a/packages/ember-routing/lib/location/history_location.js +++ b/packages/ember-routing/lib/location/history_location.js @@ -64,7 +64,14 @@ export default EmberObject.extend({ this.supportsHistory = true; } - this.replaceState(this.formatURL(this.getURL())); + let state = this.getState(); + let path = this.formatURL(this.getURL()); + if (state && state.path === path) { // preserve existing state + // used for webkit workaround, since there will be no initial popstate event + this._previousURL = this.getURL(); + } else { + this.replaceState(path); + } }, /** diff --git a/packages/ember-routing/tests/location/history_location_test.js b/packages/ember-routing/tests/location/history_location_test.js index 7e73d3304b7..c50d176276c 100644 --- a/packages/ember-routing/tests/location/history_location_test.js +++ b/packages/ember-routing/tests/location/history_location_test.js @@ -304,3 +304,25 @@ QUnit.test('HistoryLocation.getURL() drops duplicate slashes', function() { equal(location.getURL(), '/'); }); + +QUnit.test('Existing state is preserved on init', function() { + expect(1); + + let existingState = { + path: '/route/path', + uuid: 'abcd' + }; + + FakeHistory.state = existingState; + + HistoryTestLocation.reopen({ + init() { + this._super(...arguments); + set(this, 'location', mockBrowserLocation('/route/path')); + } + }); + + createLocation(); + location.initState(); + deepEqual(location.getState(), existingState); +});