question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Workaround/Idea: Preloading images with srcset attributes (chrome)

See original GitHub issue

Hi, I’m working on a mobile app and was searching for a solution to preload the “current” image when an image has the srcset attribute (for calculating it’s container height). I had many issues on chrome/win and resolved them by using the img property ‘currentSrc’.

Because there was no event, when currentSrc has been set by chrome (I was testing many timeout values), I choose following approach:

function waitForComputedSrcset (images, $dfd) {
    $dfd = $dfd || $.Deferred();

    var computed = 0,
        length = images.length;

    for (var i = 0; i < length; i++) {
        if (images[i].hasOwnProperty('currentSrc') && !! !images[i].currentSrc) {
            window.setTimeout(this.waitForComputedSrcset.bind(this, images, $dfd), 100);
            return $dfd;
        }
        computed++;
    }
    return (length === computed) ? $dfd.resolve(images) : $dfd;
}

When all images are “computed” by chrome I set all src properties of the resolved images to var length = images.length; for (var i = 0; i < length; i++) { if (images[i].hasOwnProperty(‘currentSrc’) && !! !images[i].currentSrc) { images.src = images[i].currentSrc; } }

I’m not sure if this is a solution to the specs, but so I’ve got it working and imagesloaded doesn’t load the wrong images initially. Imo/Hopefully only browsers, which are supporting the srcset attribute are setting img.currentSrc.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
hayatbiralemcommented, Nov 26, 2015

Hi,

I did not find a solution with imagesLoaded for my issue than I wrote a simple function with some jQuery improvemens. That works for me now without imagesLoaded library:

My Pen: http://codepen.io/hayatbiralem/pen/jbjLZe?editors=001 Original Pen: http://codepen.io/desandro/pen/bIFyl

var imagesLoaded = function(selector, onComplete, onProgress) {
  var $images = $(selector).find('img');
  var success = 0;
  var error = 0;
  var iteration = 0;
  var total = $images.length;
  var check = function(el, status) {
    iteration++;
    var data = {
      img: el,
      iteration: iteration,
      success: success,
      error: error,
      total: total,
      status: status
    };
    $.isFunction(onProgress) && onProgress(data);
    if (success + error === total) {
      $.isFunction(onComplete) && onComplete(data);
    }
  };
  $images.each(function() {
    this.onload = function() {
      success++;
      check(this, 'success');
    };
    this.onerror = function() {
      error++;
      check(this, 'error');
    };
  });
};
0reactions
cywtfcommented, Feb 18, 2015

The workaround from @rs3d is doing the trick now for me. It’s loading fine in Chrome 40 (PC)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preloading responsive images - web.dev
Starting in Chrome 73, the browser can preload the right variant of responsive images specified in srcset before it discovers the img tag!...
Read more >
How to preload responsive images with imagesizes and ...
Improve the loading of your responsive images by using link rel="preload" elements with the new attributes imagesrcset and imagesizes.
Read more >
imagesloaded - Bountysource
I'm working on a mobile app and was searching for a solution to preload the "current" image when an image has the srcset...
Read more >
responsive images srcset not working - Stack Overflow
Im using chrome 40 and all I get is the largest image, resizing my browser, clearing the cache does nothing. I read somewhere...
Read more >
srcset and imgsizes attributes on link rel=preload
The srcset and sizes attributes of images enable authors to define multiple image resources that fit multiple device resolutions / layouts. However, currently ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found