Skip to content

Commit 65cbf50

Browse files
committed
Let the browser handle history produced by forwards/backwards actions and enhanced nav handle the history produced by onDocumentClick.
1 parent 9a1d687 commit 65cbf50

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/Components/Web.JS/src/Services/NavigationEnhancement.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,25 @@ function onDocumentClick(event: MouseEvent) {
132132
});
133133
}
134134

135-
function onPopState(state: PopStateEvent) {
135+
function removeScrollPositionFromState() {
136+
const currentState = history.state || {};
137+
const { scrollPosition, ...rest } = currentState;
138+
history.replaceState(Object.keys(rest).length ? rest : null, /* ignored title */ '', location.href);
139+
}
140+
141+
function onPopState(event: PopStateEvent) {
136142
if (hasInteractiveRouter()) {
137143
return;
138144
}
139145

140146
// load the new page
141-
const scrollPosition = history.state?.scrollPosition;
142-
saveScrollPosition();
143147
performEnhancedPageLoad(location.href, /* interceptedLink */ false).then(() => {
148+
const scrollPosition = event.state?.scrollPosition;
144149
if (scrollPosition !== undefined &&
145150
(scrollPosition.X !== window.scrollX || scrollPosition.Y !== window.scrollY)) {
146151
window.scrollTo(scrollPosition.X, scrollPosition.Y);
152+
// let the browser handle scroll restoration for the history produced by forwards/backwards actions
153+
removeScrollPositionFromState();
147154
}
148155
})
149156
}

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollPositionTests.razor

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
<NavLink id="do-navigation-with-fragment" href="@($"nav/scroll-to-hash#some-content")">
1616
Navigation to another page with fragment (scroll-to-hash#some-content)
1717
</NavLink>
18+
<NavLink id="do-navigation" href="@($"nav/scroll-to-hash")">
19+
Navigation to another page (scroll-to-hash)
20+
</NavLink>
1821
}
1922

2023
<div style="height: 2000px; border: 2px dashed red;">spacer bottom</div>
@@ -23,10 +26,6 @@
2326
{
2427
<h2 id="some-content">Some content</h2>
2528
<p>This is the content.</p>
26-
27-
<NavLink id="do-navigation" href="@($"nav/scroll-to-hash")">
28-
Navigation to another page (scroll-to-hash)
29-
</NavLink>
3029
}
3130

3231
@code {

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollPositionTestsNoStreaming.razor

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
<NavLink id="do-navigation-with-fragment" href="@($"nav/scroll-to-hash-no-streaming#some-content")">
1616
Navigation to another page with fragment (scroll-to-hash#some-content)
1717
</NavLink>
18+
<NavLink id="do-navigation" href="@($"nav/scroll-to-hash-no-streaming")">
19+
Navigation to another page (scroll-to-hash)
20+
</NavLink>
1821
}
1922

2023
<div style="height: 2000px; border: 2px dashed red;">spacer bottom</div>
@@ -23,10 +26,6 @@
2326
{
2427
<h2 id="some-content">Some content</h2>
2528
<p>This is the content.</p>
26-
27-
<NavLink id="do-navigation" href="@($"nav/scroll-to-hash-no-streaming")">
28-
Navigation to another page (scroll-to-hash)
29-
</NavLink>
3029
}
3130

3231
@code {

0 commit comments

Comments
 (0)