Skip to content

Commit

Permalink
feat(webp): Making webp works for story mode
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jul 31, 2022
1 parent 8afa4a0 commit e6f8dd1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
11 changes: 8 additions & 3 deletions cmd/fibr/templates/async-image.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,19 @@

try {
await isWebPCompatible();
window.dispatchEvent(new Event('thumbnail-done'));
} catch (e) {
resolveScript(
await resolveScript(
'https://unpkg.com/[email protected]/dist-cjs/webp-hero.bundle.js',
'sha512-DA6h9H5Sqn55/uVn4JI4aSPFnAWoCQYYDXUnvjOAMNVx11///hX4QaFbQt5yWsrIm9hSI5fLJYfRWt3KXneSXQ==',
'anonymous',
).then(() => new webpHero.WebpMachine().polyfillDocument());
);

const webpMachine = new webpHero.WebpMachine();
webpMachine.polyfillDocument();
webpMachine.clearCache();
}

window.dispatchEvent(new Event('thumbnail-done'));
},
false,
);
Expand Down
35 changes: 24 additions & 11 deletions cmd/fibr/templates/story.html
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,30 @@
});

window.addEventListener('thumbnail-done', () => {
const lazyImageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) {
return;
}

const lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
lazyImageObserver.unobserve(lazyImage);
});
});
const lazyImageObserver = new IntersectionObserver(
async (entries, observer) => {
entries.forEach(async (entry) => {
if (!entry.isIntersecting) {
return;
}

const lazyImage = entry.target;
if (window.webpHero) {
const content = await fetch(lazyImage.dataset.src, {
credentials: 'same-origin',
}).arrayBuffer();

const webpMachine = new webpHero.WebpMachine();
lazyImage.src = await webpMachine.decode(new Uint8Array(content));
webpMachine.clearCache();
} else {
lazyImage.src = lazyImage.dataset.src;
}

lazyImageObserver.unobserve(lazyImage);
});
},
);

document
.querySelectorAll('img.thumbnail')
Expand Down

0 comments on commit e6f8dd1

Please sign in to comment.