bbc / Imager.js

Responsive images while we wait for srcset to finish cooking
Apache License 2.0
3.84k stars 224 forks source link

Firefox 29 requests `undefined` URL in case of lazy loading #98

Closed tokkonopapa closed 9 years ago

tokkonopapa commented 10 years ago

When lazyload is set to true, Firefox 29 fails to get naturalWidth and requests undefined URL from fallback function for IE8. Please check http://jsfiddle.net/tokkonoPapa/ms3c6/ or http://contentloaded.com/responsive/bbc-news/ .

thom4parisot commented 10 years ago

Hello @tokkonopapa,

do you have any insight on how to fix this issue?

Thank you :-)

tokkonopapa commented 10 years ago

Hi @oncletom, For example, the following code suppresses unnecessary HTTP request.

    getNaturalWidth = (function(){
        if (Object.prototype.hasOwnProperty.call(document.createElement('img'), 'naturalWidth')) {
            return function (image){ return image.naturalWidth;};
        }
        // IE8 and below lacks the naturalWidth property
        return function (source){
            var img = document.createElement('img');
            img.src = source.src || "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=";
            return img.width;
        };
    })();

When source.src is undefined, the object source is HTMLDivElement. I don't know why this happens.

thom4parisot commented 10 years ago

It means the naturalWidth is somewhat performed while the element is not yet replaced by an image in the DOM.

I have seen such a similar error from time to time in the CI tests so I wonder if there is a ran-DOM race condition which would trigger your issue.

tokkonopapa commented 10 years ago

Thank you for your information.

In my experience, this can be found only in Firefox. So it seems not this project concern.

Regardless of any reason, waste of HTTP request is not good for us. So I left the above temporal code for who are intersted in this topic. And I will entrust to you that this issue should be closed or still opened.

Thanks again.

thom4parisot commented 9 years ago

104 now creates placeholders when lazyload is on. So a naturalWidth should always be available.

Let me know if you still encounter the issue when 0.3.1 is out.

tokkonopapa commented 9 years ago

0.3.1 is fine about this issue!

thom4parisot commented 9 years ago

Thanks for your feedback @tokkonopapa :+1: