diff --git a/src/shared/components/image/image.tsx b/src/shared/components/image/image.tsx index 70b6fd5f8..1025932e1 100644 --- a/src/shared/components/image/image.tsx +++ b/src/shared/components/image/image.tsx @@ -6,6 +6,7 @@ import { type ImgHTMLAttributes, memo, ReactNode, + useRef, } from 'react'; import { Img } from 'react-image'; @@ -144,6 +145,22 @@ function ImageWithDebounce({ const { inViewport, ref } = enableViewport ? viewport : { inViewport: true, ref: undefined }; const { className: containerPropsClassName, ...restContainerProps } = imageContainerProps || {}; + const hasBeenInViewportRef = useRef(false); + const prevDebouncedSrcRef = useRef(debouncedSrc); + + if (prevDebouncedSrcRef.current !== debouncedSrc) { + prevDebouncedSrcRef.current = debouncedSrc; + hasBeenInViewportRef.current = false; + } + + if (inViewport && debouncedSrc) { + hasBeenInViewportRef.current = true; + } + + const shouldShowImage = enableViewport + ? (inViewport || hasBeenInViewportRef.current) && debouncedSrc + : debouncedSrc; + if (enableViewport) { return ( - {inViewport && debouncedSrc ? ( + {shouldShowImage && debouncedSrc ? ( - {inViewport && src ? ( + {shouldShowImage ? (