Skip to content
Source
Sign inSign up
TanStack/virtual

fix(virtual-core): reject 0-size resizeItem when cached measurement exists #1226

Closedhiro-nikaitou(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/1226/headopened

Description

When an element is temporarily hidden (e.g., display:none on the container), the ResizeObserver fires with size 0. This overwrites the valid cached measurement in resizeItem, causing calculateRangeImpl to include all items in the visible range (since all items now have end === 0), which results in a browser freeze or Maximum update depth exceeded error.

Fix

Guard resizeItem against accepting a 0-size measurement when the item already has a previously cached size. First-time measurements of genuinely 0-height items remain accepted (no prior cache entry).

Tests

  • Verify 0-size is rejected when previous non-zero measurement exists in cache
  • Verify first-time 0-size measurement is accepted (no prior cache)
  • Verify 0-size → non-zero → 0-size cycle: non-zero measured after first 0 accepted, then 0 rejected after non-zero cached

Fixes #1188

Summary by CodeRabbit

  • Bug Fixes
    • Prevented temporary zero-size measurements from overwriting previously cached item sizes.
    • Reduced unnecessary re-renders and avoided infinite render loops when virtualized elements are temporarily hidden.
    • Improved range calculations when items briefly report zero dimensions.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 0bf9fa20-beb1-4841-a43e-4b6fc350334a

      📥 Commits

      Reviewing files that changed from the base of the PR and between d49cc526fe248be7b5ad97ec6ac814db8271b0d0 and 0846ad95b14a51eaa53faf2f0555738103883fe3.

      📒 Files selected for processing (3)
      • .changeset/zero-size-reject-measurement.md
      • packages/virtual-core/src/index.ts
      • packages/virtual-core/tests/index.test.ts

      📝 Walkthrough

      Walkthrough

      Virtualizer.resizeItem now ignores zero-size measurements when an item already has a cached size. Tests cover cache update behavior, and a Changesets entry records a patch release for @tanstack/virtual-core.

      Changes

      Resize measurement handling

      Layer / File(s)Summary
      Guard zero-size cache updates
      packages/virtual-core/src/index.ts, packages/virtual-core/tests/index.test.ts, .changeset/zero-size-reject-measurement.md
      resizeItem preserves existing cached sizes for zero measurements, while tests verify initial zero measurements and later non-zero updates remain valid. A patch Changesets entry documents the fix.

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Possibly related PRs

      • TanStack/virtual#1186: Also addresses transient zero-size measurements affecting cached virtual item sizes.

      Suggested reviewers: piecyk

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title accurately describes the main fix: rejecting zero-size resizeItem updates when a cached measurement exists.
      Description check✅ PassedThe description covers the bug, fix, and tests, though it omits the template's checklist and release-impact sections.
      Linked Issues check✅ PassedThe change matches #1188 by preventing zero-size measurements from overwriting cached sizes and causing crashes or slow loads.
      Out of Scope Changes check✅ PassedThe changes are in scope: code fix, tests, and a changeset note all support the zero-size measurement bug fix.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Warning

      There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

      🔧 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/index.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/index.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
      Comment `@coderabbitai help` to get the list of available commands.
    2. piecyk reviewed

      We already have an opt-in solution for this use case with useCachedMeasurements:

      https://tanstack.com/virtual/latest/docs/api/virtualizer#usecachedmeasurements

      It was added specifically for lists that are temporarily hidden with display: none, where ResizeObserver reports 0 and would otherwise overwrite the cached measurements.

      This PR moves that behavior into resizeItem globally. That means an item with a cached measurement could never legitimately resize to 0, and it would also affect custom measureElement implementations and direct resizeItem calls.

    3. piecyk closed this

Sign in to join this conversation.