Skip to content

fix(virtual-core): apply initialMeasurementsCache only once - #1391

Merged
piecyk merged 1 commit into
TanStack:mainfrom
h00ns:fix/initial-measurements-cache-reseed
Nov 22, 2026
Merged

piecyk merged 1 commit into
TanStack:mainfrom
h00ns:fix/initial-measurements-cache-reseed

Conversation

@h00ns

@h00ns h00ns commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Changes

initialMeasurementsCache is documented as "consumed only once, on the first getMeasurements() call after mount", but it is applied again every time the list becomes empty. When count goes to 0 and back (a search with no matches, a loading state during a refetch), the snapshot's sizes overwrite the sizes rows were measured at since mount.

const v = new Virtualizer({
  count: 20,
  estimateSize: () => 50,
  initialMeasurementsCache: [{ index: 0, key: 0, start: 0, size: 80, end: 80, lane: 0 }],
  // ...
})
v['getMeasurements']()          // row 0 = 80 (from the snapshot)
v.resizeItem(0, 120)            // row 0 measured again at 120

v.setOptions({ ...v.options, count: 0 }); v['getMeasurements']()
v.setOptions({ ...v.options, count: 20 })
v['getMeasurements']()[0].size  // expected 120, actual 80

Off-screen rows keep the stale sizes, so getTotalSize(), the scrollbar and scrollToIndex targets are off until those rows render again, and visible rows jump when the ResizeObserver corrects them.

getMeasurements() seeds from the snapshot whenever measurementsCache.length === 0, which is also true after any build with count === 0. A private flag now makes the seeding happen once. enabled: false already clears the measured sizes and also resets the flag, so toggling enabled keeps today's behavior.

Added a test that empties and refills a list restored from a snapshot; it fails without the fix.

✅ 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.

Summary by CodeRabbit

  • Bug Fixes
    • Preserved updated item sizes when a list becomes empty and is later repopulated, preventing older cached measurements from replacing newer ones.
    • Reset measurement caching when virtualization is disabled, so the initial measurements can be applied again when it is re-enabled.

getMeasurements re-seeded itemSizeCache from initialMeasurementsCache
whenever measurementsCache was empty, which also happens after count
drops to 0. Refilling the list then put the snapshot's sizes back over
the sizes measured since mount. Seed it once; enabled: false still
resets it along with the measured sizes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@changeset-bot

changeset-bot Bot commented Nov 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8d36be8

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 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The virtualizer now applies initialMeasurementsCache only once until it is disabled. A regression test checks that an item’s measured size remains after the list count changes to zero and then returns.

Changes: Initial measurement cache

Layer / File(s) Summary
Snapshot application and regression test
packages/virtual-core/src/index.ts, packages/virtual-core/tests/index.test.ts, .changeset/initial-measurements-cache-once.md
getMeasurements applies the initial snapshot only if it has not already been applied, the cache is empty, and lane settling is inactive. Disabling the virtualizer resets the applied flag. The test verifies that an item resized from 80 to 120 retains size 120 after the list empties and refills. A patch changeset records the change.

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

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: shunn2, piecyk

Merge Risk: Minimal · up to 8d36b

This fix keeps measured row sizes when a list empties and refills after being restored from a snapshot. It also keeps the existing reset behavior when the virtualizer is disabled. No actionable merge risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the actual change: getMeasurements() now applies initialMeasurementsCache only once, which matches the raw summary (a private flag gates the seeding) and the PR objectives. It uses the…
Description check Passed The description follows the repository template. It has a Changes section that explains the problem, the cause, the fix, and the reproduction. It also has a checked Checklist and a checked Release Imp…
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 actual change: getMeasurements() now applies initialMeasurementsCache only once, which matches the raw summary (a private flag gates the seeding) and the PR objectives. It uses the conventional fix(scope) form and is specific enough for a teammate scanning history.

Full details: Description check

Explanation

The description follows the repository template. It has a Changes section that explains the problem, the cause, the fix, and the reproduction. It also has a checked Checklist and a checked Release Impact section, with a changeset recorded in the diff. The Changes section covers the motivation and the intended behavior of enabled: false. The description does not include the template's second Release Impact option, but that option is for non-release changes and does not apply here.

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/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

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: 67917002-7562-4352-88a7-4736c2d2b1b9
Commits

Reviewing files that changed from the base of the PR and between b883a1c and 8d36be8.

Files selected for processing: 3

  • .changeset/initial-measurements-cache-once.md
  • packages/virtual-core/src/index.ts
  • packages/virtual-core/tests/index.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.

@piecyk piecyk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @h00ns, nice catch!

@nx-cloud

nx-cloud Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 8d36be8

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 4m 33s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 20s 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-22 13:04:44 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 22, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-virtual

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

@tanstack/lit-virtual

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

@tanstack/marko-virtual

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

@tanstack/react-virtual

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

@tanstack/solid-virtual

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

@tanstack/svelte-virtual

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

@tanstack/virtual-core

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

@tanstack/vue-virtual

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

commit: 8d36be8

@piecyk
piecyk merged commit df00c7f into TanStack:main Nov 22, 2026
10 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 22, 2026
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