In my earlier post on per-item ResizeObserver feedback loops, the fix was to measure once at row creation and stop observing. That stops the loop, but it opens a quieter problem: every measurement you cache can go stale, and stale measurements fail far from where they were taken.

By the end of this post you'll know the three failure modes of cached row heights, how content-visibility changes the picture, and a reconciliation pass you can drop into most virtualizers.

Where stale heights actually show up

A cached height is only wrong if the row's content changes after you measure it. In practice that happens constantly:

  • Fonts finish loading and line heights shift by a pixel or two.
  • Images without a fixed aspect ratio resolve and reflow the row.
  • A diff viewer collapses whitespace or expands a wrapped line on toggle.

The nasty part is that the symptom appears rows away from the cause. You cache row 412's height at 80px, the font loads, its true height becomes 92px, and the scrollbar jumps the moment the user scrolls past row 400. No error, no warning, just a scroll position that lies.

Why content-visibility is not a free fix

content-visibility: auto tells the browser to skip rendering offscreen subtrees while keeping a size estimate (contain-intrinsic-size) for them. It looks like it solves measurement entirely:

.row { content-visibility: auto; contain-intrinsic-size: auto 80px; } 

The auto keyword in contain-intrinsic-size is the important part. Once a row has actually rendered once, the browser remembers its real size and uses that estimate when it leaves the viewport. You get a self-correcting cache for free.

But it only corrects rows that have been rendered. Rows below the fold still use your 80px guess, so total scroll height is wrong until the user has visited everything. If your virtualizer reads offsetTop of an unrendered row to decide what to paint, you are back to lying measurements — the browser just owns the cache now, and you cannot invalidate it.

The rule that has held up for me: use content-visibility when you render everything and only want to skip paint work. Use an explicit virtualizer when you need accurate total height before rows are seen.

Reconcile measurements on demand

The pattern that survived production for me is a two-tier cache. Trust the cached height for layout, but reconcile whenever a row enters the viewport for real:

const cache = new Map(); // id -> height function measure(id, el) { const h = el.offsetHeight; const old = cache.get(id); if (old !== h) { cache.set(id, h); if (old !== undefined) scheduleTotalHeightUpdate(); } return h; } 

Notice the check is old !== undefined, not just a change. The first measurement of a row seeds the cache; only subsequent differences mean content shifted, and only those need a scrollbar recompute. Skipping the recompute on first measure is what keeps this from becoming the feedback loop from the previous post.

Keep the observer, shrink its job

You can keep a single ResizeObserver — the danger was per-item callbacks mutating layout, not observation itself. Restrict it to one sentinel row and re-measure the rest against a shared trigger:

const ro = new ResizeObserver((entries) => { for (const entry of entries) { const id = entry.target.dataset.rowId; reconcile(id, entry.contentRect.height); } }); // Observe only rows currently painted by the virtualizer. // Unobserve on recycle. Never write layout in this callback. 

Two constraints make this safe. Observe only rows that are actually on screen, so the callback volume stays bounded by viewport size. And never set any style or size inside the callback — schedule it with requestAnimationFrame so a resize cannot trigger a resize within the same frame.

The edge case that still bites: scroll anchoring

When a row above the viewport changes height, browsers with scroll anchoring (Chrome, Firefox) try to keep the visible content stable by adjusting scrollTop. If your virtualizer also adjusts scrollTop during reconciliation, the two corrections stack and the user sees a small hop.

The fix is to set overflow-anchor: none on your scrolling container and do the anchor compensation yourself inside the same rAF batch as your height update:

.scroller { overflow-anchor: none; } 

Owning the compensation means one correction per frame instead of two fighting ones. It is a small CSS line with a surprisingly large effect on perceived smoothness.

Key takeaways

  • Cached row heights go stale on font load, image load, and any row-internal state change, so plan a reconciliation path from day one.
  • content-visibility: auto with contain-intrinsic-size: auto Npx self-corrects rendered rows but cannot help a virtualizer that needs accurate heights for unseen rows.
  • A single shared ResizeObserver is fine if you observe only painted rows and never write layout in its callback.
  • Disable native scroll anchoring on virtualized containers and do anchor compensation in the same frame as your height update.

Source

Your Per-Item ResizeObserver Is a Layout Feedback Loop — this follow-up covers stale cached measurements, content-visibility tradeoffs, a reconciliation pattern, and scroll anchoring, none of which the original post addressed.

Support this work

These write-ups are researched and published with no paywall, sponsor, or tracking. If one saved you an afternoon, a small tip keeps them coming.

USDT, USDC or USDD · TRC-20 (Tron)

TFTNsfyomKrnUutRjBTGVULp19ByW29KbY