Skip to content

Commit

Permalink
feat: apply intersectionObserver for lazy rendering to improve CLS an…
Browse files Browse the repository at this point in the history
…d Speed Index
  • Loading branch information
hwanheejung committed Dec 29, 2024
1 parent 3f46e02 commit a8cc347
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react'
import { useEffect, useRef, useState } from 'react'
import { Polaroid } from '@/types'
import PolaroidCard from '@/components/Polaroid/PolaroidCard'
import { BoardTutorial } from '@/components/Tutorial'
Expand All @@ -20,6 +20,26 @@ const PolaroidListItem = ({
}: PolaroidListItemProps) => {
const [rotate, setRotate] = useState<number>(0)
const { data: session } = useSession()
const [isVisible, setIsVisible] = useState(false)
const ref = useRef<HTMLDivElement>(null)

useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setIsVisible(true)
observer.disconnect()
}
},
{ threshold: 0.05 },
)

if (ref.current) observer.observe(ref.current)

return () => {
if (ref.current) observer.disconnect()
}
}, [])

useEffect(() => {
const randomRotate =
Expand All @@ -40,18 +60,26 @@ const PolaroidListItem = ({
</div>
)

return isFirstItem ? (
<BoardTutorial
step={session ? 3 : 2}
tooltip={<Step3Tooltip />}
hasNext={false}
targetStyle="FIT"
targetStyleProperites={{ rotate: `${rotate}deg`, borderRadius: '2px' }}
>
{renderItem}
</BoardTutorial>
) : (
renderItem
return (
<div ref={ref}>
{isVisible &&
(isFirstItem ? (
<BoardTutorial
step={session ? 3 : 2}
tooltip={<Step3Tooltip />}
hasNext={false}
targetStyle="FIT"
targetStyleProperites={{
rotate: `${rotate}deg`,
borderRadius: '2px',
}}
>
{renderItem}
</BoardTutorial>
) : (
renderItem
))}
</div>
)
}

Expand Down
7 changes: 4 additions & 3 deletions src/components/Polaroid/Base/PolaroidImage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client'

import { FILTERS } from '@/lib'
import { useEffect, useState } from 'react'
import { getImageWidthHeight } from '@/lib/utils/image'
import Image from 'next/image'
import { useEffect, useState } from 'react'

interface PolaroidImageProps {
imageUrl: string
Expand Down Expand Up @@ -40,9 +40,10 @@ const PolaroidImage = ({ imageUrl }: PolaroidImageProps) => {
<Image
src={imageUrl}
alt="폴라로이드 이미지"
width={1000}
height={1000}
width={500}
height={500}
quality={100}
priority
className={`${aspectRatio} object-cover`}
style={{
filter: FILTERS.POLAROID,
Expand Down

0 comments on commit a8cc347

Please sign in to comment.