Skip to content

Virtualize: viewport stability on prepend regresses when items are inserted while the user is still within the initial render window (_itemsBefore == 0) #66541

Closed

Description

@danroth27

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

PR #65951 ("Virtualize keeps the viewport stable when content above it changes") added scroll-anchoring for two scenarios — height changes above the viewport, and items prepended to the collection. Height-change anchoring works correctly. Prepend anchoring fails in a common edge case: when the user has scrolled within the page but the rendering window still starts at index 0 (i.e. _itemsBefore == 0, spacerBefore has zero height), prepending items silently teleports the user to the new items instead of anchoring on what they were viewing.

The prepend-detection block in Virtualize<TItem>.RefreshDataCoreAsync is guarded by _itemsBefore > 0:

if (num > 0 && _itemsBefore > 0 && _previousFirstLoadedItem != null && _itemsProvider == new ItemsProviderDelegate<TItem>(DefaultItemsProvider))
{
    // ...adjust _itemsBefore by the number of newly prepended items
}

This guard skips the case where _itemsBefore == 0, which is real when the rendered window happens to span index 0 and the user has only scrolled within that window. In that situation:

  • _itemsBefore stays at 0 after the prepend
  • The DOM keeps rendering items[0..N), which now point to the new prepended items
  • scrollTop does not adjust
  • The previously-visible item leaves the DOM
  • The user sees the prepended items they didn't request to see

Expected behavior

After prepend, the previously-visible item should remain in the same screen position regardless of _itemsBefore's value. The release notes for .NET 11 Preview 4 describe this as the documented behavior:

Visible items stay in place [...] when items are prepended to the collection.

Actual behavior

Two scenarios:

_itemsBefore before prepend Behavior
> 0 ✅ Works as documented — _itemsBefore is bumped by the inserted count, spacerBefore grows, browser scroll-anchors visible items
== 0 ❌ Detection block skipped, visible items replaced by prepended items, scroll position unchanged

Steps to reproduce

Minimal Blazor SSR (or interactive) sample:

@page "/repro"
@rendermode InteractiveServer

<div style="height: 400px; overflow-y: auto; border: 1px solid;">
    <Virtualize Items="items" Context="item">
        <div style="padding: 1em; border-bottom: 1px solid #ddd;">
            <strong>Item #@item.Id</strong>
        </div>
    </Virtualize>
</div>

<button @onclick="Prepend">Prepend 100 items</button>

@code {
    private List<Item> items = new();
    private int nextNew = -1;

    protected override void OnInitialized()
    {
        for (int i = 0; i < 1000; i++) items.Add(new Item(i));
    }

    private void Prepend()
    {
        var newItems = Enumerable.Range(0, 100).Select(_ => new Item(nextNew--)).ToList();
        items.InsertRange(0, newItems);
    }

    record Item(int Id);
}
  1. Open /repro page in a Chromium-based browser.
  2. Scroll the inner div down by ~3000px so item 20 or so is at the top of the viewport. Do not scroll far enough to make spacerBefore non-empty. Confirm with DevTools that the first spacer ([aria-hidden="true"]) has height: 0.
  3. Click "Prepend 100 items".

Expected: Item 20 (or whichever item was at the top) stays at the top of the viewport.
Actual: The viewport now shows Item -26 or similar — the user is teleported into the prepended items, and the originally-visible item is no longer in the DOM.

If step 2 is changed to scroll past the initial render window (e.g. scrollTop ≈ 100,000), the prepend works correctly. The bug is specifically in the edge case where _itemsBefore is still 0.

Diagnostic data

Captured via Playwright on 11.0.100-preview.4.26224.122:

Phase scrollTop spacerBefore height First visible _itemsBefore (inferred)
After scroll to 3000 3000 0 Item 24 0
After Prepend 100 1855 0 Item -26 0 (unchanged)
Phase scrollTop spacerBefore height First visible _itemsBefore (inferred)
After scroll to 100000 101000 97737 Item 702 ≈688
After Prepend 100 115247 111984 Item 702 ≈788 (correctly bumped by 100)

