From b08df2a966633644197f80bb09bd0f74c7bccb2b Mon Sep 17 00:00:00 2001 From: James Xylight Date: Mon, 14 Jul 2025 16:40:55 -0700 Subject: [PATCH] impr: optimize image delivery --- src/lib/components/lemmy/post/helpers.ts | 20 ++++----- .../lemmy/post/link/PostLink.svelte | 39 ++++++++-------- .../lemmy/post/media/PostImage.svelte | 45 ++++++++++++------- .../lemmy/post/media/PostMedia.svelte | 1 - .../lemmy/post/media/PostMediaCompact.svelte | 27 ++++++++++- 5 files changed, 82 insertions(+), 50 deletions(-) diff --git a/src/lib/components/lemmy/post/helpers.ts b/src/lib/components/lemmy/post/helpers.ts index 20fa5a1a..5dd670a9 100644 --- a/src/lib/components/lemmy/post/helpers.ts +++ b/src/lib/components/lemmy/post/helpers.ts @@ -7,17 +7,16 @@ import type { CommentView, PersonView, Post } from 'lemmy-js-client' export const isCommentMutable = (comment: CommentView, me: PersonView) => me.person.id == comment.creator.id +// Algorithm to determine the best image URL to use export const bestImageURL = ( post: Post, - compact: boolean = true, + thumbnail: boolean = true, width: number = 1024, + format: 'avif' | 'webp' | null = 'avif', ) => { - if (compact) { - /* empty */ - } - if (post.url) return optimizeImageURL(post.url, width) - else if (post.thumbnail_url) - return optimizeImageURL(post.thumbnail_url, width) + if (post.thumbnail_url && (thumbnail || !post.url)) + return optimizeImageURL(post.thumbnail_url, width, format) + else if (post.url) return optimizeImageURL(post.url, width, format) return post.url ?? '' } @@ -25,11 +24,12 @@ export const bestImageURL = ( export const optimizeImageURL = ( urlStr: string, width: number = 1024, + format: 'avif' | 'webp' | null = 'avif', ): string => { try { const url = new URL(urlStr) - if (!url.searchParams.has('format')) url.searchParams.set('format', 'webp') + if (format) url.searchParams.set('format', format) if (width > 0 && !url.searchParams.has('thumbnail')) { url.searchParams.set( @@ -41,10 +41,6 @@ export const optimizeImageURL = ( ) } - if (width == -1) { - url.searchParams.delete('thumbnail') - } - return url.toString() } catch (e) { console.error(e) diff --git a/src/lib/components/lemmy/post/link/PostLink.svelte b/src/lib/components/lemmy/post/link/PostLink.svelte index 98809f73..1f124343 100644 --- a/src/lib/components/lemmy/post/link/PostLink.svelte +++ b/src/lib/components/lemmy/post/link/PostLink.svelte @@ -30,11 +30,11 @@ {#if embed_title && view == 'cozy'} @@ -60,24 +60,27 @@ - - - + {#each ['webp'] as format} + + {/each}  diff --git a/src/lib/components/lemmy/post/media/PostImage.svelte b/src/lib/components/lemmy/post/media/PostImage.svelte index 2ad229a8..d56c3764 100644 --- a/src/lib/components/lemmy/post/media/PostImage.svelte +++ b/src/lib/components/lemmy/post/media/PostImage.svelte @@ -40,27 +40,38 @@ scale-[1.2] opacity-50" /> - - - + {#each ['webp'] as format} + + {/each} {post.alt_text (imageLoaded = true)} /> diff --git a/src/lib/components/lemmy/post/media/PostMedia.svelte b/src/lib/components/lemmy/post/media/PostMedia.svelte index aeb6475b..c7c08697 100644 --- a/src/lib/components/lemmy/post/media/PostMedia.svelte +++ b/src/lib/components/lemmy/post/media/PostMedia.svelte @@ -46,7 +46,6 @@ url={post.url} thumbnail_url={post.thumbnail_url} nsfw={post.nsfw} - embed_description={post.embed_description} embed_title={post.embed_title} {view} /> diff --git a/src/lib/components/lemmy/post/media/PostMediaCompact.svelte b/src/lib/components/lemmy/post/media/PostMediaCompact.svelte index 58dae156..1a17c10e 100644 --- a/src/lib/components/lemmy/post/media/PostMediaCompact.svelte +++ b/src/lib/components/lemmy/post/media/PostMediaCompact.svelte @@ -1,7 +1,6 @@