impr: optimize image delivery

This commit is contained in:
James Xylight committed 2025-07-14 18:35:44 -07:00
1 parent 5c9c909787
commit b08df2a966
5 files changed
+82 -50

No files matched your search

+8 -12
View File
@@ -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)
@@ -30,11 +30,11 @@
{#if embed_title && view == 'cozy'}
<a target="_blank" href={url} class="group/link">
<Material
color="distinct"
class="flex flex-col-reverse sm:flex-row
overflow-hidden gap-4 embed-card z-0 relative
dark:border-t-zinc-800 border-opacity-80 max-w-full
dark:bg-zinc-950 group-hover/link:bg-slate-50 dark:group-hover/link:bg-zinc-900 transition-colors"
color="uniform"
class={[
'flex flex-col-reverse sm:flex-row overflow-hidden gap-4 embed-card z-0 relative',
'dark:bg-zinc-950 group-hover/link:bg-slate-50 dark:group-hover/link:bg-zinc-900 transition-colors',
]}
rounding="2xl"
element="article"
>
@@ -60,24 +60,27 @@
<picture
class="-m-4 mb-auto sm:-mb-4 sm:ml-auto shrink-0 sm:w-1/3 sm:max-w-60"
>
<source
srcset={optimizeImageURL(thumbnail_url, 256)}
media="(max-width: 256px)"
/>
<source
srcset={optimizeImageURL(thumbnail_url, 512)}
media="(max-width: 512px)"
/>
<source
srcset={optimizeImageURL(thumbnail_url, 1024)}
media="(max-width: 1024px)"
/>
{#each ['webp'] as format}
<source
srcset="{optimizeImageURL(
thumbnail_url,
256,
format as 'avif' | 'webp',
)} 256w, {optimizeImageURL(
thumbnail_url,
512,
format as 'avif' | 'webp',
)} 512w"
media="(min-width: 0px)"
type="image/{format}"
/>
{/each}
<img
src={optimizeImageURL(thumbnail_url, -1)}
class="object-cover w-full sm:max-w-96 h-32 sm:min-h-16 bg-slate-200 dark:bg-zinc-800"
width={600}
height={400}
alt=" "
alt=""
class:blur-3xl={nsfw}
/>
</picture>
@@ -40,27 +40,38 @@
scale-[1.2] opacity-50"
/>
<picture class="max-h-[inherit]">
<source
srcset={bestImageURL(post, false, 512)}
media="(max-width: 256px)"
/>
<source
srcset={bestImageURL(post, false, 1024)}
media="(max-width: 512px)"
/>
<source
srcset={bestImageURL(post, false, 1536)}
media="(max-width: 1024px)"
/>
{#each ['webp'] as format}
<source
srcset="{bestImageURL(
post,
false,
512,
format as 'avif' | 'webp',
)} 512w, {bestImageURL(
post,
false,
768,
format as 'avif' | 'webp',
)} 768w, {bestImageURL(
post,
false,
1024,
format as 'avif' | 'webp',
)} 1024w"
media="(min-width: 0px)"
type="image/{format}"
/>
{/each}
<img
src={bestImageURL(post, false, -1)}
src={bestImageURL(post, false, -1, null)}
loading="lazy"
class="max-h-[inherit] max-w-full h-auto z-30
transition-opacity duration-500 object-contain mx-auto
{imageLoaded === false ? 'opacity-0' : 'opacity-100'}"
class={[
'max-h-[inherit] max-w-full h-auto z-30 transition-opacity duration-500 object-contain mx-auto',
imageLoaded === false ? 'opacity-0' : 'opacity-100',
blur && 'blur-3xl',
]}
width={512}
height={300}
class:blur-[64px]={blur}
alt={post.alt_text ?? ''}
onload={() => (imageLoaded = true)}
/>
@@ -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}
/>
@@ -1,7 +1,6 @@
<script lang="ts">
import {
bestImageURL,
optimizeImageURL,
postLink,
type MediaType,
} from '$lib/components/lemmy/post/helpers.js'
@@ -70,10 +69,34 @@
class="cursor-pointer"
>
{#if post.thumbnail_url || isImage(post.url)}
{@const thumbnail = post.thumbnail_url != undefined && !isImage(post.url)}
<div class="relative overflow-hidden rounded-xl">
<picture>
<!--I would add AVIF, but lemmy.world's AVIF is broken as of currently-->
{#each ['webp'] as format}
<source
srcset="{bestImageURL(
post,
thumbnail,
128,
format as 'avif' | 'webp',
)} 1x, {bestImageURL(
post,
thumbnail,
256,
format as 'avif' | 'webp',
)} 2x, {bestImageURL(
post,
thumbnail,
512,
format as 'avif' | 'webp',
)} 3x"
media="(min-width: 0px)"
type="image/{format}"
/>
{/each}
<img
src={optimizeImageURL(post.thumbnail_url || post.url || '', 256)}
src={bestImageURL(post, thumbnail, -1, null)}
loading="lazy"
class={[
'object-cover relative overflow-hidden bg-slate-100 dark:bg-zinc-800 rounded-xl transition-colors',