Suggested fix

Drop the _itemsBefore > 0 guard. The detection should also fire at _itemsBefore == 0:

if (num > 0 && _previousFirstLoadedItem != null && _itemsProvider == new ItemsProviderDelegate<TItem>(DefaultItemsProvider))

When _itemsBefore == 0 before prepend, Items.ElementAtOrDefault(0) is the new prepended item, which differs from _previousFirstLoadedItem (the original index-0 item) — so the existing reference comparison still works correctly. After bumping, _itemsBefore becomes num, the spacerBefore grows in DOM, and the browser's native scroll anchoring (or the manual fallback path) compensates scrollTop automatically.

Versions

  • ASP.NET Core: 11.0.0-preview.4.26224.122
  • SDK: 11.0.100-preview.4.26224.122
  • OS: Windows
  • Browser: Chromium (Playwright)

Activity

  1. added
    area-blazor
    on Apr 30, 2026
  2. added
    bug
    on Apr 30, 2026
  3. github-actions commented on Apr 30, 2026

    @github-actions
    Contributor

    Triage Summary

    Area: area-blazor ✅ (already labeled — Virtualize<TItem> component in src/Components/)
    Type: bug — Clearly describes a reproducible regression in the Virtualize component's prepend-anchoring logic introduced by PR #65951.

    Regression Info

    • Previously working version: N/A (the feature itself was introduced in .NET 11 Preview 4 via PR [Virtualization] Visible content does not shift when in-DOM items above the viewport change height #65951)
    • Broken since: ASP.NET Core 11.0.0-preview.4.26224.122
    • The scroll-anchoring on prepend was intended to keep previously-visible items in place, but the _itemsBefore > 0 guard in Virtualize.RefreshDataCoreAsync causes the feature to silently skip the case where the rendered window still starts at index 0, teleporting the user to the newly prepended items instead.

    Potential Duplicates

    • None found — searches for "Virtualize prepend scroll anchor" and "Virtualize viewport stability prepend" returned only this issue.

    Notes

    • The reporter (a MEMBER) provides a clear root-cause analysis and a minimal fix: removing && _itemsBefore > 0 from the prepend-detection condition. The existing reference comparison (_previousFirstLoadedItem) still correctly detects prepends when _itemsBefore == 0.

    Generated by Issue Triage Agent for dotnet/aspnetcore for issue #66541 · ● 157.7K · ◷

  4. added a commit that references this issue on Apr 30, 2026
  5. ilonatommy commented on Apr 30, 2026

    @ilonatommy
    Member

    Thank you. The concern is valid but we merged #66521 that removed this code from p4 (and main).

    File: src/Components/Web/src/Virtualization/Virtualize.cs:

    - // Detect if items were prepended above the current viewport position.
    - if (countDelta > 0 && _itemsBefore > 0 && _previousFirstLoadedItem != null
    -     && _itemsProvider == DefaultItemsProvider)
    + if (itemsAdded && isDefaultProvider && _previousFirstLoadedItem != null)

    Test coverage for this case is:

    Test Scenario
    AnchorMode_NearTop_PrependKeepsViewportStable (Theory: anchorModes 0/1/2 × default/items provider) Scrolls ~200px (still inside initial render window → _itemsBefore == 0), then prepends 10 items, asserts visible item stays in place — this is exactly the issue's scenario.
    AnchorMode_None_PrependAtTop_ViewportStaysStable (variableHeight × useItemsProvider) At absolute top, prepend, viewport stays stable.
    DynamicContent_PrependItemsWhileScrolledToMiddle_VisibleItemsStayInPlace Deeper scroll (_itemsBefore > 0) prepend stability.
    AnchorMode_Beginning/End/...PrependAtTop_* Other AnchorMode prepend variants.
    Window-scroll mirrors (AnchorMode_WindowScroll_*_PrependAtTop_*) Same scenarios but with WindowScroll.
  6. added a commit that references this issue on Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-blazorbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions