From a496c047013e620e3996e4ed64390af6c6a4e18d Mon Sep 17 00:00:00 2001 From: jeffvli Date: Thu, 29 Jan 2026 19:12:07 -0800 Subject: [PATCH] persist image render after entering viewport --- src/shared/components/image/image.tsx | 35 +++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) 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 ? (