Skip to content

Avoid false positive prepend detection that causes backward scroll jumps - #69362

Merged
ilonatommy merged 13 commits into
dotnet:mainfrom
ilonatommy:scroll-jumps-on-data-load
Sep 22, 2026
Merged

ilonatommy merged 13 commits into
dotnet:mainfrom
ilonatommy:scroll-jumps-on-data-load

Conversation

@ilonatommy

@ilonatommy ilonatommy commented Sep 17, 2026 •

Copy link
Copy Markdown
Member
appends.drag.the.view.down.mp4

Reported by Aspire. The viewport (and scroll) jumps down even if user scrolls only up with the mouse wheel. In the video it's most visible after the user reaches the top: the viewport starts escaping downward without further user action. Setup: ItemProvider with ongoing prepends.

Description

The issue is a false positive in Virtualize prepend detection. The check is activated when live updates increase the list count (countDelta > 0). The prepend detection keeps the previously rendered first item and compares it with the first item returned for the latest provider window. While the user scrolls, the requested provider window changes, so those items naturraly differ even though data was appended rather than prepended.

To avoid this false positive, Virtualize now stores the first rendered item's global index and compares the item at that same index in the new provider result. This distinguishes a real prepend from an ordinary provider-window change caused by scrolling.

Continuous tail-validation also showed that advancing an End-anchored window required a second provider request and exposed a partially updated window state. End-tail requests now include a bounded look-ahead, reuse prefetched rows for normal appends and publish the shifted window only after its rows are available. That improved perf is reflected in updated test EndAnchoredAppend_IssuesExactlyTwoProviderCalls_AdvancingToTail -> EndAnchoredAppend_ReusesPrefetchedTailWithOneProviderCall.

For Start anchoring, JavaScript now tracks if the viewport is still following the top with IsFollowingTopAsync (same mechanism as we already had for the tail of the list with IsFollowingBottomAsync). New rows remain visible while following the top. After the user scrolls away, Virtualize loads the rows for the shifted window before changing the rendered range and prepares anchor restoration before rendering. For tables and other manual-anchoring cases, RestoreAnchorAsync can now defer restoration until the matching spacer change. This lets JavaScript restore the saved row before the shifted rendered frame becomes visible.

For test reviewers:

  • Table_EndAnchor_AsyncProvider_AppendDuringRapidUserScroll_DoesNotReverseScrollDirection is regression test for the reported issue.
  • Table_EndAnchor_AsyncProvider_ContinuousAppend_MaterializesNewTailBeforeUpdatesStop verifies that appended items are materialized while updates continue, visible indexes remain monotonic, intermediate indexes are not skipped, and the viewport remains pinned to the tail.
  • Table_StartAnchor_AsyncProvider_ContinuousPrepend_MaterializesNewHeadWithoutIndexReversal verifies that Start-following remains pinned and displays each prepended item (symmetric to the end-append). It failed before the top-following change because head updates either did not materialize or moved visible indexes backward.
  • Table_StartAnchor_AsyncProvider_ContinuousPrependAfterLeavingHead_KeepsViewportStable covers QuickGrid Start anchoring does not preserve viewport after prepending rows in Server-rendered modes #69380 by requiring the same visible item to remain at the same viewport-relative offset during prepends.
  • EndAnchoredAppend_ReusesPrefetchedTailWithOneProviderCall verifies that a normal End-anchored append reuses the bounded prefetch instead of issuing a second provider request. Covers the behavior reported in QuickGrid issues duplicate ItemsProvider requests during prepend and append refreshes #69381.
  • Virtualize_ItemsProvider_PrependedSinglePassEnumerable_IsNotEnumeratedMoreThanOnce verifies that prepend detection and prefetch reuse do not repeatedly enumerate a provider-supplied sequence.

Fixes #69381
Fixes #69380

@ilonatommy ilonatommy added this to the 11.0-rc2 milestone Sep 17, 2026
@ilonatommy ilonatommy self-assigned this Sep 17, 2026
@ilonatommy ilonatommy added area-blazor Includes: Blazor, Razor Components feature-blazor-virtualization This issue is related to the Blazor Virtualize component labels Sep 17, 2026
Prefetch adjusted provider windows and defer anchor restoration until the matching spacer mutation, preventing visible viewport shifts during continuous prepends.
reuse the list for comparison, slicing, and rendering.

Add coverage for single-pass provider results and tighten the rapid
scroll regression to reject any backward scroll movement.
@ilonatommy
ilonatommy marked this pull request as ready for review September 21, 2026 13:06
@ilonatommy
ilonatommy requested a review from a team as a code owner September 21, 2026 13:06
@ilonatommy
ilonatommy requested review from dariatiurina and a lite review from Copilot September 21, 2026 13:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

Five unresolved review comments remain, including four moderate issues affecting anchoring and test reliability.

Review effort: Lite
Findings: None

What changed in this PR

Fixes virtualization prepend detection, scroll anchoring, and duplicate provider requests for async lists.

Changes:

  • Compares items at stable global indexes.
  • Adds bounded tail prefetching and deferred anchor restoration.
  • Adds top-follow tracking and regression coverage.
File Summary
src/​Components/​Web/​test/​Virtualization/​VirtualizeTest.cs Tests single-pass provider enumeration.
src/​Components/​Web/​src/​Virtualization/​VirtualizeJsInterop.cs Adds top-following and deferred-restore interop.
src/​Components/​Web/​src/​Virtualization/​Virtualize.cs Updates detection, prefetching, and anchoring. Moderate findings: deferred restoration may be canceled prematurely (1 vote); update comparer documentation (nit, 1 vote).
src/​Components/​Web.JS/​src/​Virtualize.ts Tracks top-following and mutation-based restoration. Moderate finding: user input may be bypassed during top convergence (1 vote).
src/​Components/​test/​testassets/​BasicTestApp/​VirtualizationTableScrollWithAppend.razor Adds the async table scenario. Moderate finding: fixed timing makes tests scheduling-sensitive (1 vote).
src/​Components/​test/​testassets/​BasicTestApp/​Index.razor Registers the test scenario.
src/​Components/​test/​E2ETest/​Tests/​VirtualizationTest.cs Adds scrolling and anchoring tests. Moderate finding: the sentinel can accept an empty frame (1 vote).
src/​Components/​test/​E2ETest/​ServerRenderingTests/​VirtualizationRenderModesTest.cs Verifies tail prefetch reuse.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Canceled refreshes can publish stale state, and several timing-based E2E probes are nondeterministic.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 2 Medium severity · 1 Low severity

Open (3)

… when scrolling moves past the previously rendered first item.

Add prepend and append unit coverage to verify that shifted windows preserve the requested item without reintroducing false prepend detection.

@dariatiurina dariatiurina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great! My question is about order of operations and cancellation. I remember we spoke of something similar before, so disregard if it's a duplicate

@ilonatommy
ilonatommy enabled auto-merge (squash) September 21, 2026 16:23
@ilonatommy
ilonatommy disabled auto-merge September 21, 2026 16:37
@ilonatommy

Copy link
Copy Markdown
Member Author

The failures look related. I have to investigate before merging.

@ilonatommy
ilonatommy enabled auto-merge (squash) September 21, 2026 20:13
@ilonatommy
ilonatommy merged commit 8e5048e into dotnet:main Sep 22, 2026
31 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 23, 2026
@ilonatommy

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

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-virtualization This issue is related to the Blazor Virtualize component

Projects

None yet

5 participants