impr: optimize image delivery
This commit is contained in:
5 files changed
+82
-50
No files matched your search
@@ -7,17 +7,16 @@ import type { CommentView, PersonView, Post } from 'lemmy-js-client'
|
|||||||
export const isCommentMutable = (comment: CommentView, me: PersonView) =>
|
export const isCommentMutable = (comment: CommentView, me: PersonView) =>
|
||||||
me.person.id == comment.creator.id
|
me.person.id == comment.creator.id
|
||||||
|
|
||||||
|
// Algorithm to determine the best image URL to use
|
||||||
export const bestImageURL = (
|
export const bestImageURL = (
|
||||||
post: Post,
|
post: Post,
|
||||||
compact: boolean = true,
|
thumbnail: boolean = true,
|
||||||
width: number = 1024,
|
width: number = 1024,
|
||||||
|
format: 'avif' | 'webp' | null = 'avif',
|
||||||
) => {
|
) => {
|
||||||
if (compact) {
|
if (post.thumbnail_url && (thumbnail || !post.url))
|
||||||
/* empty */
|
return optimizeImageURL(post.thumbnail_url, width, format)
|
||||||
}
|
else if (post.url) return optimizeImageURL(post.url, width, format)
|
||||||
if (post.url) return optimizeImageURL(post.url, width)
|
|
||||||
else if (post.thumbnail_url)
|
|
||||||
return optimizeImageURL(post.thumbnail_url, width)
|
|
||||||
|
|
||||||
return post.url ?? ''
|
return post.url ?? ''
|
||||||
}
|
}
|
||||||
@@ -25,11 +24,12 @@ export const bestImageURL = (
|
|||||||
export const optimizeImageURL = (
|
export const optimizeImageURL = (
|
||||||
urlStr: string,
|
urlStr: string,
|
||||||
width: number = 1024,
|
width: number = 1024,
|
||||||
|
format: 'avif' | 'webp' | null = 'avif',
|
||||||
): string => {
|
): string => {
|
||||||
try {
|
try {
|
||||||
const url = new URL(urlStr)
|
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')) {
|
if (width > 0 && !url.searchParams.has('thumbnail')) {
|
||||||
url.searchParams.set(
|
url.searchParams.set(
|
||||||
@@ -41,10 +41,6 @@ export const optimizeImageURL = (
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (width == -1) {
|
|
||||||
url.searchParams.delete('thumbnail')
|
|
||||||
}
|
|
||||||
|
|
||||||
return url.toString()
|
return url.toString()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
|
|||||||
@@ -30,11 +30,11 @@
|
|||||||
{#if embed_title && view == 'cozy'}
|
{#if embed_title && view == 'cozy'}
|
||||||
<a target="_blank" href={url} class="group/link">
|
<a target="_blank" href={url} class="group/link">
|
||||||
<Material
|
<Material
|
||||||
color="distinct"
|
color="uniform"
|
||||||
class="flex flex-col-reverse sm:flex-row
|
class={[
|
||||||
overflow-hidden gap-4 embed-card z-0 relative
|
'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',
|
||||||
dark:bg-zinc-950 group-hover/link:bg-slate-50 dark:group-hover/link:bg-zinc-900 transition-colors"
|
]}
|
||||||
rounding="2xl"
|
rounding="2xl"
|
||||||
element="article"
|
element="article"
|
||||||
>
|
>
|
||||||
@@ -60,24 +60,27 @@
|
|||||||
<picture
|
<picture
|
||||||
class="-m-4 mb-auto sm:-mb-4 sm:ml-auto shrink-0 sm:w-1/3 sm:max-w-60"
|
class="-m-4 mb-auto sm:-mb-4 sm:ml-auto shrink-0 sm:w-1/3 sm:max-w-60"
|
||||||
>
|
>
|
||||||
<source
|
{#each ['webp'] as format}
|
||||||
srcset={optimizeImageURL(thumbnail_url, 256)}
|
<source
|
||||||
media="(max-width: 256px)"
|
srcset="{optimizeImageURL(
|
||||||
/>
|
thumbnail_url,
|
||||||
<source
|
256,
|
||||||
srcset={optimizeImageURL(thumbnail_url, 512)}
|
format as 'avif' | 'webp',
|
||||||
media="(max-width: 512px)"
|
)} 256w, {optimizeImageURL(
|
||||||
/>
|
thumbnail_url,
|
||||||
<source
|
512,
|
||||||
srcset={optimizeImageURL(thumbnail_url, 1024)}
|
format as 'avif' | 'webp',
|
||||||
media="(max-width: 1024px)"
|
)} 512w"
|
||||||
/>
|
media="(min-width: 0px)"
|
||||||
|
type="image/{format}"
|
||||||
|
/>
|
||||||
|
{/each}
|
||||||
<img
|
<img
|
||||||
src={optimizeImageURL(thumbnail_url, -1)}
|
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"
|
class="object-cover w-full sm:max-w-96 h-32 sm:min-h-16 bg-slate-200 dark:bg-zinc-800"
|
||||||
width={600}
|
width={600}
|
||||||
height={400}
|
height={400}
|
||||||
alt=" "
|
alt=""
|
||||||
class:blur-3xl={nsfw}
|
class:blur-3xl={nsfw}
|
||||||
/>
|
/>
|
||||||
</picture>
|
</picture>
|
||||||
|
|||||||
@@ -40,27 +40,38 @@
|
|||||||
scale-[1.2] opacity-50"
|
scale-[1.2] opacity-50"
|
||||||
/>
|
/>
|
||||||
<picture class="max-h-[inherit]">
|
<picture class="max-h-[inherit]">
|
||||||
<source
|
{#each ['webp'] as format}
|
||||||
srcset={bestImageURL(post, false, 512)}
|
<source
|
||||||
media="(max-width: 256px)"
|
srcset="{bestImageURL(
|
||||||
/>
|
post,
|
||||||
<source
|
false,
|
||||||
srcset={bestImageURL(post, false, 1024)}
|
512,
|
||||||
media="(max-width: 512px)"
|
format as 'avif' | 'webp',
|
||||||
/>
|
)} 512w, {bestImageURL(
|
||||||
<source
|
post,
|
||||||
srcset={bestImageURL(post, false, 1536)}
|
false,
|
||||||
media="(max-width: 1024px)"
|
768,
|
||||||
/>
|
format as 'avif' | 'webp',
|
||||||
|
)} 768w, {bestImageURL(
|
||||||
|
post,
|
||||||
|
false,
|
||||||
|
1024,
|
||||||
|
format as 'avif' | 'webp',
|
||||||
|
)} 1024w"
|
||||||
|
media="(min-width: 0px)"
|
||||||
|
type="image/{format}"
|
||||||
|
/>
|
||||||
|
{/each}
|
||||||
<img
|
<img
|
||||||
src={bestImageURL(post, false, -1)}
|
src={bestImageURL(post, false, -1, null)}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
class="max-h-[inherit] max-w-full h-auto z-30
|
class={[
|
||||||
transition-opacity duration-500 object-contain mx-auto
|
'max-h-[inherit] max-w-full h-auto z-30 transition-opacity duration-500 object-contain mx-auto',
|
||||||
{imageLoaded === false ? 'opacity-0' : 'opacity-100'}"
|
imageLoaded === false ? 'opacity-0' : 'opacity-100',
|
||||||
|
blur && 'blur-3xl',
|
||||||
|
]}
|
||||||
width={512}
|
width={512}
|
||||||
height={300}
|
height={300}
|
||||||
class:blur-[64px]={blur}
|
|
||||||
alt={post.alt_text ?? ''}
|
alt={post.alt_text ?? ''}
|
||||||
onload={() => (imageLoaded = true)}
|
onload={() => (imageLoaded = true)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -46,7 +46,6 @@
|
|||||||
url={post.url}
|
url={post.url}
|
||||||
thumbnail_url={post.thumbnail_url}
|
thumbnail_url={post.thumbnail_url}
|
||||||
nsfw={post.nsfw}
|
nsfw={post.nsfw}
|
||||||
embed_description={post.embed_description}
|
|
||||||
embed_title={post.embed_title}
|
embed_title={post.embed_title}
|
||||||
{view}
|
{view}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {
|
import {
|
||||||
bestImageURL,
|
bestImageURL,
|
||||||
optimizeImageURL,
|
|
||||||
postLink,
|
postLink,
|
||||||
type MediaType,
|
type MediaType,
|
||||||
} from '$lib/components/lemmy/post/helpers.js'
|
} from '$lib/components/lemmy/post/helpers.js'
|
||||||
@@ -70,10 +69,34 @@
|
|||||||
class="cursor-pointer"
|
class="cursor-pointer"
|
||||||
>
|
>
|
||||||
{#if post.thumbnail_url || isImage(post.url)}
|
{#if post.thumbnail_url || isImage(post.url)}
|
||||||
|
{@const thumbnail = post.thumbnail_url != undefined && !isImage(post.url)}
|
||||||
<div class="relative overflow-hidden rounded-xl">
|
<div class="relative overflow-hidden rounded-xl">
|
||||||
<picture>
|
<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
|
<img
|
||||||
src={optimizeImageURL(post.thumbnail_url || post.url || '', 256)}
|
src={bestImageURL(post, thumbnail, -1, null)}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
class={[
|
class={[
|
||||||
'object-cover relative overflow-hidden bg-slate-100 dark:bg-zinc-800 rounded-xl transition-colors',
|
'object-cover relative overflow-hidden bg-slate-100 dark:bg-zinc-800 rounded-xl transition-colors',
|
||||||
|
|||||||
Reference in new issue
Block a user