Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image request blocking FB.getLoginStatus

Facebook's Javascript SDK has a method called getLoginStatus that stalls (and never fires the callback passed into it) while an image request on the page also stalls (i.e. the browser doesn't receive a 200 or 404 for a very long time.)

If you wait an extremely long time and the browser (?) finally closes out the attempt to fetch the image, the SDK continues on its merry way.

What might be going on, and is there a way to prevent it? It's awfully inconvenient when a user can't sign in or sign up just because of an image request.

like image 407
John O'Neill Avatar asked Oct 21 '22 05:10

John O'Neill


2 Answers

Blocking (HTML):

<img src="..." />

Non-Blocking (with CSS):

#someDiv {
    background-image: url(...) no-repeat;
    width: xxx;
    height: xxx;
}

Non-Blocking (with JS):

var img = new Image();
img.onload = function () {
    document.getElementById('someDiv').appendChild(img);
};
img.src = "...";

Try with solution number 2 or 3 - there are also many preloader plugins for JavaScripts making it easier for you to load a lot of images asynchronously, for example: http://thinkpixellab.com/pxloader/

Another solution would be to load smaller images first and load the hires ones asynchronously.

like image 183
andyrandy Avatar answered Nov 01 '22 17:11

andyrandy


When you use the initialization code from the Facebook SDK website, by default it wants to wait for the page to be fully loaded be for running certain events, like the fbAsyncInit function.

I'm not sure of an "officially supported" way to bypass this, but you could load the Javascript source yourself and call the routines outright (i.e. not in the async wrapper).

This is a barebones example that stalled like you mentioned using the Facebook SDK initialization procedure but works fine with this workaround.

<html>
<head>
    <title>This is a test</title>
    <script src="http://connect.facebook.net/fr_FR/sdk.js"></script>
    <script language="javascript">
    <!--
    var loggedIn = false;
    var authenticated = false;

    FB.init({
        appId      : '{your app ID here}',
        xfbml      : true,
        version    : 'v2.0'
    });

    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            // the user is logged in and has authenticated your
            // app, and response.authResponse supplies
            // the user's ID, a valid access token, a signed
            // request, and the time the access token 
            // and signed request each expire
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            loggedIn = true;
            authenticated = true;
        } else if (response.status === 'not_authorized') {
            // the user is logged in to Facebook, 
            // but has not authenticated your app
            loggedIn = true;
        } else {
            // the user isn't logged in to Facebook.
        }
    });

    function testLogin()
    {
        alert("Logged in: " + loggedIn + "\nAuthenticated: " + authenticated);
    }
    // -->
    </script>
</head>
<body>
Testing!
<button onclick="testLogin()">Test login?</button>
<img src="http://deelay.me/5000/ http://example.com/image.gif">
</body>
</html>

I'm not sure how this will affect integration with your site, but I can't imagine it would be a problem. If anything I suppose it's worth a shot!

like image 29
Anthony Avatar answered Nov 01 '22 17:11

Anthony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!