Skip to content

fix(virtual-core): drop rows that a render detaches after their ref callback - #1412

Merged
piecyk merged 2 commits into
TanStack:mainfrom
jrandolf:fix/virtual-core-detached-row-observation
Nov 30, 2026
Merged

piecyk merged 2 commits into
TanStack:mainfrom
jrandolf:fix/virtual-core-detached-row-observation

Conversation

@jrandolf

@jrandolf jrandolf commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Changes

A virtualized list logs "ResizeObserver loop completed with undelivered notifications." when it shrinks and a render in the ResizeObserver delivery unmounts rows.
A framework clears the ref of a row before it detaches the row, so measureElement(null) still observes the last row to leave.
measureElement(null) also queues one microtask that drops the rows that have left the page by then.

Related: #1345, fixed by #1348. #1348 drops detached rows after a synchronous render in the row observer only.
That drop runs before a render in a microtask, and the scroll element's observer has no drop.

Reproduction, with Vue 3.5.22 and @tanstack/vue-virtual 3.13.55 (@tanstack/virtual-core 3.19.14):

  1. Open the page below.
  2. Run document.documentElement.style.setProperty('--viewport', '300px') in the console.
  3. The page logs the message above. With this change, it logs nothing.
<!doctype html>
<style>
  :root {
    --viewport: 600px;
  }
  #parent {
    height: var(--viewport);
    overflow: auto;
  }
</style>
<div id="app"></div>
<script type="module">
  import { createApp, h, ref } from 'https://esm.sh/vue@3.5.22'
  import { useVirtualizer } from 'https://esm.sh/@tanstack/vue-virtual@3.13.55'

  addEventListener('error', (event) => console.log(event.message))

  createApp({
    setup() {
      const parent = ref(null)
      const virtualizer = useVirtualizer({
        count: 200,
        getScrollElement: () => parent.value,
        estimateSize: () => 32,
      })
      return () =>
        h(
          'div',
          { id: 'parent', ref: parent },
          h(
            'div',
            { style: { height: `${virtualizer.value.getTotalSize()}px`, position: 'relative' } },
            virtualizer.value.getVirtualItems().map((row) =>
              h(
                'div',
                {
                  key: row.key,
                  ref: virtualizer.value.measureElement,
                  'data-index': row.index,
                  style: {
                    position: 'absolute',
                    top: 0,
                    width: '100%',
                    height: '32px',
                    transform: `translateY(${row.start}px)`,
                  },
                },
                `Row ${row.index}`,
              ),
            ),
          ),
        )
    },
  }).mount('#app')
</script>

In Chromium 159, Firefox 159 and WebKit 27.2, this page logs the error in 20 of 20 runs on main (f78d04a) and in 0 of 20 runs with this change.
Three unit tests in measurement.test.ts and the new case in resize-delivery.spec.ts fail without the change.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a resize-observer loop error that could occur when a virtualized list shrinks during rendering in frameworks with microtask-based updates.
    • Detached rows are now cleaned up reliably after resize events, helping scrolling and row updates settle without lingering observations or page errors.
    • Added coverage for list shrinking during scroll, row resizing, and batched updates to verify resize delivery remains stable.

…allback

A framework clears a row's ref before it detaches the row's element, so
`measureElement(null)` found the last unmounted row still connected and kept
observing it. When a render runs in a microtask of a ResizeObserver delivery
(Vue, Lit, Svelte), the row is then detached and observed at the end of the
delivery, and the browser reports "ResizeObserver loop completed with
undelivered notifications". The scroll element's observer and the row
observer of a framework that renders in a microtask both hit this.

`measureElement(null)` now also queues one microtask that drops the rows that
are detached by then. The microtask runs after the render and before the
browser gathers the next observations.
@changeset-bot

changeset-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f6e1ee0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@tanstack/virtual-core Patch
@tanstack/angular-virtual Patch
@tanstack/lit-virtual Patch
@tanstack/marko-virtual Patch
@tanstack/react-virtual Patch
@tanstack/solid-virtual Patch
@tanstack/svelte-virtual Patch
@tanstack/vue-virtual Patch
@tanstack/virtual-benchmarks Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The virtualizer now cleans up disconnected elements immediately and repeats the cleanup in one queued microtask. Unit and end-to-end tests cover detached rows during resize delivery.

Changes: Detached-row cleanup

Layer / File(s) Summary
Deferred cleanup and unit coverage
packages/virtual-core/src/index.ts, packages/virtual-core/tests/measurement.test.ts, .changeset/quiet-detached-rows.md
measureElement(null) removes disconnected cached elements immediately and queues one deduplicated microtask cleanup. Unit tests cover resize delivery, row detachment, and cleanup batching. The changeset records a patch release.
End-to-end resize coverage
packages/react-virtual/e2e/app/resize-delivery/index.html, packages/react-virtual/e2e/app/resize-delivery/main.tsx, packages/react-virtual/e2e/app/test/resize-delivery.spec.ts
The resize-delivery app uses a CSS property for viewport height. The end-to-end test checks row unmounting and verifies that no ResizeObserver loop errors or page errors occur after frames settle.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant ResizeObserver
  participant Framework
  participant Virtualizer
  ResizeObserver-->>Framework: Resize delivery triggers rendering
  Framework->>Virtualizer: measureElement(null) ref callback
  Virtualizer->>Virtualizer: dropDisconnected immediately
  Virtualizer->>Virtualizer: Queue one cleanup microtask
  Framework->>Framework: Detach rows after ref callbacks
  Virtualizer->>Virtualizer: Repeat dropDisconnected in microtask
Loading

Possibly related PRs

  • TanStack/virtual#1348: It removes detached rows from the ResizeObserver after synchronous resize renders. This change covers detached rows after microtask renders.

Suggested reviewers: piecyk, minwookshin

Merge Risk: Minimal · up to f6e1e

This change prevents ResizeObserver loop errors when a framework detaches virtualized rows during resize delivery. No concrete merge-blocking risk was found. The change is small and easy to revert, and it has unit and browser test coverage.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: virtual-core now drops rows that a render detaches after their ref callback. It uses the conventional fix(scope) prefix and matches the changeset, the measureElement(n…
Description check Passed The description follows the template. It has a Changes section that explains the problem, the cause (ref cleared before detach, so measureElement(null) observes a row that later leaves), the fix (one …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: virtual-core now drops rows that a render detaches after their ref callback. It uses the conventional fix(scope) prefix and matches the changeset, the measureElement(null) microtask cleanup, and the added tests. It is specific and concise.

Full details: Description check

Explanation

The description follows the template. It has a Changes section that explains the problem, the cause (ref cleared before detach, so measureElement(null) observes a row that later leaves), the fix (one microtask that drops departed rows), and the relation to #1348. It includes a reproduction with versions, browser results, and test coverage. The Checklist and Release Impact sections are filled in, with the changeset box checked. The template's Changes heading is retained. The description is complete and specific.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/virtual-core/tests/measurement.test.ts

Parsing error: "parserOptions.project" has been provided for @typescript-eslint/parser.
The file was not found in any of the provided project(s): packages/virtual-core/tests/measurement.test.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f92533dd-6cef-49e4-bfe0-02a9ac94f106
Commits

Reviewing files that changed from the base of the PR and between f78d04a and f6e1ee0.

Files selected for processing: 6

  • .changeset/quiet-detached-rows.md
  • packages/react-virtual/e2e/app/resize-delivery/index.html
  • packages/react-virtual/e2e/app/resize-delivery/main.tsx
  • packages/react-virtual/e2e/app/test/resize-delivery.spec.ts
  • packages/virtual-core/src/index.ts
  • packages/virtual-core/tests/measurement.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@nx-cloud

nx-cloud Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit f6e1ee0

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 3m 4s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 17s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-30 06:51:17 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 30, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-virtual

npm i https://pkg.pr.new/@tanstack/angular-virtual@1412

@tanstack/lit-virtual

npm i https://pkg.pr.new/@tanstack/lit-virtual@1412

@tanstack/marko-virtual

npm i https://pkg.pr.new/@tanstack/marko-virtual@1412

@tanstack/react-virtual

npm i https://pkg.pr.new/@tanstack/react-virtual@1412

@tanstack/solid-virtual

npm i https://pkg.pr.new/@tanstack/solid-virtual@1412

@tanstack/svelte-virtual

npm i https://pkg.pr.new/@tanstack/svelte-virtual@1412

@tanstack/virtual-core

npm i https://pkg.pr.new/@tanstack/virtual-core@1412

@tanstack/vue-virtual

npm i https://pkg.pr.new/@tanstack/vue-virtual@1412

commit: f6e1ee0

@piecyk
piecyk merged commit 5a6a279 into TanStack:main Nov 30, 2026
10 checks passed
@piecyk

piecyk commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Merged, thanks @jrandolf! Really nice work on this one, the repro page and the e2e case for the scroll element observer made it easy to check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants