Skip to content

About

A repro of what I think is a bug in tanstack/virtual.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

TanStack Virtual — ResizeObserver Element Hijacking Bug

Live Demo

Bug Summary

When an item is removed from a virtualized list, the removed element's ResizeObserver callback can fire with a stale data-index that collides with a surviving element's new index. _measureElement misidentifies the dead element as the live one, swaps the elementsCache entry (unobserving the live element, observing the dead one), and the live element is permanently orphaned from observation. Any subsequent size changes to that element are invisible to the virtualizer.

Affected version: @tanstack/[email protected] (@latest as of February 2025) and likely all earlier versions with the same _measureElement logic

Repository Structure

  • broken/ — Reproduces the bug using unpatched @tanstack/[email protected] (@latest)
  • fixed/ — Same app with a patch-package fix applied to @tanstack/virtual-core

Each subdirectory is an independent Vite + React + TypeScript app.

Quick Start

# Run the broken version
cd broken
npm install
npm run dev

# Run the fixed version (patch-package applies automatically)
cd fixed
npm install
npm run dev

Reproduction Steps

  1. Expand A — click "Expand A". A's row grows to ~160px. The virtualizer updates totalSize. Works fine.
  2. Collapse A — click "Collapse A". A shrinks back. Works fine.
  3. Delete A — click "Delete A". A is removed. B shifts from index 1 to index 0.
  4. Expand B — click "Expand B". B's row should grow, but totalSize stays at ~35px. BUG.

The totalSize indicator at the top turns red when the virtualizer's size doesn't match the expanded content.

Click "Reset" to restore the initial state and try again.

Root Cause

In Virtualizer._measureElement (@tanstack/virtual-core/dist/esm/index.js):

this._measureElement = (node, entry) => {
  const index = this.indexFromElement(node);       // reads data-index from DOM
  const item = this.measurementsCache[index];
  if (!item) return;

  const key = item.key;
  const prevNode = this.elementsCache.get(key);
  if (prevNode !== node) {
    if (prevNode) this.observer.unobserve(prevNode);  // ← unobserves LIVE element
    this.observer.observe(node);                       // ← observes DEAD element
    this.elementsCache.set(key, node);                 // ← maps live key → dead element
  }
  if (node.isConnected) {                              // ← too late, damage done
    this.resizeItem(index, this.options.measureElement(node, entry, this));
  }
};

When item A (index 0) is deleted:

  • B shifts from index 1 to index 0
  • A's dead div fires a final ResizeObserver callback (height → 0)
  • _measureElement reads data-index=0 from A's dead div
  • measurementsCache[0] is now B's entry
  • The method thinks A's dead div is B's div → swaps the observer
  • B's real div is unobserved forever

Proposed Fix

Check node.isConnected before the elementsCache swap:

this._measureElement = (node, entry) => {
  if (!node.isConnected) {
    this.observer.unobserve(node);
    return;
  }
  const index = this.indexFromElement(node);
  const item = this.measurementsCache[index];
  if (!item) return;

  const key = item.key;
  const prevNode = this.elementsCache.get(key);
  if (prevNode !== node) {
    if (prevNode) this.observer.unobserve(prevNode);
    this.observer.observe(node);
    this.elementsCache.set(key, node);
  }
  this.resizeItem(index, this.options.measureElement(node, entry, this));
};

The patch is applied in fixed/patches/.

About

A repro of what I think is a bug in tanstack/virtual.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages