Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): Add lazy loading for images #784

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const nextConfig = {
}
return config;
},
images: {
domains: ['appx-recordings.s3.ap-south-1.amazonaws.com', 'cdn.dribbble.com'],
},
};

module.exports = nextConfig;
6 changes: 3 additions & 3 deletions src/components/CourseCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { SecondaryButton } from './buttons/SecondaryButton';
import { useRouter } from 'next/navigation';
import { Button } from './ui/button';
import { ChevronRight } from 'lucide-react';
import { LazyLoadImage } from 'react-lazy-load-image-component';
import 'react-lazy-load-image-component/src/effects/blur.css';
import { LazyLoadImage } from './LazyLoadImage';

export const CourseCard = ({
course,
Expand Down Expand Up @@ -48,8 +47,9 @@ export const CourseCard = ({
</div>
<LazyLoadImage
alt={course.title}
effect="blur"
src={course.imageUrl}
width={400}
height={250}
className="rounded-md"
/>
<div className="p-2">
Expand Down
60 changes: 60 additions & 0 deletions src/components/LazyLoadImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Image from 'next/image';
import React, { useState, useRef, useEffect } from 'react';

const registerObserver = (
Copy link
Collaborator

@siinghd siinghd Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this complex logic? NextJs Image its already loas images like that...
what i was saying is until the image isnt loaded show the skeleton

Copy link
Author

@senthil-athiban senthil-athiban Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to observe the viewport by writing custom logic. Utilizing this LazyLoadImage component, we can customize our own skeleton, and styling effect.

ref: React.RefObject<Element>,
setShowImage: React.Dispatch<React.SetStateAction<boolean>>,
) => {
if (!ref.current) {
console.log(' Ref is not attached to a DOM element');
return;
}
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
setShowImage(true);
observer.disconnect();
});
});
observer.observe(ref.current);
};

interface LazyLoadImageProps {
src: string;
alt: string;
className: string;
width?: number;
height?: number;
}
export const LazyLoadImage = ({
src,
alt,
className,
width,
height,
}: LazyLoadImageProps) => {
const [showImage, setshowImage] = useState(false);
const imgRef = useRef(null);
useEffect(() => {
registerObserver(imgRef, setshowImage);
}, []);
return showImage ? (
<Image
alt={alt}
src={src}
className={className}
width={width}
height={height}
/>
) : (
<Image
alt={alt}
src={src}
className={className}
width={width}
height={height}
ref={imgRef}
style={{ filter: 'blur(4px)' }}
/>
);
};