-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Blazor NavigationManager.NavigateTo always scrolls page to the top. #40190
Comments
.NET6 has a new API to manipulate the query string Did you try that? |
@rogihee Thanks for the tip. I have tried this. That api does not actually navigate to anything it just gets a new value for the querystring. This does not really solve the issue as I still have to navigate to that uri to update the browser url and the NavigateTo call is what is scrolling the viewport needlessly. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Just to bump up - this behaviour affects our application as well (quite significantly). Is there any workaround for that until the issue gets resolved? |
@lukblazewicz I have had some success using JS interop to capture the current page scroll position and and then trying to reset the position after the update query string operation completes. I had to add some random Task.Delay statements in my C# code to get things to work and the user experience is still pretty bad as the page jumps up and then back down but at least your users don't have to scroll manually back to where they were. Hopefully this helps. |
@mkArtakMSFT I'm not sure why this is tagged as an enhancement, I'm fairly certain this it is a regression from Blazor on .NET 5. The issue did not manifest until I upgraded from .NET 5 to .NET 6. This regression really impacts user experience of this product. |
@groogiam Thanks for the tip, we will have to consider it if there is little hope to get this bug fixed soon. |
Look at the Workaround from @smirceamihai: |
@dp-sgr Disregard my last post. This does work. There is some code in a third party component libraries that scrolls a different element to top. This solution does work for the window scroll. Thanks again for pointing this work around out. |
This is a non-trivial blocker for me. Imagine the common filter experience where facets are stored in the URL as query strings. Whenever you'd call We're using Blazor Server, and afaik there's no way to manually keep NavManager in sync with a workaround that uses js I found this bit in the source code, being able to conditionally call function performInternalNavigation(absoluteInternalHref: string, interceptedLink: boolean, replace: boolean) {
// Since this was *not* triggered by a back/forward gesture (that goes through a different
// code path starting with a popstate event), we don't want to preserve the current scroll
// position, so reset it.
// To avoid ugly flickering effects, we don't want to change the scroll position until
// we render the new page. As a best approximation, wait until the next batch.
resetScrollAfterNextBatch();
if (!replace) {
history.pushState(null, /* ignored title */ '', absoluteInternalHref);
} else {
history.replaceState(null, /* ignored title */ '', absoluteInternalHref);
}
notifyLocationChanged(interceptedLink);
} /cc @SteveSandersonMS I noticed you introduced this scrolling functionality in #12423, perhaps you may have more relevant insight into the issue and possible workarounds. I appreciate any feedback. |
Auto-scrolling to top is causing issues for me as well, particularly in mobile layouts where screen is more narrow. I attempted to use JS interop for window.history, but that has side-effects w/ NaviationManager.LocationChanged and OnParameterSet[Async]. A |
This appears to be causing issues for third-party component libraries as well: |
Yes, also affecting my WASM app which uses anchor navigation in headers. When the user clicks on a header link (which only adds an anchor fragment, e.g. |
This creates an unpleasant experience. |
.NET 7 has already closed for new features, but I can see this is a common requirement so it's definitely something we can consider for .NET 8. In the meantime, as a workaround, you could implement manual control over this using JavaScript. For example, <!-- In index.html -->
<script>
const origScrollTo = window.scrollTo;
window.scrollTo = (x, y) => {
const shouldSkip = true; // TODO: Insert your own logic here
if (x === 0 && y === 0 && shouldSkip)
return;
return origScrollTo.apply(this, arguments);
};
</script> You'd have to put in some logic of your own to control the I know it's not very convenient so I do agree we should offer built-in control for this! |
Thanks for contacting us. We're moving this issue to the |
Thanks for contacting us. We're moving this issue to the |
@DerekFoulk we workaround this by adding |
@KevinH404 Thanks for the tip! In my case- I'm just an idiot... I was using |
Saw this while using static SSR with streaming... solved with this note from the docs:
Obviously won't help if you are doing anything interactive with |
I just found this blog in which it is explained how to disable the enhanced navigation. Globally using the solution @AndrewTriesToCode mention but also locally. The following examples disable enhanced navigation using the data-enhance-nav attribute. Below, data-enhance-nav="false" disables the feature for a single link instance. <a href="redirect" data-enhance-nav="false">
GET without enhanced navigation
</a> |
Nice find. @sonnemaf I have to admit for SSR I did NOT expect that enhanced navigation would be in play at all especially for local anchors. |
Is there an existing issue for this?
Describe the bug
It appears the navigation manger in .NET 6 always scrolls the page to the top even if NavigateTo call is just updating the query string.
Expected Behavior
Navigations that do not change the page should not reset the viewport position.
Steps To Reproduce
Will post a repro shortly.
Exceptions (if any)
No response
.NET Version
6.0.102
Anything else?
No response
The text was updated successfully, but these errors were encountered: