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.

Not working with only srcset

See original GitHub issue

Hi there

I have a scenario where I have to use only srcset for images. The problem is, in this way it does not wait for the images to load and run the callback function. I did change the following line from the . js file as follows:

`// original code from js file this.proxyImage.src = this.img.src;

// I changed it as for srcset workout this.proxyImage.src = this.img.srcset;`

In this way it works like I want but it is making the double request for each image and throwing the error for 2nd img request.

How this can be fixed.

Regards, Shehzad Asif

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ashehzadgeccommented, Jul 10, 2021

Thanks for the reply, I’ll check it (will take some time as now working of some project). If not worked in my situation, will get back to you.

On Sat, Jul 10, 2021 at 4:25 AM trevDev(); @.***> wrote:

Here’s a set of functions I wrote to handle this myself. If anyone else might benefit from this go ahead:

function determineImageSource(elm) { if (elm.src) return elm.src; if (elm.currentSrc) return elm.currentSrc; if (elm.style.backgroundImage) { const pattern = /\burl\s*(\s*[“']?([^”‘\r\n)(]+)["’]?\s*)/ match = elm.style.backgroundImage.match(pattern); try { return match[1] } catch { return null } } return null; }

function isImageLoaded(elm) { return new Promise(function(resolve, reject) { var src = determineImageSource(elm); if (src === null) return resolve(); var img = new Image();

    img.onload = function() {
      resolve(img);
    };
    img.onerror = function() {
      resolve(img);
    };
    img.src = src;
})

}

function afterImagesLoad(parent, callback) { var container = document.querySelector(parent); if (container === null) throw Error(‘No parent for allImagesLoaded’);

var imgElms = Array.from(container.querySelectorAll('img'));
var bgImages = Array.from(
  container.querySelectorAll('[style*="background-image"]')
);

var imgs = imgElms.concat(bgImages);
var promises = imgs.map(isImageLoaded);

Promise.all(promises).then(callback)

}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/desandro/imagesloaded/issues/299#issuecomment-877507041, or unsubscribe https://github.com/notifications/unsubscribe-auth/APQYRBXS4VSB7JHCKYTIHW3TW6AQLANCNFSM44ZPVL6A .

0reactions
ashehzadgeccommented, Feb 23, 2022

Thanks for listening to us.

On Mon, Feb 21, 2022 at 10:53 PM David DeSandro @.***> wrote:

imagesLoaded v5 has been released with better support for srcset. Please upgrade and report back if you still run into issues.

— Reply to this email directly, view it on GitHub https://github.com/desandro/imagesloaded/issues/299#issuecomment-1047115983, or unsubscribe https://github.com/notifications/unsubscribe-auth/APQYRBR7WCZXNCBJFVKWZ6TU4J3ZPANCNFSM44ZPVL6A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

responsive images srcset not working - Stack Overflow
Have you tried to change DPR to 1.0? You can find it in the top bar of Chrome DevTools in responsive mode (try...
Read more >
srcset not working? Getting Wrong Images? Let's Find Out Why!
Sometimes, when srcset is not working and the browser is using the wrong image, it can be frustrating. This guide will help you...
Read more >
Srcset not working in Chrome (but in Firefox and Safari)
I have setup a srcset rule which works fine in Firefox and Safari, but Chrome always downloads the original file. I noticed that...
Read more >
picture srcset property is not working properly? - WordPress.org
it has to give the right image for 2k,4k,8k screen with min width of 1200px. it is showing only the fallback image. what...
Read more >
srcset not working: always loads desktop version : r/HTML
if I'm on mobile, it doesn't load the hero-mobile.jpg. Fallback is never loaded: desktop version is always loaded. What's the problem? I tried ......
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