Skip to content
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

Scroll in enhanced navigation behaves same as in client side routing #60296

Merged
merged 26 commits into from
Feb 27, 2025

Conversation

ilonatommy
Copy link
Member

@ilonatommy ilonatommy commented Feb 10, 2025

Description

  1. External navigation scrolls to the top -> see resetScrollAfterNextBatch() in enhanced nav
  2. But it does not scroll if it's self-navigation (or self-navigation with query), to make enhanced nav behave the same as introduced in NavigateTo does not scroll to the top in internal navigation to same url #60190 for client side navigation.
  3. Streaming rendering loads parts of the page with a delay, so the tests required a tool to make sure the full page is loaded: WaitForElementToBeVisible extension was added. Without it, the tests with enableStreaming=true would not be deterministic.
  4. Enhanced backwards/forwards: the history produced by forwards/backwards actions are handled properly by the browser navigation, even when enhanced navigation is switched on.

Fixes #51646

@ilonatommy ilonatommy added area-blazor Includes: Blazor, Razor Components feature-blazor-enhanced-navigation labels Feb 10, 2025
@ilonatommy ilonatommy self-assigned this Feb 10, 2025
@ilonatommy ilonatommy marked this pull request as ready for review February 20, 2025 09:02
@Copilot Copilot bot review requested due to automatic review settings February 20, 2025 09:02
@ilonatommy ilonatommy requested a review from a team as a code owner February 20, 2025 09:02
@ilonatommy ilonatommy requested a review from javiercn February 20, 2025 09:03
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.

Copilot reviewed 5 out of 9 changed files in this pull request and generated 1 comment.

Files not reviewed (4)
  • src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollPositionTests.razor: Evaluated as low risk
  • src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollPositionTestsNoStreaming.razor: Evaluated as low risk
  • src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollingToHash.razor: Evaluated as low risk
  • src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollingToHashNoStreaming.razor: Evaluated as low risk
Comments suppressed due to low confidence (1)

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

  • The function saveScrollPosition should handle cases where the state is null or undefined. Add a check to ensure the state is correctly handled.
const currentState = history.state || {};

@ilonatommy ilonatommy marked this pull request as draft February 20, 2025 09:06
@ilonatommy
Copy link
Member Author

Running the tests locally on repeat revealed an issue with browser's navigation. When we use programmatic navigation, it does not always load the new page (the history state changes but the DOM stays the same, we can see it as failure to render the element of id="test-info-hash", that should replace id="test-info-scroll"). It happens on the 1st run of the tests, with and without streaming and increasing the timeout does not impact it. On the next runs, the test case switches automatically to enhanced nav, even though "sessionStorage.setItem('suppress-enhanced-navigation', 'true')" is set at the beginning of the test.

List of affected test cases:

- Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests.EnhancedNavigationTest.EnhancedNavigationScrollBehavesSameAsBrowserOnBackwardsForwardsAction(enableStreaming: True, useEnhancedNavigation: False, programmaticNavigation: True) 
- Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests.EnhancedNavigationTest.EnhancedNavigationScrollBehavesSameAsBrowserOnBackwardsForwardsAction(enableStreaming: False, useEnhancedNavigation: False, programmaticNavigation: True) 
- Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests.EnhancedNavigationTest.EnhancedNavigationScrollBehavesSameAsBrowserOnNavigation(enableStreaming: True, useEnhancedNavigation: False, programmaticNavigation: True) 
- Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests.EnhancedNavigationTest.EnhancedNavigationScrollBehavesSameAsBrowserOnNavigation(enableStreaming: False, useEnhancedNavigation: False, programmaticNavigation: True) 

error:

Message: 
OpenQA.Selenium.WebDriverTimeoutException : Timed out after 20 seconds
---- OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"css selector","selector":"#test\-info\-hash"}
  (Session info: chrome=133.0.6943.99); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception

    Stack Trace: 
DefaultWait`1.ThrowTimeoutException(String exceptionMessage, Exception lastException)
DefaultWait`1.Until[TResult](Func`2 condition, CancellationToken token)
WebDriverExtensions.WaitForElementToBeVisible(IWebDriver browser, By by, Int32 timeoutInSeconds) line 28
EnhancedNavigationTest.AssertWeAreOnHashPage() line 834
EnhancedNavigationTest.EnhancedNavigationScrollBehavesSameAsBrowserOnBackwardsForwardsAction(Boolean enableStreaming, Boolean useEnhancedNavigation, Boolean programmaticNavigation) line 753
InvokeStub_EnhancedNavigationTest.EnhancedNavigationScrollBehavesSameAsBrowserOnBackwardsForwardsAction(Object, Span`1)
MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

@ilonatommy ilonatommy marked this pull request as ready for review February 20, 2025 15:28
@ilonatommy
Copy link
Member Author

The test cases that fall into this path:

performInternalNavigation(absoluteUri, false, options.replaceHistoryEntry, options.historyEntryState, skipLocationChangingCallback);

are failing. This is not an issue connected with this PR and it happens on main as well.

Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

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

Looks great!

Good job on the testing aspects of this feature, I know this is not a simple area and the coverage looks thorough!

@ilonatommy ilonatommy enabled auto-merge (squash) February 24, 2025 13:58
@ilonatommy ilonatommy merged commit 719c33f into dotnet:main Feb 27, 2025
27 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview3 milestone Feb 27, 2025
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 feature-blazor-enhanced-navigation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve interaction between enhanced nav and scroll position
2 participants