Skip to content

Commit

Permalink
poc: image optimazer
Browse files Browse the repository at this point in the history
  • Loading branch information
ocknamo committed Jun 6, 2024
1 parent 3e30368 commit 1395c6f
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/routes/blog/[slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
<script lang="ts">
import SvelteMarkdown from 'svelte-markdown';
import { browser } from '$app/environment';
export let data;
// お手製のfailbackのPoC
// 雑な実装なので作り込む必要がありそう
// widthなどの値も渡したい
const optimazerPrefix =
'https://nostr-image-optimizer.ocknamo.com/image/width=1600,quality=50,format=';
let src = {
img: `${optimazerPrefix}webp/${data.blog.image}`,
webp: [`${optimazerPrefix}webp/${data.blog.image}`],
jpeg: [`${optimazerPrefix}jpeg/${data.blog.image}`]
};
const handleImgError = (e: Event) => {
if (e.type !== 'error') {
return;
}
// failback
src = {
img: data.blog.image,
webp: [data.blog.image],
jpeg: [data.blog.image]
};
};
</script>

<hgroup>
{#if !!data.blog.image}
<img class="image" src={data.blog.image} alt="thumbnail" />
{/if}
<div class="image-wrapper">
{#if !!data.blog.image && browser}
<picture>
<source srcset={src.jpeg[0]} type="image/jpeg" />
<source srcset={src.webp[0]} type="image/webp" />
<img src={src.img} alt="my top" on:error={handleImgError} loading="lazy" />
</picture>
{/if}
</div>
<h1>{data.blog.title}</h1>
<p class="naddress">
naddress: <a
Expand Down Expand Up @@ -48,6 +80,10 @@
max-width: 800px;
}
hgroup .image-wrapper {
height: 320px;
}
hgroup img {
min-width: 100%;
height: 320px;
Expand Down

0 comments on commit 1395c6f

Please sign in to comment.