Skip to content

Commit

Permalink
perf: ⚡️ serve avif imgs (category and product where possible) (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskaransarkaria authored Jan 5, 2024
1 parent f6a0917 commit 80f810c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { getImageFilename } from "$lib/utils/getImageFilename";
import type { Product } from "$lib/types/product";
import { isAvifSupported } from "$lib/stores/isAvifSupported";
export let product: Product;
export let productDescription: string;
Expand All @@ -26,15 +27,17 @@
const createImgArr = (product: Product): { src: string; alt: string }[] =>
product?.ProductImages?.length
? product.ProductImages.map((img, idx) => ({
src: `${PUBLIC_BUCKET_URL}/${getImageFilename(img.ImageUrl)}`,
src: `${PUBLIC_BUCKET_URL}/${getImageFilename(img.ImageUrl)}${
isAvifSupported ? "-avif" : ""
}`,
alt: `${product.Name} image ${idx + 1}`,
}))
: [
{
src: product?.ProductImages[0]?.ImageUrl
? `${PUBLIC_BUCKET_URL}/${getImageFilename(
product.ProductImages[0].ImageUrl
)}`
)}${isAvifSupported ? "-avif" : ""}`
: "/coming-soon.png",
alt: `${product.Name} image 1`,
},
Expand Down
32 changes: 21 additions & 11 deletions src/lib/components/Hex/Hex.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts">
import { PUBLIC_BUCKET_URL } from "$env/static/public";
import { fade } from "svelte/transition";
import { lazy } from "$lib/utils/lazyAction";
import type { Category } from "$lib/types/category";
import type { Tag } from "$lib/types/tag";
import { isAvifSupported } from "$lib/stores/isAvifSupported";
export let hexHref = "";
export let isEmpty = false;
Expand All @@ -17,6 +17,7 @@
Description: "",
};
export let loaded: Map<string, HTMLImageElement> = new Map();
let imgError = false;
</script>

<div class="hex-in">
Expand All @@ -36,16 +37,25 @@
href={hexHref}
>
{#key loaded}
<img
in:fade={{ duration: 700, delay: 200 }}
src="/grey_square.png"
use:lazy={{
src: `${PUBLIC_BUCKET_URL}/${category.Description}`,
loaded,
}}
alt={`category ${category.Name}`}
data-testid="cdn-img"
/>
{#if imgError}
<img
in:fade={{ duration: 700, delay: 200 }}
src="grey_square.png"
on:error={() => (imgError = true)}
alt={`category ${category.Name}`}
data-testid="cdn-img"
/>
{:else}
<img
in:fade={{ duration: 700, delay: 200 }}
src={`${PUBLIC_BUCKET_URL}/${category.Description}${
isAvifSupported ? "-avif" : ""
}`}
on:error={() => (imgError = true)}
alt={`category ${category.Name}`}
data-testid="cdn-img"
/>
{/if}
<img
in:fade={{ duration: 500 }}
src={`${PUBLIC_BUCKET_URL}/hex_${Math.floor(
Expand Down
5 changes: 2 additions & 3 deletions src/lib/components/ProductView/ProductView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import { groupBy } from "lodash-es";
import Banner from "$lib/components/Banner/Banner.svelte";
import SingleProduct from "$lib/components/SingleProduct/SingleProduct.svelte";
import { getImageFilename } from "$lib/utils/getImageFilename";
import type { Category } from "$lib/types/category";
import type { Product } from "$lib/types/product";
Expand Down Expand Up @@ -112,7 +111,7 @@
$: variantCategoryIds = variantCategories.map((cat) => cat.Id);
$: if (productArr.length) {
$: if (productArr?.length) {
variantArr = productArr
?.filter(({ VariantGroupId }) => !!VariantGroupId)
?.filter((prod: Product) => prod.SellOnWeb && !prod.IsArchived);
Expand Down Expand Up @@ -169,7 +168,7 @@
</script>

<svelte:window bind:outerWidth />
{#if sortedCollatedArray.length}
{#if sortedCollatedArray?.length}
<div class="container" data-testid="product-view-container">
<Banner hasProducts />
<div class="sort-container">
Expand Down
7 changes: 5 additions & 2 deletions src/lib/components/SingleProduct/SingleProduct.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { PUBLIC_BUCKET_URL } from "$env/static/public";
import ImageLoader from "$lib/components/Image/ImageLoader.svelte";
import DetailedSingleProduct from "$lib/components/DetailedSingleProduct/DetailedSingleProduct.svelte";
import { isAvifSupported } from "$lib/stores/isAvifSupported";
import type { Product } from "$lib/types/product";
import type { Category } from "$lib/types/category";
Expand Down Expand Up @@ -30,7 +31,9 @@
>
<div class="position-img">
<ImageLoader
src={`${PUBLIC_BUCKET_URL}/${variantCategory.Description}`}
src={`${PUBLIC_BUCKET_URL}/${variantCategory.Description}${
isAvifSupported ? "-avif" : ""
}`}
alt={`${variantCategory.Name}`}
/>
</div>
Expand Down Expand Up @@ -71,7 +74,7 @@
src={product?.ProductImages[0]?.ImageUrl
? `${PUBLIC_BUCKET_URL}/${getImageFilename(
product.ProductImages[0].ImageUrl
)}`
)}${isAvifSupported ? "-avif" : ""}`
: "/coming-soon.png"}
alt={`${product.Name}`}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/SingleProduct/SingleProduct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ describe("Given SingleProduct", () => {
expect(multiImages).toHaveLength(2);
expect(multiImages[0]).toHaveAttribute(
"src",
"https://storage.googleapis.com/enki-website/foobar-0"
"https://storage.googleapis.com/enki-website/foobar-0-avif"
);
expect(multiImages[1]).toHaveAttribute(
"src",
"https://storage.googleapis.com/enki-website/foobar-1"
"https://storage.googleapis.com/enki-website/foobar-1-avif"
);
expect(
screen.getByRole("button", { name: "Add to Basket" })
Expand Down
3 changes: 3 additions & 0 deletions src/lib/stores/isAvifSupported.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { writable } from "svelte/store";

export const isAvifSupported = writable<boolean>(null);
16 changes: 0 additions & 16 deletions src/lib/utils/lazyAction.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
import Header from "$lib/components/Header/Header.svelte";
import Footer from "$lib/components/Footer/Footer.svelte";
import BackToTop from "$lib/components/BackToTop/BackToTop.svelte";
import { isAvifSupported } from "$lib/stores/isAvifSupported";
import "../app.css";
import { onMount } from "svelte";
let checkAvif = false;
onMount(() => {
if ($isAvifSupported === null) {
checkAvif = true;
}
});
</script>

<svelte:head>
Expand Down Expand Up @@ -32,8 +42,25 @@
<BackToTop />
</div>
<Footer />
{#if checkAvif}
<img
class="avif-supported-check"
alt="not visible and is used to check if your browser supports the avif format"
src="data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A="
on:load={() => {
isAvifSupported.set(true);
}}
on:error={() => {
isAvifSupported.set(false);
}}
/>
{/if}

<style>
.avif-supported-check {
display: none;
}
.container {
min-height: 100vh;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 80f810c

Please sign in to comment.