-
Notifications
You must be signed in to change notification settings - Fork 58
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: avoid using the problematic beforeunload event #344
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5c6a7aa
fix: avoid using the problematic beforeunload event
hedgepigdaniel 424abd1
Account for pageshow happenning after initialisation
hedgepigdaniel 3738563
Add tests for pageshow/pagehide handling
hedgepigdaniel eb48f80
refactor: use page-lifecycle library
hedgepigdaniel 7433a94
Remove this._hasSetScrollRestoration
hedgepigdaniel 9dc79fd
simplify logic for saving/restoring scroll restoration
hedgepigdaniel a3626c3
reset scrollRestoration before each test
hedgepigdaniel 95e8618
Fix initialization of variable to happen before it is used
hedgepigdaniel 4ad220f
correct assertion
hedgepigdaniel a1369b6
cover case of page initialisation hapenning after scroll ehavior init…
hedgepigdaniel 6558236
update comment
hedgepigdaniel 728190c
dedupe deps
taion 5ff993b
Merge branch 'master' into fix/beforeunload
taion 7f96586
format
taion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import scrollLeft from 'dom-helpers/query/scrollLeft'; | |
import scrollTop from 'dom-helpers/query/scrollTop'; | ||
import requestAnimationFrame from 'dom-helpers/util/requestAnimationFrame'; | ||
import invariant from 'invariant'; | ||
import PageLifecycle from 'page-lifecycle/dist/lifecycle.es5'; | ||
|
||
import { isMobileSafari } from './utils'; | ||
|
||
|
@@ -22,32 +23,27 @@ export default class ScrollBehavior { | |
this._stateStorage = stateStorage; | ||
this._getCurrentLocation = getCurrentLocation; | ||
this._shouldUpdateScroll = shouldUpdateScroll; | ||
this._oldScrollRestoration = null; | ||
|
||
// This helps avoid some jankiness in fighting against the browser's | ||
// default scroll behavior on `POP` transitions. | ||
/* istanbul ignore else: Travis browsers all support this */ | ||
if ( | ||
'scrollRestoration' in window.history && | ||
// Unfortunately, Safari on iOS freezes for 2-6s after the user swipes to | ||
// navigate through history with scrollRestoration being 'manual', so we | ||
// need to detect this browser and exclude it from the following code | ||
// until this bug is fixed by Apple. | ||
!isMobileSafari() | ||
) { | ||
this._oldScrollRestoration = window.history.scrollRestoration; | ||
try { | ||
window.history.scrollRestoration = 'manual'; | ||
|
||
// Scroll restoration persists across page reloads. We want to reset | ||
// this to the original value, so that we can let the browser handle | ||
// restoring the initial scroll position on server-rendered pages. | ||
on(window, 'beforeunload', this._restoreScrollRestoration); | ||
} catch (e) { | ||
this._oldScrollRestoration = null; | ||
this._setScrollRestoration(); | ||
|
||
// Scroll restoration persists across page reloads. We want to reset | ||
// this to the original value, so that we can let the browser handle | ||
// restoring the initial scroll position on server-rendered pages. | ||
PageLifecycle.addEventListener('statechange', ({ newState }) => { | ||
if ( | ||
newState === 'terminated' || | ||
newState === 'frozen' || | ||
newState === 'discarded' | ||
) { | ||
this._restoreScrollRestoration(); | ||
} else { | ||
this._setScrollRestoration(); | ||
} | ||
hedgepigdaniel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
this._oldScrollRestoration = null; | ||
} | ||
}); | ||
|
||
this._saveWindowPositionHandle = null; | ||
this._checkWindowScrollHandle = null; | ||
|
@@ -151,6 +147,28 @@ export default class ScrollBehavior { | |
}); | ||
} | ||
|
||
_setScrollRestoration = () => { | ||
if (this._oldScrollRestoration) { | ||
// It's possible that we already set the scroll restoration | ||
return; | ||
} | ||
if ( | ||
'scrollRestoration' in window.history && | ||
// Unfortunately, Safari on iOS freezes for 2-6s after the user swipes to | ||
// navigate through history with scrollRestoration being 'manual', so we | ||
// need to detect this browser and exclude it from the following code | ||
// until this bug is fixed by Apple. | ||
!isMobileSafari() | ||
) { | ||
this._oldScrollRestoration = window.history.scrollRestoration; | ||
try { | ||
window.history.scrollRestoration = 'manual'; | ||
} catch (e) { | ||
this._oldScrollRestoration = null; | ||
} | ||
} | ||
}; | ||
|
||
_restoreScrollRestoration = () => { | ||
/* istanbul ignore if: not supported by any browsers on Travis */ | ||
if (this._oldScrollRestoration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so just bail if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ive replaced this with just |
||
|
@@ -159,6 +177,7 @@ export default class ScrollBehavior { | |
} catch (e) { | ||
/* silence */ | ||
} | ||
this._oldScrollRestoration = null; | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
let listener; | ||
|
||
export const setEventListener = (eventType, callback) => { | ||
listener = callback; | ||
}; | ||
|
||
export const triggerEvent = (oldState, newState) => { | ||
if (listener) { | ||
const event = new Event('statechange'); | ||
event.newState = newState; | ||
event.oldState = oldState; | ||
listener(event); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because GoogleChromeLabs/page-lifecycle#7