fix(images) : fall back to original CDN file without transformations
This commit is contained in:
@@ -6,26 +6,17 @@ import { useState } from "react";
|
|||||||
const CDN_ORIGIN = "https://buzz-cdn.astrxl.dev";
|
const CDN_ORIGIN = "https://buzz-cdn.astrxl.dev";
|
||||||
|
|
||||||
export default function ResilientImage({ src, unoptimized, onError, ...props }: ImageProps) {
|
export default function ResilientImage({ src, unoptimized, onError, ...props }: ImageProps) {
|
||||||
const [fallback, setFallback] = useState<{ source: string; url: string } | null>(null);
|
const [failedSource, setFailedSource] = useState<string | null>(null);
|
||||||
const fallbackUrl = typeof src === "string" && fallback?.source === src ? fallback.url : null;
|
const useOriginal = typeof src === "string" && failedSource === src;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NextImage
|
<NextImage
|
||||||
{...props}
|
{...props}
|
||||||
src={fallbackUrl ?? src}
|
src={src}
|
||||||
unoptimized={unoptimized || Boolean(fallbackUrl)}
|
unoptimized={unoptimized || useOriginal}
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (!fallbackUrl && typeof src === "string" && src.startsWith(`${CDN_ORIGIN}/`)) {
|
if (!useOriginal && typeof src === "string" && src.startsWith(`${CDN_ORIGIN}/`)) {
|
||||||
const source = new URL(src);
|
setFailedSource(src);
|
||||||
if (!source.pathname.startsWith("/cdn-cgi/image/")) {
|
|
||||||
const optimized = new URL(event.currentTarget.currentSrc);
|
|
||||||
const width = Number(optimized.searchParams.get("w")) || 1080;
|
|
||||||
const quality = Number(optimized.searchParams.get("q")) || 75;
|
|
||||||
setFallback({
|
|
||||||
source: src,
|
|
||||||
url: `${CDN_ORIGIN}/cdn-cgi/image/width=${width},quality=${quality},format=auto${source.pathname}${source.search}`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
onError?.(event);
|
onError?.(event);
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user