[Bug Fix] Avatar: lazy-loaded image never appears (stuck on fallback) (#415)

Merged
DJ
5mo ago
+20-2

Fixes #415.

Problem

An uncached AvatarImage can get permanently stuck on the fallback and never appear.

avatar_controller.js hides a still-loading image with display:none (the hidden class) so the fallback shows while the image loads:

if (this.imageTarget.complete && this.imageTarget.naturalWidth > 0) {
  this.showImage();
} else {
  this.showFallback(); // adds `hidden` -> display:none on the <img>
}

But avatar_image.rb rendered the image with loading="lazy". Browsers do not fetch a loading="lazy" image whose box is not generated (display:none), so the load event never fires and showImage() is never called — the image stays hidden indefinitely. Cached images are unaffected because they are already complete at connect().

This is a self-contradiction: loading="lazy" defers loading until the element is visible, while connect() makes the element invisible before it has loaded.

Fix

Remove loading="lazy" from AvatarImage. The image now loads eagerly even while hidden, so the load/error handlers reliably reveal it (and the fallback is shown while loading, exactly as intended). This matches shadcn/radix, which do not lazy-load the avatar image.

Minimal change, no JS or markup restructure.

Verification

Reproduced in a browser harness with cache-busting URLs and a delayed image server:

  • Before (lazy + hide): image request is never made; fallback shows forever.
  • After (eager + hide): fallback shows during load, then the image appears on load; on error the fallback remains.

Tests: added a regression test asserting the rendered image is not loading="lazy". bundle exec rake (test + standardrb) green — 223 runs, 0 failures; 366 files, no offenses.

Notes

  • Alternatives considered (and rejected as more invasive / less reliable): only hiding on definitive failure (leaves an empty box instead of the fallback during load), or hiding via opacity/visibility + absolute overlay to keep lazy (CSS-hidden + lazy load behavior is browser-dependent). Dropping lazy is the simplest fix and aligns with shadcn/radix.
  • No docs example change needed — behavior-only fix.

Summary by cubic

Stop lazy-loading the avatar image so it loads while hidden and no longer gets stuck on the fallback. Fixes #415.

  • Bug Fixes
    • Removed loading="lazy" from AvatarImage; rebuilt MCP registry.
    • Ensures load/error handlers run and the image replaces the fallback; added a regression test that it's not lazy-loaded.