Daniel López Azaña

Theme

Social Media

Blog

GNU/Linux, Open Source, Cloud Computing, DevOps and more...

Speed up your website with a SUPERLIGHT Facebook «Like» button

facebook-like-buttonIt is often common to embed a Facebook «Like» button on your website so that on the one hand you show the number of followers of your Facebook page and on the other hand you invite the user to click on it to start following your page. However, this type of buttons tend to overload a website quite a lot because they consist of Javascript code that dynamically generates the button with the updated number of followers and the functionality needed to give a «Like». This means that every time you load a page of your website you have to send 11 extra requests to the Facebook servers to download all the necessary elements. Given that these servers are currently located on the west coast of the United States and are not available through any CDN or similar service, depending on where the user is located, it is likely that each of these requests will have to cross half the globe to complete the download. All this causes your website to slow down unnecessarily and its loading speed is affected , which is quite negative in multiple aspects.

What if you could replace the button provided by Facebook with a single and simple image of the same button showing up-to-date number of followers of your Facebook page? It would be fantastic for website performance optimization or WPO , because it would only require a single additional request to the server , and it could be served and cached quickly from a CDN very close to the user’s geographic location. In this way you could ensure that the loading speed of your website would remain very high and without performance penalties.

How can this be achieved? It seems a win-win solution too good to be true… Well, it can be achieved in a much simpler way than you think thanks to the Imagemagick library and a simple Bash script. Read on…

1.- Download source images with empty number of followers:

These are the source images you can use to generate other equivalent images, but with the number of followers filled in:

facebook-btn-transparent-9999

There are 2 images because one will be used when the number of followers is equal to or less than 9,999, and another when the page has up to 99,999 followers.

The images that the script will generate as a result will be similar to these:

facebook-btn-filledfacebook-btn-final

2.- Get an access token for your Facebook page

In addition to these images, it will be necessary to obtain an access token to our Facebook page. In the following link I explain in more detail how to do it:

Recommended read:
How to get a permanent token to access a Facebook page

3.- Run this «Like» image generation script to get the number of followers filled in:

#! /bin/bash 
 
# Directory where the images will be stored on your web site
imgDir="/var/www/vhosts/example.com/httpdocs/images/facebook-like-button"
 
# Token de acceso página de Facebook
accessToken="EAAUAA6XzH3sBAByxmeRXoivAX8DamdhghHUNWEYBxdXnQZAZAUPbDpBrmS3PexVeka2JTL9Vxn8eQe6wBLNlPbsN06nBPhxQhp0TKNrdZALXIEHgVANOb4U2eIz3BRHqTCAjSoFxqVZBWjc9K3qHyyRcGczccU7NRWjj5OkzxbAZDZD" 
 
# Facebook page access token
likesCount=`curl -X GET  "https://graph.facebook.com/v2.8/4x4proyect?fields=fan_count&access_token=${accessToken}" | cut -f 2 -d ':' | cut -f 1 -d ','` 
 
# We make sure that the value obtained is a number
re='^[0-9]+$' 
if ! [[ $likesCount =~ $re ]] ; then 
   echo "error: Not a number" >&2; exit 1 
fi 
 
if [[ $likesCount -ge 0 ]] && [[ $likesCount -le 9999 ]];then 
    maxCount=9999 
elif [[ $likesCount -ge 10000 ]] && [[ $likesCount -le 99999 ]];then 
    maxCount=99999 
else 
    echo "error: Number not between 0 and 99999" >&2; exit 1 
fi 
 
# Format the number of likes with thousands separator
likesCount=`echo $likesCount | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta'` 
 
# Generate the Facebook image with the number of likes.
/usr/bin/convert -gravity East -weight 400 -pointsize 55 -fill white -annotate +20+5 "$likesCount" "$imgDir"/facebook-btn-transparent-"${maxCount}".png "$imgDir"/facebook-btn-filled.png 
/usr/bin/convert -resize 110x -quality 100 "$imgDir"/facebook-btn-filled.png "$imgDir"/facebook-btn-final.png 
 
# Clear the mod_pagespeed cache to display the new number of likes on the button.
# Replace with any other cache invalidation method you use, or comment out this line if you don't use any.
touch /var/cache/mod_pagespeed/cache.flush 
 
echo "Generated image with number of likes equal to $likesCount" 
 
exit 0

4.- Add a new cron job

In order for the above script to run periodically unattended add the following entry to cron:

# Update image of www.example.com facebook button with up-to-date number of likes
0       2       *       *       *       /bin/bash /root/scripts/update-example-com-fb-page-likes-count.sh
Daniel López Azaña

About the author

Daniel López Azaña

20+ Years ExperienceAWS & GCP CertifiedAI/LLM Specialist

Tech entrepreneur and cloud architect with over 20 years of experience transforming infrastructures and automating processes. Specialist in AI/LLM integration, Rust and Python development, and AWS & GCP architecture. Restless mind, idea generator, and passionate about technological innovation and AI.

Related articles

hot-button-ssh-command-widget-icon

Linux remote control from your smartphone via SSH button widgets

In this post I will tell you about an Android app that is extremely useful to run commands remotely on a Linux computer: Hot Button SSH Command Widget. This application allows you to launch conveniently any command you want on a remote computer through SSH only with the push of a button on the screen of your mobile phone or tablet. This not only will facilitate automation of repetitive tasks, but also is very interesting from the perspective of security for the same reasons I exposed in my Automatically lock/unlock your screen by Bluetooth device proximity post. It will allow you for example to lock and unlock the screen without having to type your password again and again in sight of other people.

July 15, 2017
Velocidad de carga de páginas web: una importante pieza del puzzle

5 most important reasons to increase your website’s loading speed

It’s common not to pay much attention to website loading speed as long as it remains between more or less acceptable levels wich don’t make us waiting forever for content. However, web speed is becoming an increasingly important factor and optimization efforts in this area can be decisive for success or failure in achieving our goals.Below are the top 5 reasons why you should start worrying about website speed and page loading times.

September 24, 2014
AWS security groups

How to automatically update all your AWS EC2 security groups when your dynamic IP changes

One of the biggest annoyances when working with AWS and your Internet connection has a dynamic IP is that when it changes, you immediately stop accessing to all servers and services protected by an EC2 security group whose rules only allow traffic to certain specific IP’s instead of allowing open connections to everyone (0.0.0.0.0/0).Certainly the simplest thing to do is always allowing traffic on a given port to everyone, so that even if you have a dynamic IP on your Internet connection you will always be able to continue accessing even if it changes. But opening traffic on a port to everyone is not the right way to proceed from a security point of view, because then any attacker will be able to access that port without restrictions, and that is not what you want.

January 12, 2021

Comments

Be the first to comment

Submit comment