lazy load images only starting with the fifth post

This commit is contained in:
2025-07-20 11:41:16 +02:00
parent 3da5b1a974
commit f506a11416
3 changed files with 27 additions and 8 deletions

View File

@ -3,10 +3,12 @@
interface Props { interface Props {
account: Account; account: Account;
lazyLoadImages: Boolean;
} }
let { account }: Props = $props(); let { account, lazyLoadImages = true }: Props = $props();
let avatarDescription: string = $derived(`Avatar for ${account.acct}`); let avatarDescription: string = $derived(`Avatar for ${account.acct}`);
let loadingProp = $derived(lazyLoadImages ? 'lazy' : 'eager');
let sourceSetHtml: string = $derived.by(() => { let sourceSetHtml: string = $derived.by(() => {
// Sort thumbnails by file type. This is important, because the order of the srcset entries matter. // Sort thumbnails by file type. This is important, because the order of the srcset entries matter.
// We need the best format to be first // We need the best format to be first
@ -41,7 +43,7 @@
<picture> <picture>
{@html sourceSetHtml} {@html sourceSetHtml}
<img src={account.avatar} alt={avatarDescription} loading="lazy" width="50" height="50" /> <img src={account.avatar} alt={avatarDescription} loading={loadingProp} width="50" height="50" />
</picture> </picture>
<style> <style>

View File

@ -8,9 +8,10 @@
interface Props { interface Props {
post: Post; post: Post;
lazyLoadImages: Boolean;
} }
let { post }: Props = $props(); let { post, lazyLoadImages = true }: Props = $props();
let displayRelativeTime = $state(false); let displayRelativeTime = $state(false);
const absoluteDate = new Date(post.created_at).toLocaleString(); const absoluteDate = new Date(post.created_at).toLocaleString();
const timePassed = secondsSince(new Date(post.created_at)); const timePassed = secondsSince(new Date(post.created_at));
@ -20,6 +21,7 @@
} }
return absoluteDate; return absoluteDate;
}); });
let loadingProp = $derived(lazyLoadImages ? 'lazy' : 'eager');
const songs = filterDuplicates(post.songs ?? []); const songs = filterDuplicates(post.songs ?? []);
@ -115,7 +117,7 @@
</script> </script>
<div class="wrapper"> <div class="wrapper">
<div class="avatar"><AvatarComponent account={post.account} /></div> <div class="avatar"><AvatarComponent account={post.account} {lazyLoadImages} /></div>
<div class="account"><AccountComponent account={post.account} /></div> <div class="account"><AccountComponent account={post.account} /></div>
<div class="meta"> <div class="meta">
<small><a href={post.url} target="_blank" title={absoluteDate}>{dateCreated}</a></small> <small><a href={post.url} target="_blank" title={absoluteDate}>{dateCreated}</a></small>
@ -127,7 +129,12 @@
<div class="info-wrapper"> <div class="info-wrapper">
<picture> <picture>
{@html getSourceSetHtml(song)} {@html getSourceSetHtml(song)}
<img class="bgimage" src={song.thumbnailUrl} loading="lazy" alt="Blurred cover" /> <img
class="bgimage"
src={song.thumbnailUrl}
loading={loadingProp}
alt="Blurred cover"
/>
</picture> </picture>
<a href={song.pageUrl ?? song.postedUrl} target="_blank"> <a href={song.pageUrl ?? song.postedUrl} target="_blank">
<div class="info"> <div class="info">
@ -136,7 +143,7 @@
<img <img
src={song.thumbnailUrl} src={song.thumbnailUrl}
alt="Cover for {song.artistName} - {song.title}" alt="Cover for {song.artistName} - {song.title}"
loading="lazy" loading={loadingProp}
width={song.thumbnailWidth} width={song.thumbnailWidth}
height={song.thumbnailHeight} height={song.thumbnailHeight}
/> />
@ -193,6 +200,12 @@
border-radius: 3px; border-radius: 3px;
margin-bottom: 3px; margin-bottom: 3px;
} }
.cover img {
max-width: 200px;
max-height: 200px;
object-fit: contain;
border-radius: 3px;
}
.bgimage { .bgimage {
display: none; display: none;
background-color: var(--color-bg); background-color: var(--color-bg);
@ -231,6 +244,10 @@
backdrop-filter: blur(10px); backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
} }
.cover img {
max-width: 60px;
max-height: 60px;
}
.bgimage { .bgimage {
display: block; display: block;
width: 100%; width: 100%;

View File

@ -163,7 +163,7 @@
{#if posts.length === 0} {#if posts.length === 0}
Sorry, no posts recommending music have been found yet Sorry, no posts recommending music have been found yet
{/if} {/if}
{#each posts as post (post.url)} {#each posts as post, index (post.url)}
<div <div
class="post" class="post"
transition:edgeFly|global={{ transition:edgeFly|global={{
@ -173,7 +173,7 @@
easing: cubicInOut easing: cubicInOut
}} }}
> >
<PostComponent {post} /> <PostComponent {post} lazyLoadImages={index >= 4} />
</div> </div>
{/each} {/each}
<LoadMoreComponent <LoadMoreComponent