Skip to content

fix(avatar): keep the GIF freeze-frame cache filling during virtualized scroll#770

Merged
mremond merged 1 commit into
mainfrom
claude/agitated-roentgen-87ba05
Jun 30, 2026
Merged

fix(avatar): keep the GIF freeze-frame cache filling during virtualized scroll#770
mremond merged 1 commit into
mainfrom
claude/agitated-roentgen-87ba05

Conversation

@mremond

@mremond mremond commented Jun 30, 2026

Copy link
Copy Markdown
Member

Summary

Animated GIF avatars are meant to show a frozen first frame and only play on hover. After message-list virtualization went default-on, they replayed their opening frames on every scroll-in. #711 added a shared first-frame cache to fix this, but the cache never actually filled during scrolling.

The first-frame extraction (fetch, decode, canvas) ran inside the Avatar component's effect and was cancelled on unmount. A virtualized row routinely unmounts mid-decode as it scrolls past, so cacheStaticFrame() was never reached and the shared cache stayed empty during scrolling, the one scenario it was built for. The cache only filled for avatars that happened to sit still long enough.

This moves extraction into a module-level extractFirstFrame() that runs to completion regardless of whether the requesting row unmounted, populating the shared cache from there. The hook now only guards the React state update, so it never sets state on an unmounted row. The function also dedupes concurrent decodes of the same avatar (it can be mounted in several visible rows at once) and resolves cleanly via img.onerror if a blob URL is revoked mid-decode instead of hanging.

A regression test covers the race the previous tests missed: a row that unmounts mid-extraction and then remounts must show the frozen frame with no re-fetch.

Known limitation

The very first decode of a given GIF in a session still animates briefly (tens of ms) until its first frame is extracted, which is inherent to client-side extraction. After that one-time decode, every subsequent mount and scroll-in is frozen. Eliminating that first flash would require eagerly warming the cache when avatars are received, tracked separately.

…ed scroll

Animated GIF avatars still replayed on every scroll-in despite the #711
cache. First-frame extraction ran inside the Avatar effect and was cancelled
on unmount, but a virtualized row routinely unmounts mid-decode while
scrolling past, so cacheStaticFrame() was never reached and the shared cache
never filled during scrolling, the exact case it was built for.

Extraction now lives in a module-level extractFirstFrame() that runs to
completion regardless of unmount and populates the cache; the hook only
guards the setFrame state update. It also dedupes concurrent decodes of the
same avatar and resolves cleanly if a blob URL is revoked mid-decode
(img.onerror) instead of hanging.
@mremond mremond added this to the 0.17.0 milestone Jun 30, 2026
@mremond mremond merged commit 41d350b into main Jun 30, 2026
3 checks passed
@mremond mremond deleted the claude/agitated-roentgen-87ba05 branch June 30, 2026 06:50
mremond added a commit that referenced this pull request Jun 30, 2026
## Summary

Follow-up to #770. Animated avatars are meant to show a frozen first
frame and play only on hover. #770 fixed the freeze-frame cache for
virtualized scrolling, but the freeze still only ever applied to literal
GIFs.

The decision "is this animated?" was made from the declared MIME type
(`blob.type === 'image/gif'`). That misses the common real-world cases:

- Many animated avatars are APNG or animated WebP, not GIF. WebKit
animates all three in an `<img>`.
- The SDK occupant-avatar path stores avatars as `image/png` regardless
of the real format (`Profile.ts` hardcodes it for PEP avatars and
defaults to it for vCard avatars with no `<TYPE>`), so even a GIF can
arrive mistyped.

So these avatars were never frozen and animated continuously (observed:
kuyuhi in #XSF, an animated PNG).

This detects animation from the actual image bytes instead of the
declared type: GIF header, APNG `acTL` control chunk, or animated-WebP
`ANIM` chunk. The existing canvas first-frame extraction already handles
any decodable raster format, so the freeze now covers all three. Static
JPEG/PNG/WebP are left untouched (no signature match), so there is no
extra work for the common case.

### Known gap

Animated AVIF/HEIF are not detected (rare as avatars, and WebKit
animation support for them is limited). They would keep animating.
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.

1 participant