Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix history.listen triggered by initial page load on some old browsers #555

Merged
merged 2 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions modules/DOMUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ export const supportsGoWithoutReloadUsingHash = () =>
window.navigator.userAgent.indexOf("Firefox") === -1;

/**
* 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 const isExtraneousPopstateEvent = event =>
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 @@ -14,7 +14,7 @@ import {
getConfirmation,
supportsHistory,
supportsPopStateOnHashChange,
isExtraneousPopstateEvent
shouldIgnorePopstateEvent
} from "./DOMUtils";

const PopStateEvent = "popstate";
Expand Down Expand Up @@ -87,8 +87,7 @@ const createBrowserHistory = (props = {}) => {
};

const handlePopState = event => {
// Ignore extraneous popstate events in WebKit.
if (isExtraneousPopstateEvent(event)) return;
if (shouldIgnorePopstateEvent(event)) return;

handlePop(getDOMLocation(event.state));
};
Expand Down