Skip to content

Commit

Permalink
refactor: Creator Image Lazy loading ์ ์šฉ (#795)
Browse files Browse the repository at this point in the history
* feat: fallback src๋„ lazy loading์„ ์ง€์›ํ•˜๋„๋ก ๋ณ€๊ฒฝ

* feat: fallback src ์ ์šฉ

* feat: lazy loading์„ ์ ์šฉํ•˜์—ฌ ์ดˆ๊ธฐ ๋กœ๋”ฉ ์‹œ ์•„๋ฐ”ํƒ€ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค์ง€ ์•Š๊ฒŒ ์„ค์ •

* style: ์ด๋ฏธ์ง€ url ์œ ํ‹ธ ํ•จ์ˆ˜ ์‚ฌ์šฉ
  • Loading branch information
jinhokim98 authored and Todari committed Oct 24, 2024
1 parent 9e97796 commit 312a1c1
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 42 deletions.
11 changes: 9 additions & 2 deletions client/src/hooks/useImageLazyLoading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ import {useEffect, useState} from 'react';
type UseImageLazyLoadingProps<T extends HTMLElement> = {
targetRef: React.RefObject<T>;
src: string;
fallbackSrc?: string;
threshold?: number;
};

type ImgTagSrcType = string | undefined;

const useImageLazyLoading = <T extends HTMLElement>({
targetRef,
src,
fallbackSrc,
threshold = 0.05,
}: UseImageLazyLoadingProps<T>) => {
const [imageSrc, setImageSrc] = useState<string | undefined>(undefined);
const [imageSrc, setImageSrc] = useState<ImgTagSrcType>(undefined);
const [fallbackImageSrc, setFallbackImageSrc] = useState<ImgTagSrcType>(undefined);

useEffect(() => {
if (targetRef && !imageSrc) {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setImageSrc(src);
setFallbackImageSrc(fallbackSrc);
if (targetRef.current) {
observer.unobserve(targetRef.current);
}
Expand All @@ -39,10 +45,11 @@ const useImageLazyLoading = <T extends HTMLElement>({
}

return;
}, [targetRef, src]);
}, [targetRef, src, fallbackSrc]);

return {
imageSrc,
fallbackImageSrc,
};
};

Expand Down
7 changes: 6 additions & 1 deletion client/src/pages/MainPage/Section/CreatorSection/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ interface Props {
const Avatar = ({imagePath, name, navigateUrl}: Props) => {
return (
<a href={navigateUrl} target="_blank" css={avatarStyle}>
<Image src={getImageUrl(imagePath, 'webp')} fallbackSrc={getImageUrl(imagePath, 'png')} css={avatarImageStyle} />
<Image
src={getImageUrl(imagePath, 'webp')}
fallbackSrc={getImageUrl(imagePath, 'png')}
loading="lazy"
css={avatarImageStyle}
/>
<Text size="bodyBold" textColor="white" responsive={true}>
{name}
</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ import {descriptionSectionStyle, imgStyle} from './DescriptionSection.style';
const DescriptionSection = () => {
const descriptionRef = useRef<HTMLDivElement>(null);

const {imageSrc} = useImageLazyLoading({
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: descriptionRef,
src: `${process.env.IMAGE_URL}/standingDog.webp`,
src: getImageUrl('standingDog', 'webp'),
fallbackSrc: getImageUrl('standingDog', 'png'),
threshold: 0.05,
});

return (
<div css={descriptionSectionStyle} ref={descriptionRef}>
<Image
css={imgStyle}
src={imageSrc!}
alt="ํ–‰๋Œ•์ด - ํ–‰๋™๋Œ€์žฅ ๋งˆ์Šค์ฝ”ํŠธ"
fallbackSrc={getImageUrl('standingDog', 'png')}
/>
<Image css={imgStyle} src={imageSrc!} alt="ํ–‰๋Œ•์ด - ํ–‰๋™๋Œ€์žฅ ๋งˆ์Šค์ฝ”ํŠธ" fallbackSrc={fallbackImageSrc} />
<img css={imgStyle} />
<Text style={{textAlign: 'center'}} size="subTitle" responsive={true}>{`ํ–‰๋™๋Œ€์žฅ๋“ค์„ ์œ„ํ•ด
ํ–‰๋™๋Œ€์žฅ์„ ์ค€๋น„ํ–ˆ์–ด์š”
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './AutoCalculate.style';

const AutoCalculate = () => {
const sectionRef = useRef<HTMLElement>(null);

const {imageSrc} = useImageLazyLoading({
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: sectionRef,
src: `${process.env.IMAGE_URL}/feature2.webp`,
src: getImageUrl('feature2', 'webp'),
fallbackSrc: getImageUrl('feature2', 'png'),
threshold: 0.05,
});

return (
<section css={sectionStyle} ref={sectionRef}>
<article css={articleStyle}>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
<div css={textContainerStyle}>
<Text size="subTitle" responsive={true}>
๊ณ„์‚ฐ์€ ์ €ํฌ๊ฐ€ ์•Œ์•„์„œ ํ•ด๋“œ๋ ค์š”
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './CheckDeposit.style';

const CheckDeposit = () => {
const sectionRef = useRef<HTMLElement>(null);

const {imageSrc} = useImageLazyLoading({
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: sectionRef,
src: `${process.env.IMAGE_URL}/feature3.webp`,
src: getImageUrl('feature3', 'webp'),
fallbackSrc: getImageUrl('feature3', 'png'),
threshold: 0.05,
});

Expand All @@ -30,7 +33,7 @@ const CheckDeposit = () => {
๊ฐ„ํŽธํ•˜๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์š”.`}
</Text>
</div>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
</article>
</section>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {css} from '@emotion/react';
import {useRef} from 'react';

import useMainPageYScroll from '@hooks/useMainPageYScroll';

Expand All @@ -11,7 +10,6 @@ import {RecordMemoryWithPhoto} from './RecordMemoryWithPhoto';

const FeatureSection = () => {
const {featureSectionRef} = useMainPageYScroll();
const simpleTransferRef = useRef<HTMLElement>(null);

return (
<div
Expand All @@ -27,8 +25,8 @@ const FeatureSection = () => {
<SimpleShare />
<AutoCalculate />
<CheckDeposit />
<SimpleTransfer targetRef={simpleTransferRef} />
<RecordMemoryWithPhoto targetRef={simpleTransferRef} />
<SimpleTransfer />
<RecordMemoryWithPhoto />
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import {useRef} from 'react';

import Image from '@components/Design/components/Image/Image';

import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './RecordMemoryWithPhoto.style';

type RecordMemoryWithPhotoProps = {
targetRef: React.RefObject<HTMLElement>;
};
const RecordMemoryWithPhoto = () => {
const sectionRef = useRef<HTMLElement>(null);

const RecordMemoryWithPhoto = ({targetRef}: RecordMemoryWithPhotoProps) => {
const {imageSrc} = useImageLazyLoading({
targetRef,
src: `${process.env.IMAGE_URL}/feature5.webp`,
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: sectionRef,
src: getImageUrl('feature5', 'webp'),
fallbackSrc: getImageUrl('feature5', 'png'),
threshold: 0.05,
});

return (
<section css={sectionStyle}>
<section css={sectionStyle} ref={sectionRef}>
<article css={articleStyle}>
<div css={textContainerStyle}>
<Text size="subTitle" responsive={true}>
Expand All @@ -30,7 +33,7 @@ const RecordMemoryWithPhoto = ({targetRef}: RecordMemoryWithPhotoProps) => {
์ •์‚ฐ์€ ํˆฌ๋ช…ํ•˜๊ฒŒ, ์ถ”์–ต์€ ์˜ค๋ž˜์˜ค๋ž˜ ๊ฐ„์งํ•  ์ˆ˜ ์žˆ์–ด์š”.`}
</Text>
</div>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
</article>
</section>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './SimpleShare.style';

const SimpleAccount = () => {
const sectionRef = useRef<HTMLElement>(null);

const {imageSrc} = useImageLazyLoading({
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: sectionRef,
src: `${process.env.IMAGE_URL}/feature1.webp`,
src: getImageUrl('feature1', 'webp'),
fallbackSrc: getImageUrl('feature1', 'png'),
threshold: 0.05,
});

Expand All @@ -30,7 +33,7 @@ const SimpleAccount = () => {
๋ณต์žกํ•œ ์ ˆ์ฐจ ์—†์ด, ๋น ๋ฅด๊ฒŒ ์ •์‚ฐ์„ ๋งˆ์น˜์„ธ์š”.`}
</Text>
</div>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
</article>
</section>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import {useRef} from 'react';

import Image from '@components/Design/components/Image/Image';

import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './SimpleTransfer.style';

type SimpleTransferProps = {
targetRef: React.RefObject<HTMLElement>;
};
const SimpleTransfer = () => {
const simpleTransferRef = useRef<HTMLElement>(null);

const SimpleTransfer = ({targetRef}: SimpleTransferProps) => {
const {imageSrc} = useImageLazyLoading({
targetRef,
src: `${process.env.IMAGE_URL}/feature4.webp`,
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: simpleTransferRef,
src: getImageUrl('feature4', 'webp'),
fallbackSrc: getImageUrl('feature4', 'png'),
threshold: 0.05,
});

return (
<section css={sectionStyle} ref={targetRef}>
<section css={sectionStyle} ref={simpleTransferRef}>
<article css={articleStyle}>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
<div css={textContainerStyle}>
<Text size="subTitle" responsive={true}>
๋ช‡ ๋ฒˆ์˜ ํด๋ฆญ์œผ๋กœ ์†ก๊ธˆ ์™„๋ฃŒ!
Expand Down

0 comments on commit 312a1c1

Please sign in to comment.