-
Couldn't load subscription status.
- Fork 10.5k
Fix fetch request in data-enhance-nav="false" for same-page anchors #63449
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 fetch request in data-enhance-nav="false" for same-page anchors #63449
Conversation
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.
Pull Request Overview
This pull request fixes an issue where navigation to same-page anchors (hash-only URL changes) with data-enhance-nav="false" would still trigger unnecessary fetch requests. The fix introduces better detection of hash-only changes to prevent full page reloads and improve performance when navigating to anchors within the same page.
- Added
isHashOnlyChangeutility function to detect when navigation only changes the URL hash - Updated
onPopStatehandler to skip enhanced navigation logic for hash-only changes - Added comprehensive test coverage to verify the fix prevents unnecessary fetch requests
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| NavigationUtils.ts | Added isHashOnlyChange utility function to detect hash-only URL changes |
| NavigationEnhancement.ts | Updated onPopState handler to use isHashOnlyChange and skip enhanced navigation |
| PageForScrollingToHash.razor | Added test markup with data-enhance-nav="false" anchor for testing |
| EnhancedNavigationTest.cs | Added end-to-end test to verify no fetch requests occur for non-enhanced hash navigation |
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.
We have the link scenaro covered in the test (<a/> element).
Let's also make sure that other cases work as expected:
- Programmatic navigation (<button @OnClick="@(() => NavigationManager.NavigateTo(ulr#otherHash))">)
NavLinknavigation (<NavLink href="ulr#otherHash">)- POST navigation, a form that uses custom
onsumbitmethod with redirection to "ulr#otherHash" and disables enhanced nav. A variation on: https://github.com/dotnet/aspnetcore/blob/2cd7fb4901bfe710f2cf0ab23e411d7f7ef5f364/src/Components/test/testassets/TestContentPackage/NotFound/ComponentThatPostsNotFound.razor.
NavigateTo behaves differently from |
|
/backport to release/10.0 |
|
Started backporting to release/10.0: https://github.com/dotnet/aspnetcore/actions/runs/18220339527 |
Fix fetch request in data-enhance-nav="false" for same-page anchors
Description
This pull request enhances the navigation logic to better handle hash-only changes in URLs, ensuring that navigation to anchors within the same page does not trigger a full page reload or unnecessary fetch requests. It introduces a utility function to detect hash-only changes and updates the navigation handler to leverage this, improving performance and user experience. Additional end-to-end tests and markup updates verify and demonstrate the new behavior.
Changes:
isHashOnlyChangeutility function inNavigationUtils.tsto detect when a navigation event only changes the hash fragment, avoiding unnecessary page loads.onPopStatehandler inNavigationEnhancement.tsto useisHashOnlyChange, preventing enhanced navigation logic from running when only the hash changes.EnhancedNavigationTest.csto verify that non-enhanced navigation to a hash does not trigger a page fetch, ensuring the scroll behavior works as expected without unnecessary network activity.Fixes #63396