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 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 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.

@dariatiurina dariatiurina merged commit 448e602 into dotnet:main Sep 8, 2025
29 checks passed
@ilonatommy
Copy link
Member

/backport to release/10.0

@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2025

Started backporting to release/10.0: https://github.com/dotnet/aspnetcore/actions/runs/18220339527

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 pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

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