Skip to content

Commit

Permalink
Merge pull request #125 from whitep4nth3r/optimise-twitch-thumb-on-ho…
Browse files Browse the repository at this point in the history
…mepage

Use Netlify image optimisation to optimise homepage Twitch thumbnail
  • Loading branch information
whitep4nth3r authored Mar 5, 2024
2 parents e7cb231 + 56e4802 commit 03e6449
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 17 deletions.
5 changes: 4 additions & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@
[[redirects]]
from = "/newsletter"
to = "https://buttondown.email/weirdwidewebhole"
status = 301
status = 301

[images]
remote_images = ["https://static-cdn.jtvnw.net/cf_vods/.*"]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mk2-p4nth3rblog",
"version": "1.0.3",
"version": "1.0.4",
"description": "My website and blog.",
"scripts": {
"watch:sass": "sass --watch ./src/_sass:src/_css",
Expand Down
30 changes: 23 additions & 7 deletions src/_components/responsiveImage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function ResponsiveImage({ image }) {
// Now transforms images from Contentful and third party images from Twitch using /.netlify/images/?url=

function ResponsiveImage({ image, classOverride = null, loading = null }) {
const className = classOverride ? classOverride : "post__responsiveImage";
const loadingStrat = loading ? loading : "lazy";
const { contentType } = image;
// Inspect contentType to convert GIF to WebP and not AVIF
// more info: https://twitter.com/whitep4nth3r/status/1460244790059188226
Expand All @@ -14,31 +18,43 @@ function ResponsiveImage({ image }) {
const maxContainerSize = 600;

// Note, this could be further optimised by considering padding inside the container
const sizes = `(max-width: ${maxContainerSize - 1}px) 100vw, ${maxContainerSize}px`;
const sizes = `(max-width: ${
maxContainerSize - 1
}px) 100vw, ${maxContainerSize}px`;

function makeSrcSetArray(format) {
const formatString = format === undefined ? "" : `&fm=${format}`;

return imageWidths.map((width) => `${image.url}?q=75&w=${width}${formatString} ${width}w`);
return imageWidths.map(
(width) => `${image.url}?q=75&w=${width}${formatString} ${width}w`,
);
}

function makeSrcSetString(format) {
return makeSrcSetArray(format).join(", ");
}

return /* html */ `<picture>
${!isGif ? `<source type="image/avif" srcSet="${makeSrcSetString("avif")}" sizes="${sizes}" />` : ""}
<source type="image/webp" srcSet="${makeSrcSetString("webp")}" sizes="${sizes}" />
${
!isGif
? `<source type="image/avif" srcSet="${makeSrcSetString(
"avif",
)}" sizes="${sizes}" />`
: ""
}
<source type="image/webp" srcSet="${makeSrcSetString(
"webp",
)}" sizes="${sizes}" />
<img
srcSet="${makeSrcSetString()}"
sizes="${sizes}"
src="${image.url}"
alt="${image.description}"
loading="lazy"
loading="${loadingStrat}"
decoding="async"
width="${image.width}"
height="${image.height}"
class="post__responsiveImage"
class="${className}"
/>
</picture>`;
}
Expand Down
20 changes: 13 additions & 7 deletions src/_components/twitchInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const PlayIcon = require("./svg/playIcon");
const ResponsiveImage = require("./responsiveImage");

function TwitchInfo({ isLive, vodData }) {
return /* html */ `
Expand All @@ -16,13 +17,18 @@ function TwitchInfo({ isLive, vodData }) {
${vodData.subtitle} ${PlayIcon()}
</p>
</div>
<img
src="${vodData.thumbnail.url}"
alt="Stream screenshot. It's auto-generated so I can't give you any details, sorry!"
class="twitchInfo__thumbnail"
height="${vodData.thumbnail.height}"
width="${vodData.thumbnail.width}"
/>
${ResponsiveImage({
image: {
url: `/.netlify/images/?url=${vodData.thumbnail.url}`,
height: vodData.thumbnail.height,
width: vodData.thumbnail.width,
contentType: "image/jpeg",
description:
"Stream screenshot. It's auto-generated so I can't give you any details, sorry!",
},
classOverride: "twitchInfo__thumbnail",
loading: "eager",
})}
</a>
</div>`
: /* html */ `<div id="twitch-embed" class="twitchInfo__embed"></div>
Expand Down
5 changes: 5 additions & 0 deletions src/_css/main.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/_css/main.css.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/_sass/_twitch-info.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
display: block;
height: 100%;
width: 100%;

picture {
width: 100%;
height: 100%;
z-index: 0;
}
}

.twitchInfo__link {
Expand Down

0 comments on commit 03e6449

Please sign in to comment.