-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Enhanced navigation should not scroll before content renders #64054
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
Open
ilonatommy
wants to merge
4
commits into
dotnet:main
Choose a base branch
from
ilonatommy:fix-64015
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+397
−77
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,8 +11,15 @@ import { getAndRemovePendingRootComponentContainer } from './JSRootComponents'; | |||||
| interface BrowserRendererRegistry { | ||||||
| [browserRendererId: number]: BrowserRenderer; | ||||||
| } | ||||||
|
|
||||||
| export enum ScrollResetSchedule { | ||||||
| None, | ||||||
| AfterBatch, | ||||||
| AfterDocumentUpdate, | ||||||
| } | ||||||
|
|
||||||
| const browserRenderers: BrowserRendererRegistry = {}; | ||||||
| let shouldResetScrollAfterNextBatch = false; | ||||||
| let pendingScrollResetTiming: ScrollResetSchedule = ScrollResetSchedule.None; | ||||||
|
|
||||||
| export function attachRootComponentToLogicalElement(browserRendererId: number, logicalElement: LogicalElement, componentId: number, appendContent: boolean): void { | ||||||
| let browserRenderer = browserRenderers[browserRendererId]; | ||||||
|
|
@@ -88,19 +95,28 @@ export function renderBatch(browserRendererId: number, batch: RenderBatch): void | |||||
| browserRenderer.disposeEventHandler(eventHandlerId); | ||||||
| } | ||||||
|
|
||||||
| resetScrollIfNeeded(); | ||||||
| resetScrollIfNeeded(ScrollResetSchedule.AfterBatch); | ||||||
| } | ||||||
|
|
||||||
| export function resetScrollAfterNextBatch(): void { | ||||||
| shouldResetScrollAfterNextBatch = true; | ||||||
| } | ||||||
| export function scheduleScrollReset(timing: ScrollResetSchedule): void { | ||||||
| if (timing != ScrollResetSchedule.AfterBatch) { | ||||||
|
||||||
| if (timing != ScrollResetSchedule.AfterBatch) { | |
| if (timing !== ScrollResetSchedule.AfterBatch) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
src/Components/test/E2ETest/Infrastructure/ScrollOverrideScope.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using OpenQA.Selenium; | ||
| using Xunit.Sdk; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure; | ||
|
|
||
| internal sealed class ScrollOverrideScope : IDisposable | ||
| { | ||
| private readonly IJavaScriptExecutor _executor; | ||
| private readonly bool _isActive; | ||
|
|
||
| public ScrollOverrideScope(IWebDriver browser, bool isActive) | ||
| { | ||
| _executor = (IJavaScriptExecutor)browser; | ||
| _isActive = isActive; | ||
|
|
||
| if (!_isActive) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _executor.ExecuteScript(@" | ||
| (function() { | ||
| if (window.__enhancedNavScrollOverride) { | ||
| if (window.__clearEnhancedNavScrollLog) { | ||
| window.__clearEnhancedNavScrollLog(); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const original = window.scrollTo.bind(window); | ||
| const log = []; | ||
|
|
||
| function resolvePage() { | ||
| const landing = document.getElementById('test-info-1'); | ||
| if (landing && landing.textContent === 'Scroll tests landing page') { | ||
| return 'landing'; | ||
| } | ||
|
|
||
| const next = document.getElementById('test-info-2'); | ||
| if (next && next.textContent === 'Scroll tests next page') { | ||
| return 'next'; | ||
| } | ||
|
|
||
| return 'other'; | ||
| } | ||
|
|
||
| window.__enhancedNavScrollOverride = true; | ||
| window.__enhancedNavOriginalScrollTo = original; | ||
| window.__enhancedNavScrollLog = log; | ||
| window.__clearEnhancedNavScrollLog = () => { log.length = 0; }; | ||
| window.__drainEnhancedNavScrollLog = () => { | ||
| const copy = log.slice(); | ||
| log.length = 0; | ||
| return copy; | ||
| }; | ||
|
|
||
| window.scrollTo = function(...args) { | ||
| log.push({ | ||
| page: resolvePage(), | ||
| url: location.href, | ||
| time: performance.now(), | ||
| args | ||
| }); | ||
|
|
||
| return original(...args); | ||
| }; | ||
| })(); | ||
| "); | ||
|
|
||
| ClearLog(); | ||
| } | ||
|
|
||
| public void ClearLog() | ||
| { | ||
| if (!_isActive) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _executor.ExecuteScript("if (window.__clearEnhancedNavScrollLog) { window.__clearEnhancedNavScrollLog(); }"); | ||
| } | ||
|
|
||
| public void AssertNoPrematureScroll(string expectedPage, string navigationDescription) | ||
| { | ||
| if (!_isActive) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var entries = DrainLog(); | ||
| if (entries.Length == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var unexpectedEntries = entries | ||
| .Where(entry => !string.Equals(entry.Page, expectedPage, StringComparison.Ordinal)) | ||
| .ToArray(); | ||
|
|
||
| if (unexpectedEntries.Length == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var details = string.Join( | ||
| ", ", | ||
| unexpectedEntries.Select(entry => $"page={entry.Page ?? "null"} url={entry.Url} time={entry.Time:F2}")); | ||
|
|
||
| throw new XunitException($"Detected a scroll reset while the DOM still displayed '{unexpectedEntries[0].Page ?? "unknown"}' during {navigationDescription}. Entries: {details}"); | ||
| } | ||
|
|
||
| private ScrollInvocation[] DrainLog() | ||
| { | ||
| if (!_isActive) | ||
| { | ||
| return Array.Empty<ScrollInvocation>(); | ||
| } | ||
|
|
||
| var result = _executor.ExecuteScript("return window.__drainEnhancedNavScrollLog ? window.__drainEnhancedNavScrollLog() : [];"); | ||
| if (result is not IReadOnlyList<object> entries || entries.Count == 0) | ||
| { | ||
| return Array.Empty<ScrollInvocation>(); | ||
| } | ||
|
|
||
| var resolved = new ScrollInvocation[entries.Count]; | ||
| for (var i = 0; i < entries.Count; i++) | ||
| { | ||
| if (entries[i] is IReadOnlyDictionary<string, object> dict) | ||
| { | ||
| dict.TryGetValue("page", out var pageValue); | ||
| dict.TryGetValue("url", out var urlValue); | ||
| dict.TryGetValue("time", out var timeValue); | ||
|
|
||
| resolved[i] = new ScrollInvocation( | ||
| pageValue as string, | ||
| urlValue as string, | ||
| timeValue is null ? 0D : Convert.ToDouble(timeValue, CultureInfo.InvariantCulture)); | ||
| continue; | ||
| } | ||
|
|
||
| resolved[i] = new ScrollInvocation(null, null, 0D); | ||
| } | ||
|
|
||
| return resolved; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| if (!_isActive) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _executor.ExecuteScript(@" | ||
| (function() { | ||
| if (!window.__enhancedNavScrollOverride) { | ||
| return; | ||
| } | ||
|
|
||
| if (window.__enhancedNavOriginalScrollTo) { | ||
| window.scrollTo = window.__enhancedNavOriginalScrollTo; | ||
| delete window.__enhancedNavOriginalScrollTo; | ||
| } | ||
|
|
||
| delete window.__enhancedNavScrollOverride; | ||
| delete window.__enhancedNavScrollLog; | ||
| delete window.__clearEnhancedNavScrollLog; | ||
| delete window.__drainEnhancedNavScrollLog; | ||
| })(); | ||
| "); | ||
| } | ||
|
|
||
| private readonly record struct ScrollInvocation(string Page, string Url, double Time); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Would it be possible to add comments explaining the values? When someone comes to this a year later they might have harder time understanding the difference between AfterBatch and AfterDocumentUpdate.