This is from a simplified testing scope. I have some WP site with Foogallery Pro being used on it. The client added a plethora of huge jpegs for a full screen presentation. Like 900 images, net worth 1 GiB of network load for a single page. Can't be talked out of it either.
For modern Browsers I figured I'd experiment with trying to fool the thing into using AVIF (88 MiB total rather than 1GiB) images I generated for the test. Only computed some 24 hours on a 16 core Xeon WS but alas.
Now for my test I siply figure I'd trigger the replacement by presence of a CSS class .useAVIF
on the sub page I'm testing this on.
I then want to know if the browser can display AVIF images in the first place and find out by JS:
var canUseAVIF=false;
var avif = new Image();
avif.src = "data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAIAAAACAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQ0MAAAAABNjb2xybmNseAACAAIAAYAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAACVtZGF0EgAKCBgANogQEAwgMg8f8D///8WfhwB8+ErK42A=";
avif.onload = function () {canUseAVIF=true;};
So far so good, later on when we have a DOM, I figured I'd just replace all the DOM elements that Foogallery uses with the AVIFs rather than the JPEGs. They simply all reside in the same folder, they all exist, too.
jQuery(document).ready(function( $ ){
canUseAVIF=($(".useAVIF").size()>0)&&canUseAVIF;
if (canUseAVIF)
{
$(".foogallery-container .fg-item-inner img").each(function(){
var avif=$(this).attr('data-src-fg');
avif=avif.replace(".jpg", ".avif");
$(this).attr('data-src-fg',avif);
avif=$(this).attr('src');
avif=avif.replace(".jpg", ".avif");
$(this).attr('src',avif);
});
$(".foogallery-container .fg-item-inner a").each(function(){
var avif=$(this).attr('href');
avif=avif.replace(".jpg", ".avif");
$(this).attr('href',avif);
});
}
...
Could be more elegant but it is a quick test, rember.
Now that does tweak the DOM like I thought but it does not work. Because the Foogalery specific scripts must have already parsed the content or there must be some other mechanism at play. When the images get loaded the src
becomes JPEG again.
Does anyone have insight or inspiration where those JPEG references may live in the JS datastructures, so that I could loop over them and replace them?