views:

1213

answers:

2

Today I have checked performance statistics of my website using YSlow. I got a warning (or error may be) which is as below

Add Expires headers
There are 15 static components without a far-future expiration date.

    * (no expires) http://www.example.com/video/css/global.css
    * (no expires) http://www.example.com/video/js/global.js
    * (no expires) http://www.example.com/video/images/main-bg.png

What this means and how to achieve this in PHP and ASP.NET both. I am on shared hosting server, so please tell me some way to do this using code, because I'll not able to make any modification at the server end.

If I'll expire the header then is there any chance that if I'll make changes in the CSS then user will not get them right away because css and other files are cached for a certain time limit (1 month, week). Is it?

Is there any disadvantages of using expire headers??

+5  A: 

PHP

$time = time() + 3 * 24 * 60 * 60; // 3 days
header('expires: ' . gmdate('D, d M Y H:i:s \G\M\T', $time);

But I would recommend you the Apache module mod_expires [1]. You can then put something like this in an .htaccess file

<FilesMatch "\.(jpg|gif|png|css|js)$">
    ExpiresActive on
    ExpiresDefault "access plus 3 days"
</FilesMatch>

This will match all Images, CSS and JavaScript files and set an Expires header for 3 days

[1] http://httpd.apache.org/docs/2.0/mod/mod_expires.html

Philippe Gerber
if you are worried about your css being cached for too long, what you can do is in the source html page is append some text to the stylesheet or javascrip link, eg <link href="styles.css?changethistostopcaching" >. just change that text when you publish a new css file
bumperbox
I know that many web proxy configurations will kill the query tag scheme because the proxy config is set to never cache any URL with a query, due to bad practice of web apps using query GETs when they should use POST.
Zan Lynx
+2  A: 

Obviously the disadvantage to the expire header is what you already said, if you make a change then the user won't receive the new version until there's expires.

The other option is to send a date modified header, that way next time the user visits the browser will ask if the file has changed since then... if not it will just load the cached version. Not sure which browsers actually follow this standard though.

These options still only work with dynamic pages, to do this with static content such as stylesheets or images you will need more access to the server. (You can achieve some of these results with a .htaccess file as well)

Here I Googled further reading for you.

PHP Resource: http://www.sitepoint.com/article/caching-php-performance/

ASP.NET Resource: http://ondotnet.com/pub/a/dotnet/2002/12/30/cachingaspnet.html

.htaccess Resource: http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html

Kane Wallmann
All browsers worth the name follow the If-Modified-Since scheme. However, that still requires an expensive round trip to the web server. An Expires tag is hundreds of milliseconds faster. Multiply by many items and it becomes seconds faster.
Zan Lynx