-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 식품 이미지 미처 적용 안됐던 lazy loading 적용 (#529)
* feat: FoodItem lazy loading 적용 * refactor: observerRef를 RefObject > MutableRefObject로 변경 * refactor: SuspendedImg의 lazy loading을 LazyImage를 활용하도록 변경 * refactor: LazyImage > LazyImg 네이밍 변경 * refactor: LazyImg 불필요한 loading 속성 제거
- Loading branch information
Showing
5 changed files
with
46 additions
and
52 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ComponentPropsWithoutRef, ForwardedRef, forwardRef, memo, useCallback } from 'react'; | ||
|
||
import { useIntersectionObserver } from '@/hooks/@common/useIntersectionObserver'; | ||
|
||
const LazyImg = forwardRef( | ||
(props: ComponentPropsWithoutRef<'img'>, ref: ForwardedRef<HTMLImageElement>) => { | ||
const { src, ...restProps } = props; | ||
|
||
const { targetRef, isIntersected } = useIntersectionObserver<HTMLImageElement>({ | ||
observerOptions: { threshold: 0.1 }, | ||
}); | ||
|
||
const callbackRef = useCallback((instance: HTMLImageElement | null) => { | ||
targetRef.current = instance; | ||
|
||
if (!ref) return; | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
typeof ref === 'function' ? ref(instance) : (ref.current = instance); | ||
}, []); | ||
|
||
if (isIntersected && targetRef.current && !targetRef.current.src && src) { | ||
targetRef.current.src = src; | ||
} | ||
|
||
// eslint-disable-next-line jsx-a11y/alt-text | ||
return <img {...restProps} ref={callbackRef} />; | ||
}, | ||
); | ||
|
||
LazyImg.displayName = 'LazyImg'; | ||
|
||
export default memo(LazyImg); |
32 changes: 10 additions & 22 deletions
32
frontend/src/components/@common/SuspendedImg/SuspendedImg.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,43 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { ComponentPropsWithoutRef, ComponentPropsWithRef, useEffect } from 'react'; | ||
import { ComponentPropsWithoutRef, memo } from 'react'; | ||
|
||
import { useIntersectionObserver } from '@/hooks/@common/useIntersectionObserver'; | ||
|
||
import LazyImg from '../LazyImg/LazyImg'; | ||
|
||
interface SuspendedImgProps extends ComponentPropsWithoutRef<'img'> { | ||
staleTime?: number; | ||
cacheTime?: number; | ||
enabled?: boolean; | ||
lazy?: boolean; | ||
} | ||
|
||
// eslint-disable-next-line react/display-name | ||
const SuspendedImg = (props: SuspendedImgProps) => { | ||
const { src, cacheTime, staleTime, enabled, lazy, ...restProps } = props; | ||
const { src, cacheTime, staleTime, lazy, ...restProps } = props; | ||
|
||
const img = new Image(); | ||
|
||
const { targetRef, isIntersected } = useIntersectionObserver<HTMLImageElement>({ | ||
observerOptions: { threshold: 0.1 }, | ||
}); | ||
|
||
const lazyOptions: ComponentPropsWithRef<'img'> & { 'data-src'?: string } = { | ||
loading: 'lazy', | ||
ref: targetRef, | ||
'data-src': src, | ||
}; | ||
|
||
useQuery({ | ||
queryKey: [src], | ||
queryFn: () => | ||
new Promise(resolve => { | ||
img.onload = resolve; | ||
img.onerror = resolve; | ||
|
||
img.src = src!; | ||
}), | ||
...(staleTime == null ? {} : { staleTime }), | ||
...(cacheTime == null ? {} : { cacheTime }), | ||
enabled: enabled && Boolean(src), | ||
enabled: lazy ? isIntersected : true, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!targetRef.current) return; | ||
|
||
if ('loading' in HTMLImageElement.prototype || isIntersected) { | ||
targetRef.current.src = String(targetRef.current.dataset.src); | ||
} | ||
}, [isIntersected]); | ||
|
||
if (lazy) { | ||
return <LazyImg src={src} ref={targetRef} {...restProps} />; | ||
} | ||
// eslint-disable-next-line jsx-a11y/alt-text | ||
return <img {...restProps} {...(lazy ? lazyOptions : { src })} />; | ||
return <img src={src} {...restProps} />; | ||
}; | ||
|
||
export default SuspendedImg; | ||
export default memo(SuspendedImg); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters