@@ -60,7 +60,6 @@ export class ScrollService {
60
60
// the type is `hashchange` when the fragment identifier of the URL has changed. It allows us to go to position
61
61
// just before a click on an anchor
62
62
if ( event . type === 'hashchange' ) {
63
- this . popStateFired = false ;
64
63
this . scrollToPosition ( ) ;
65
64
} else {
66
65
// The popstate event is always triggered by doing a browser action such as a click on the back or forward button.
@@ -94,17 +93,23 @@ export class ScrollService {
94
93
}
95
94
96
95
/**
97
- * When we load a document, we have to scroll to the correct position depending on whether this is a new location
98
- * , a back/forward in the history, or a refresh
96
+ * When we load a document, we have to scroll to the correct position depending on whether this is a new location,
97
+ * a back/forward in the history, or a refresh
99
98
* @param delay before we scroll to the good position
100
99
*/
101
100
scrollAfterRender ( delay : number ) {
102
- // If we do rendering following a refresh, we use the scroll Position from the storage.
103
- if ( this . getStoredScrollPosition ( ) ) {
104
- this . viewportScroller . scrollToPosition ( this . getStoredScrollPosition ( ) ! ) ;
101
+ // If we do rendering following a refresh, we use the scroll position from the storage.
102
+ const storedScrollPosition = this . getStoredScrollPosition ( ) ;
103
+ if ( storedScrollPosition ) {
104
+ this . viewportScroller . scrollToPosition ( storedScrollPosition ) ;
105
105
} else {
106
- if ( ! this . needToFixScrollPosition ( ) ) {
107
- // The document was reloaded following a link. If the location contains a hash, we have to wait for async
106
+ if ( this . needToFixScrollPosition ( ) ) {
107
+ // The document was reloaded following a popState `event` (called by the forward/back button), so we manage
108
+ // the scroll position
109
+ this . scrollToPosition ( ) ;
110
+ } else {
111
+ // The document was loaded either of the following cases: a direct navigation via typing the URL in the
112
+ // address bar or a click on a link. If the location contains a hash, we have to wait for async
108
113
// layout.
109
114
if ( this . isLocationWithHash ( ) ) {
110
115
// Scroll 500ms after the new document has been inserted into the doc-viewer.
@@ -114,9 +119,6 @@ export class ScrollService {
114
119
// If the location doesn't contain a hash, we scroll to the top of the page.
115
120
this . scrollToTop ( ) ;
116
121
}
117
- } else {
118
- // The document was reloaded following a popState `event`, so we manage the scroll scrollPosition
119
- this . scrollToPosition ( ) ;
120
122
}
121
123
}
122
124
}
0 commit comments