Repository navigation
Description
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:
_itemsBeforestays at 0 after the prepend- The DOM keeps rendering items[0..N), which now point to the new prepended items
scrollTopdoes 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);
}- Open
/repropage in a Chromium-based browser. - 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
spacerBeforenon-empty. Confirm with DevTools that the first spacer ([aria-hidden="true"]) hasheight: 0. - 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
Refine Preview 4 aspnetcore notes after live validation
github-actions commentedon Apr 30, 2026
Triage Summary
Area:
area-blazor✅ (already labeled —Virtualize<TItem>component insrc/Components/)Type:
bug— Clearly describes a reproducible regression in theVirtualizecomponent's prepend-anchoring logic introduced by PR #65951.Regression Info
_itemsBefore > 0guard inVirtualize.RefreshDataCoreAsynccauses 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
Notes
&& _itemsBefore > 0from the prepend-detection condition. The existing reference comparison (_previousFirstLoadedItem) still correctly detects prepends when_itemsBefore == 0.Add Preview 4 feature demos
ilonatommy commentedon Apr 30, 2026
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:Test coverage for this case is:
AnchorMode_NearTop_PrependKeepsViewportStable(Theory: anchorModes 0/1/2 × default/items provider)_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)DynamicContent_PrependItemsWhileScrolledToMiddle_VisibleItemsStayInPlace_itemsBefore > 0) prepend stability.AnchorMode_Beginning/End/...PrependAtTop_*AnchorMode_WindowScroll_*_PrependAtTop_*)WindowScroll.Add Preview 4 feature demos