Google Minify and Far Future Expires Header
Posted on | November 3, 2014 | No Comments
Google takes into account page load time into its ranking algorithm. As such, web developers the world over are trying to squeeze as much functionality into as few bytes as possible. This is where Minifiers come into the picture – they automatically remove all un-needed bytes from javascript and css files before sending them to the user. Less bytes means less load time. This is all well and good, but a new issue crops up when using Google Minify – the Expires header is not a Far Future Header. This blog post explores what a far future expires header is, and how to configure Google Minify to fix the issue.
What is an Expire Header?
An Expires Header tells the users browser how long to wait before asking for the same resource again. As an example, a css file is not going to change between requests so there is no point asking for it again. A far-future expires header has an expiration date of a week or more. On an Apache server we can use a .htaccess file to add the following command to add an expires header to our css, js, and image files telling the browser not to download these files again for a week:
[c]
## expire 1 WEEK to images, css and javascript files ##
Header set Cache-Control “max-age=604800, public”
[/c]
BUT!
But, this does not work on Google Minify. Minify is a PHP script that is combining, minifying, and echoing the contents of css and js files, but is not a CSS or JS file!
The answer lies in Minify’s config.php file.
There is a config setting that controls the maximum age of the response sent to the browser:
[php]
$min_serveOptions[‘maxAge’] = 5400;
[/php]
Although the documentation does not mention it, this also controls the expires header! The above value is in seconds and works out to 90 minutes. Admit it – that’s not very far-future, is it?
I have recently become a fan of “strtotime”, so to set the expires to a week for this setting I did:
[php]
$min_serveOptions[‘maxAge’] = strtotime(“+1 week”) – time();
[/php]
Et voila, the expires header for the minified css and js files has been set to a week.
Conclusion
The value of the expire header in Google Minify is controlled in the $min_serveOptions[‘maxAge’] setting in the config.php file. Setting it to a ‘far-future’ value will help save your bandwidth, the users’ bandwidth, and reduce the load time of your website on users’ browsers.
Posted By:Richard Parnaby-King