Skip to content

Commit 971708a

Browse files
committed
fix(history-browser): browser history state not empty on new entries in IE/Edge
Necessary for PR aurelia/router/history-state-consolidation
1 parent fca651e commit 971708a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export class BrowserHistory extends History {
5151
this.root = ('/' + this.options.root + '/').replace(rootStripper, '/');
5252

5353
this._wantsHashChange = this.options.hashChange !== false;
54-
this._hasPushState = !!(this.options.pushState && this.history && this.history.pushState);
54+
this._hasPushState = !!(this.history && this.history.pushState);
55+
this._usePushState = !!(this._hasPushState && this.options.pushState);
5556

5657
// Determine how we check the URL state.
5758
let eventName;
@@ -72,15 +73,15 @@ export class BrowserHistory extends History {
7273

7374
// If we've started off with a route from a `pushState`-enabled
7475
// browser, but we're currently in a browser that doesn't support it...
75-
if (!this._hasPushState && !atRoot) {
76+
if (!this._usePushState && !atRoot) {
7677
this.fragment = this._getFragment(null, true);
7778
this.location.replace(this.root + this.location.search + '#' + this.fragment);
7879
// Return immediately as browser will do redirect to new url
7980
return true;
8081

8182
// Or if we've started out with a hash-based route, but we're currently
8283
// in a browser where it could be `pushState`-based instead...
83-
} else if (this._hasPushState && atRoot && loc.hash) {
84+
} else if (this._usePushState && atRoot && loc.hash) {
8485
this.fragment = this._getHash().replace(routeStripper, '');
8586
this.history.replaceState({}, DOM.title, this.root + this.fragment + loc.search);
8687
}
@@ -151,6 +152,9 @@ export class BrowserHistory extends History {
151152
// If pushState is available, we use it to set the fragment as a real URL.
152153
if (this._hasPushState) {
153154
url = url.replace('//', '/');
155+
if (!this._usePushState) {
156+
url = '#' + url;
157+
}
154158
this.history[replace ? 'replaceState' : 'pushState']({}, DOM.title, url);
155159
} else if (this._wantsHashChange) {
156160
// If hash changes haven't been explicitly disabled, update the hash
@@ -189,7 +193,7 @@ export class BrowserHistory extends History {
189193
let root;
190194

191195
if (!fragment) {
192-
if (this._hasPushState || !this._wantsHashChange || forcePushState) {
196+
if (this._usePushState || !this._wantsHashChange || forcePushState) {
193197
fragment = this.location.pathname + this.location.search;
194198
root = this.root.replace(trailingSlash, '');
195199
if (!fragment.indexOf(root)) {
@@ -203,9 +207,12 @@ export class BrowserHistory extends History {
203207
return '/' + fragment.replace(routeStripper, '');
204208
}
205209

206-
_checkUrl(): boolean {
210+
_checkUrl(event: any): boolean {
207211
let current = this._getFragment();
208212
if (current !== this.fragment) {
213+
if (event.type === "popstate") {
214+
this.history.replaceState(event.state, null);
215+
}
209216
this._loadUrl();
210217
}
211218
}

0 commit comments

Comments
 (0)