26 lines
713 B
TypeScript
26 lines
713 B
TypeScript
"use client";
|
|
|
|
import NextImage, { type ImageProps } from "next/image";
|
|
import { useState } from "react";
|
|
|
|
const CDN_ORIGIN = "https://buzz-cdn.astrxl.dev";
|
|
|
|
export default function ResilientImage({ src, unoptimized, onError, ...props }: ImageProps) {
|
|
const [failedSource, setFailedSource] = useState<string | null>(null);
|
|
const useOriginal = typeof src === "string" && failedSource === src;
|
|
|
|
return (
|
|
<NextImage
|
|
{...props}
|
|
src={src}
|
|
unoptimized={unoptimized || useOriginal}
|
|
onError={(event) => {
|
|
if (!useOriginal && typeof src === "string" && src.startsWith(`${CDN_ORIGIN}/`)) {
|
|
setFailedSource(src);
|
|
}
|
|
onError?.(event);
|
|
}}
|
|
/>
|
|
);
|
|
}
|