Get a Perfect Score of 100 on Google PageSpeed Insights

Recently, I was exploring on how to get a perfect score of 100 on Google PageSpeed Insights. I searched for days and did several trial and errors, finally I was able to get my static page to score 100. Bellow are some tips on how I was able to do it.
Checkout Demo site   Run Demo site on PageSpeed Insights

  • Use CDN – This allow users from a around the world to download static content from a closer source instead of spanning the Atlantic ocean to retrieve data.
  • Compress your JS, CSS, Images and HTML. The more compressed you get your scripts / images, the faster the download will be.
  • Get a faster hosting. Google also rank sites base on the amount of time spent to deliver your content to readers. According to Google, users tend to leave sites that take more than 3 seconds to load. So it’s important to to pick the right hosting for your website. Tip: new hosting sites are usually better because their servers are less crowded.
  • Leverage browser caching. I always use the following config to enable caching for specific types of file.
  • Enable Gzip Compression – Gzip Compression allows your web server to provide smaller file sizes which load faster for your website users. The code below should be added to your .htaccess file.

    The code above will work on Apache. If it’s not working there is another way that may work for you. Remove the above code from your .htaccess file and try this one instead:

    In my experience, enabling Gzip Compression gives you most of the score difference on Google PageSpeed Insights.
  • Get rid of render blocking scripts.
    • If you put your scripts within the head tag of your HTML –  the page will have to wait for the css and js to finish loading before it starts rendering your content. The best approach I found is to append css and js scripts dynamically to the header tag and in the end of body tag, that is – after DOM has completed loading.  You can get the script here: https://gist.github.com/carlo-fontanos/abc69dfea9d1e853c0e49fe509dbaa4b
    • Note that the loadScript()  and loadStyle()  loads the files asynchronously so if you have scripts that are depending on another script, ex. jQuery, then you should nest them like this:.
    • Observation: when you refresh your page you will notice that it loads an unstyled HTML for a few seconds then renders back properly after your critical stylesheets have been fully loaded – uhh.. that’s ugly. Let’s fix that!  Within the head tag of your HTML (right after your meta tags), add the following styles:
    • The CSS code above TEMPORARILY sets all the elements to appear white and all images removed. Basically we are trying to make the screen appear like it’s still trying to load the content when actually the content is already there, but just not visible yet.
    • If you look at the <styles>  tag, it has an ID “init-style” – this will help us easily identify and remove it when all our critical css files have been fully loaded. So for example, we need to load a critical css file such as bootstrap: 1st is we load bootstrap dynamically, then 2nd we remove the initial styles from the header tag right after. Like this:
  • Combine CSS Files. Fewer HTTP requests for CSS content is better rather than loading a bunch individual CSS files. You can use this PHP script to combine multiple CSS files into a single file then compress it on-the-fly: https://gist.github.com/carlo-fontanos/f2ab6afaa71f29595b3f0d95f40ad2b0

I see a lot of posts complaining about scripts like Google Analytics being a render blocker. Well, if Google is cheating on you, you can cheat Google back:

This is the user-agent for pageSpeed:

You can insert a conditional to avoid serving the analytics script to PageSpeed:

or via Javascript:

If your only concern is getting a 100/100 score this will do it.