Skip to content

Conversation

dariatiurina
Copy link
Contributor

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:

  • Added the isHashOnlyChange utility function in NavigationUtils.ts to detect when a navigation event only changes the hash fragment, avoiding unnecessary page loads.
  • Updated the onPopState handler in NavigationEnhancement.ts to use isHashOnlyChange, preventing enhanced navigation logic from running when only the hash changes.
  • Added a new end-to-end test in EnhancedNavigationTest.cs to 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

@dariatiurina dariatiurina self-assigned this Aug 27, 2025
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Aug 27, 2025
@dariatiurina dariatiurina marked this pull request as ready for review August 27, 2025 10:59
@Copilot Copilot AI review requested due to automatic review settings August 27, 2025 10:59
@dariatiurina dariatiurina requested a review from a team as a code owner August 27, 2025 10:59
Copy link
Contributor

@Copilot Copilot AI left a 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 isHashOnlyChange utility function to detect when navigation only changes the URL hash
  • Updated onPopState handler 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

Copy link
Member

@ilonatommy ilonatommy left a 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:

@dariatiurina
Copy link
Contributor Author

We have the link scenaro covered in the test (<a/> element). Let's also make sure that other cases work as expected:

NavigateTo behaves differently from <a/> and <NavLink/> with enhanced navigation, so this bug does not affect them.

Comment on lines 54 to 55
return a.origin === b.origin && a.pathname === b.pathname
&& a.search === b.search && b.hash !== '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this correct? This is just checking whether the new url has a hash, but it it's not checking that it has a different hash.

If a is https://www.example.com/path?query=value#something and b is https://www.example.com/path?query=value#something that function will return true, when the hash is the same, isn't it?

Also, what's with the try..catch here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this correct? This is just checking whether the new url has a hash, but it it's not checking that it has a different hash.

If a is https://www.example.com/path?query=value#something and b is https://www.example.com/path?query=value#something that function will return true, when the hash is the same, isn't it?

Also, what's with the try..catch here.

try..catch was to catch error during the conversion, but I see that isForSamePath in the same file doesn't care about those errors, so I will remove try..catch

About different hash - naming problem. Sorry. This is basically the same function as was before, just added a second url instead of location. I will rename it as it was before isSamePageWithHash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

data-enhance-nav="false" still triggers fetch calls for same-page anchors
3 participants