Site Analyzer vs mod_pagespeed
I will explain to you how to manage to validate ” the implementation of cache ” by the Site Analyzer tool . Whether you use mod_pagespeed or not on apache in version 2.X. It is not necessarily THE objective, but it takes an important part in the scoring. And all the other tools will also ask you to validate this parameter. (for the impatient, the solution is at the very end of the article).
Site Analyzer Paradox
Before continuing further on how to succeed, I want to warn you that doing this may slightly degrade your rating on Page Speed Insights … On the home page I lost 20 points on average on mobile and desktop. On the content pages I only have a loss of 5/6 points.
Cache-control the header so much sought
So, to find out if you have cache on web resources (html, css, js, etc.) with apache nothing more simple. To do so, simply look at the headers returned by the website’s web server. To see these headers under firefox you can use the firebug extension via the “Network” tab and by clicking on the desired resource:
Cache-control , is the header that your server sends to tell the web browser to keep the file cached. So there are several values that we will look at right away.
- no-cache: tells the browser to systematically request the file from the server
- public: resource can be cached by anyone
- private: same as public, except that the resource cannot be cached by proxies
- must-revalidate: the resource you request must be re-downloaded by the browser
- max-age: this is the time in seconds that your browser will have to wait before going to search for the content.
So if your header looks like one of the examples below it means that you have no cache on this resource:
- max-age = 0, no-cache
- max-age = 0
- no-cache
What we are looking for is something like this: max-age = 3600, public or max-age = 3600, private
To know everything about Cache-Control you can also read this page on Wikipedia: Cache-Control
How to configure Cache-Control on apache?
To do this you already need the following modules installed on your apache server:
- headers
- hidden
I will not go back on how to install them on apaches, it does not seem necessary to me, because very often they are automatically. For apache to use caching in your browser, simply add the following lines to the .htaccess file in your file.
<IfModule mod_headers.c>
#Caching of all
Cache-Control Header set “max-age = 600, private, must-revalidate”
#Extended caching for
image, CSS and JS resources
<FilesMatch “. (Ico | jpe? G | png | gif | swf | css | gz | js |
js? Ver = * | css? Ver = *) $” >
Header set Cache-Control “max-age = 2592000, public”
</FilesMatch>
#Caching HTML
resources (the famous ones!)
<FilesMatch “. (Html | htm) $”>
Header set Cache-Control “max-age = 7200, public”
</FilesMatch>
</ IfModule>
Once you add these lines to your .htaccess file, refresh your site page and watch what firebug tells you about the headers. Logically everything should be good!
Mod_pagespeed cache-control for HTML, PHP files
So, in my first mod_pagespeed test I was very happy. But when I was asked to validate the site on Site Analyzer , I was a little disappointed … I also spent 3 or 4 hours finding a solution, I scoured the internet but found nothing …
The real concern is that all my resources were well cached, css, js etc. But the HTML page in itself no … While I had everything it does, at least I thought so, in my .htaccess to have this value.
First, the little magic lines that I showed you before were missing. But above all I had a setting to activate on mod_pagespeed which made all the possible configurations that I could do in my .htaccess obsolete.
So to validate the setting up of cache under Site Analyzer with mod_pagespeed you should not activate the following option:
AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text / html
You must therefore comment the line in the mod_pagespeed configuration file . Please note that you can only deactivate it from the configuration file. So if your host has activated it, you are blue … Hope this article helps you!
You must therefore comment the line in the mod_pagespeed configuration file . Please note that you can only deactivate it from the configuration file. So if your host has activated it, you are blue … Hope this article helps you!