Is your website slowing down after adding Facebook social features like the Like/Share button, comments, or Messenger chat? These features load external scripts that can hurt page speed and lower your Google PageSpeed Insights score. This guide will show you how to optimize them to maintain performance without losing functionality.
Fix a Slow Website: Facebook Optimization
Why Does Facebook Code Slow Down Your Website?
When you embed Facebook social widgets, they:
Load external JavaScript (sdk.js), blocking the page from rendering quickly.
Send multiple requests to Facebook’s servers, increasing load time.
Affect metrics like Largest Contentful Paint (LCP) and Interaction to Next Paint (INP), reducing user experience.
Instead of loading Facebook scripts immediately, we delay their execution until the user interacts with the page (e.g., scrolling). This significantly improves page speed.
Replace [YOUR_LINK] with your actual link, which can be integrated using PHP and SQL. For example, if you’re using WordPress, you can use the code below:
add_filter('the_content', 'add_fb_comment_after_content');
function add_fb_comment_after_content($content)
{
/** Add only after post */
if (is_singular('post')) {
?>
<div id="fb-root"></div>
/**
add code script optimized here */
<?php
$fb_comment_after = '<div class="fb-comments" data-href="'. get_permalink(get_the_ID()) . '" data-width="100%" data-numposts="5"></div>';
return $content . $fb_comment_after;
}
return $content;
}
Optimized Facebook Messenger Code (Lazy Loading)
<!-- FB Messenger Chat Code Optimization -->
<script>
window.addEventListener('load', function () {
var is_facebook_load = 0
var chatbox = document.getElementById('fb-customer-chat');
chatbox.setAttribute("page_id", "111009301424827");
chatbox.setAttribute("attribution", "biz_inbox");
window.addEventListener('scroll', function () {
if (is_facebook_load == 0) {
is_facebook_load = 1;
window.fbAsyncInit = function () {
FB.init({
xfbml: true,
version: 'v15.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 = 'https://connect.facebook.net/en_US/sdk/xfbml.customerchat.js';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
}, false);
});
</script>
Note: Please replace (page_id 111009301424827) with your Facebook link.
Results After Optimization
✅ Faster website loading times (up to 50% improvement on mobile) ✅ Improved Google PageSpeed Insights scores (20-30% increase) ✅ Better user experience without losing social media functionality ✅ Enhanced SEO performance by optimizing Core Web Vitals
By implementing lazy loading for Facebook social media features, you can maintain a fast website while still engaging users. According to Google’s Web Performance Guidelines, optimizing third-party scripts can significantly improve Core Web Vitals, boosting SEO rankings and user retention.
If you also use Google Analytics or AdSense, similar optimization techniques can be applied to further enhance performance. 🚀
Share your optimization results after applying the steps above!