Skip to content

Commit a688908

Browse files
committed
fix: enhance SpeakerAvatarImage to handle image loading failures
1 parent 9cd4aac commit a688908

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/components/common/SpeakerAvatarImage.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useState } from 'react'
3+
import { useCallback, useState } from 'react'
44
import { MissingAvatar } from './MissingAvatar'
55

66
interface SpeakerAvatarImageProps {
@@ -20,6 +20,14 @@ export function SpeakerAvatarImage({
2020
}: SpeakerAvatarImageProps) {
2121
const [failed, setFailed] = useState(false)
2222

23+
// Callback ref detects images that already failed before React hydration
24+
// attached the onError handler (e.g. broken URLs on slow connections).
25+
const imgRef = useCallback((img: HTMLImageElement | null) => {
26+
if (img?.complete && img.naturalWidth === 0) {
27+
setFailed(true)
28+
}
29+
}, [])
30+
2331
if (failed) {
2432
return (
2533
<MissingAvatar
@@ -33,6 +41,7 @@ export function SpeakerAvatarImage({
3341

3442
return (
3543
<img
44+
ref={imgRef}
3645
src={src}
3746
alt={name}
3847
className={`${className} h-full w-full object-cover object-center`}

0 commit comments

Comments
 (0)