Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/yangmingshan/history into…
Browse files Browse the repository at this point in the history
… yangmingshan-master
  • Loading branch information
mjackson committed Sep 11, 2019
2 parents ad82376 + 3567ed3 commit 54e8948
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 18 additions & 4 deletions modules/DOMUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,24 @@ export function supportsGoWithoutReloadUsingHash() {
}

/**
* Returns true if a given popstate event is an extraneous WebKit event.
* Returns true if a given popstate event is an extraneous WebKit event,
* or it fired on initial page load (the solution is from page.js [https://github.com/visionmedia/page.js]).
* Accounts for the fact that Chrome on iOS fires real popstate events
* containing undefined state when pressing the back button.
*/
export function isExtraneousPopstateEvent(event) {
return event.state === undefined && navigator.userAgent.indexOf('CriOS') === -1;
}
export const shouldIgnorePopstateEvent = (() => {
let loaded = false;
if (document.readyState === 'complete') {
loaded = true;
} else {
window.addEventListener('load', () =>
setTimeout(() => {
loaded = true;
})
);
}
return event =>
(event.state === undefined &&
navigator.userAgent.indexOf('CriOS') === -1) ||
!loaded;
})();
5 changes: 2 additions & 3 deletions modules/createBrowserHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getConfirmation,
supportsHistory,
supportsPopStateOnHashChange,
isExtraneousPopstateEvent
shouldIgnorePopstateEvent
} from './DOMUtils.js';
import invariant from './invariant.js';
import warning from './warning.js';
Expand Down Expand Up @@ -86,8 +86,7 @@ function createBrowserHistory(props = {}) {
}

function handlePopState(event) {
// Ignore extraneous popstate events in WebKit.
if (isExtraneousPopstateEvent(event)) return;
if (shouldIgnorePopstateEvent(event)) return;
handlePop(getDOMLocation(event.state));
}

Expand Down

0 comments on commit 54e8948

Please sign in to comment.