<%= javascript_include_tag 'application' %>

<% cache 'footer_social_js' do %>

  <%# Facebook SDK for javascript initialization %>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : <%= Rails.application.secrets.facebook_app_ID %>,
        version    : 'v2.0'
      });
    };

    (function(d, s, id){
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) {return;}
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
  </script>

  <%= javascript_include_tag '//platform.twitter.com/widgets.js' %>

<% end %>

<%= render 'layouts/noscript_warning' %>
<%= javascript_include_tag 'application' %>

<% cache 'footer_social_js' do %>

  <%# Facebook SDK for javascript asynchronous load and initialization %>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : <%= Rails.application.secrets.facebook_app_ID %>,
        version    : 'v2.0'
      });
    };

    (function(d, s, id){
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) {return;}
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
  </script>

  <%# Twitter widgets.js asynchronous load and initialization %>
  <script>window.twttr = (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0],
        t = window.twttr || {};
    if (d.getElementById(id)) return t;
    js = d.createElement(s);
    js.id = id;
    js.src = "https://platform.twitter.com/widgets.js";
    fjs.parentNode.insertBefore(js, fjs);

    t._e = [];
    t.ready = function(f) {
      t._e.push(f);
    };

    return t;
  }(document, "script", "twitter-wjs"));</script>

<% end %>

<%= render 'layouts/noscript_warning' %>
Load twitter javascript (widgets.js) asynchrously, instead of with a plain <script> tag.
Load twitter javascript (widgets.js) asynchrously, instead of with a plain <script> tag.

This means faster page load (widgets.js loads after the page is already loaded and usable) and, most importantly, it will not block page load for about 20 seconds until timeout in places where twitter is blocked, e.g. China.
