Skip to content

Commit

Permalink
Lazy load preview images
Browse files Browse the repository at this point in the history
  • Loading branch information
NeKzor committed May 21, 2024
1 parent 89dd2ae commit 1263892
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/server/app/assets/js/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ const minWidthBreakpoints = {
md: 768,
};

const initPreviews = () => {
const previews = document.querySelectorAll('[x-preview]');
for (const preview of previews) {
preview.addEventListener(
'mouseover',
() => {
preview.src = preview.getAttribute('x-preview');
},
{ once: true },
);
}
};

// Home

if (location.pathname === '/') {
initPreviews();
}

// Navbar

const navItemsLeft = document.getElementById('nav-items-left');
Expand Down Expand Up @@ -380,6 +399,7 @@ if (location.pathname.startsWith('/search') && location.search.length !== 0) {
search.open();
}

initPreviews();
initShareModal();
}

Expand All @@ -389,3 +409,9 @@ const goBackButton = document.getElementById('not-found-go-back');
if (goBackButton) {
goBackButton.addEventListener('click', () => history.back());
}

// Profile

if (location.pathname.startsWith('/profile/')) {
initPreviews();
}
2 changes: 1 addition & 1 deletion src/server/app/components/VideoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const VideoCard = ({ video }: { video: VideoCardData }) => {
{video.video_preview_url && (
<img
className={tw`absolute top-0 left-0 opacity-0 transition-opacity duration-300 transform hover:opacity-100 object-cover w-full h-full rounded-[12px]`}
src={video.video_preview_url}
x-preview={video.video_preview_url}
alt='Preview of video'
/>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/server/app/components/VideoRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const VideoRow = ({ video }: { video: VideoRowData }) => {
{video.video_preview_url && (
<img
className={tw`absolute top-0 left-0 opacity-0 transition-opacity duration-300 transform hover:opacity-100 object-cover w-full h-full rounded-[12px]`}
src={video.video_preview_url}
x-preview={video.video_preview_url}
alt='Preview of video'
/>
)}
Expand Down

0 comments on commit 1263892

Please sign in to comment.