Skip to content

Commit 3b7d758

Browse files
committed
use object tag instead of manually writing error handler
1 parent c962fb4 commit 3b7d758

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

web/src/components/ImageFallback.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ type Props = {
55
fallback: React.ReactElement;
66
width: string;
77
height: string;
8-
alt: string;
98
};
109

1110
// https://medium.com/@abhishekmicosoft/handling-img-fallback-307653b2f30
12-
export function ImageFallback({ width, height, url, fallback, alt }: Props) {
11+
export function ImageFallback({ width, height, url, fallback }: Props) {
1312
const [ok, setOK] = useState<boolean>(true);
1413
useEffect(() => {
1514
url;
@@ -20,21 +19,27 @@ export function ImageFallback({ width, height, url, fallback, alt }: Props) {
2019
: url;
2120

2221
return ok ? (
23-
<img
24-
src={URL}
22+
<object
23+
data={URL}
24+
type="image/webp"
25+
width={width} // there probably prevent style shaking
26+
height={height}
2527
style={{
2628
width,
2729
height,
2830
objectFit: "cover",
2931
borderRadius: "50%",
3032
pointerEvents: "none",
3133
}}
32-
onError={() => {
33-
console.log("failed to fetch image data of:", URL);
34-
setOK(false);
35-
}}
36-
alt={alt}
37-
/>
34+
>
35+
<img
36+
src="/avatar-fallback.webp"
37+
width={width}
38+
height={height}
39+
style={{ width, height }}
40+
alt=""
41+
/>
42+
</object>
3843
) : (
3944
<>{fallback}</>
4045
);

web/src/components/human/WithFallback.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export function AvatarWithFallback({ width, height, url }: Props) {
1818
sx={{ width: `${width}`, height: `${height}`, objectFit: "cover" }}
1919
/>
2020
}
21-
alt=""
2221
/>
2322
);
2423
}

0 commit comments

Comments
 (0)