Shares 18

How to load Facebook button asynchronously?

Almost every blog, a website has Facebook like, share or follow button. As you may know default Facebook social share code is loaded synchronously along with your web page resources. This will increase your website load time and damage SEO score. It’s just not SEO but also spoil user experience.

I am sure Facebook social share button is important but not as your actual code to display first. There are many blog or website has higher page load time because of default Facebook code.

Using Facebook code asynchronously will help you to load your web page 0.5 – 1.5 seconds faster. This is what you have to load Facebook like, share or follow button faster.

Here is the code I got from Facebook Developer for like and share to geekflare.com

<div id="fb-root"></div>
<script>(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#xfbml=1&appId=xxxxxxxxxx&version=v2.0";    
fjs.parentNode.insertBefore(js, fjs);  
}(document, 'script', 'facebook-jssdk'));</script>

Now, all I have to add is following the single line in my code, which will help to load buttons faster.

js.async=true;

So modified code would be:

<div id="fb-root"></div>
<script>(function(d, s, id) {    
var js, fjs = d.getElementsByTagName(s)[0];    
if (d.getElementById(id)) return;    
js = d.createElement(s); js.id = id;  
js.async=true;    
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&appId=xxxxxxxxxx&version=v2.0";    
fjs.parentNode.insertBefore(js, fjs);  
}(document, 'script', 'facebook-jssdk'));</script>

Isn’t it easy?

Alternatively, if you are using WordPress then can use a plugin like Social Warfare which is very lightweight and doesn’t  slow down your website.

I am sure you like your web page to load faster and I hope this helps you.

Shares 18

Reader Interactions

Comments

  1. Awesome work mate ! Facebook plugins for websites are a pain in the arse when it comes to load speeds. Thanks to your simple tweak, hopefully things will giddy up . 🙂

Comments

Your email address will not be published. Required fields are marked *