Saturday, July 25, 2009

Browser Caching -Expires,Cache Control & Deflate

Expires Header – will stop the request(304 request-if modified ?) sent from browser

Disadvantage – even if the asset changes the browser will serverthe cached image without even checking if the image has changed.

The only solution is to come up with Versoning if we set Expires.

Syntax(in apache httpd.conf)

#Adding expires header

<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">

Header set Expires "Wed, 31 Dec 2008 23:59:59 GMT"


Also can set using access plus x days

Cache control will over ride expires header even if set.

Add this line for disabling cache control

Header set Cache-control: No-cache,no-store

Add this line for enabling cache control

Header set Cache-Control "private,max-age=0,must-revalidate"

The following lines can be used to remove the ETag header

Header unset ETag

FileETag None

Etag generates the tag using the ip of the hosted machine and when the assets are hosted in multiple machines , and requests may go to different machines and hence e-tags may be different and they will be downloaded again

Also while doing scp(having multiple deployment machines), to retain the original timestamps we need to use ‘scp –p’ whereas the touch command can change the access/modified time for the file after it is copied.

The above cache control works on the basis of the last modified time .And hence the modified time stamps have to be kept same in all the asset holding machines , so when copying the assets copy with same time stamps else requests will be sent again.

Adding Gzip configuration
<FilesMatch "\.(html|htm|js|css)$">
SetOutputFilter DEFLATE

Adding Expires
ExpiresActive On
ExpiresDefault "now plus 0 seconds"

<FilesMatch "\.(jpg|jpeg|png|gif|swf|mp3)$">
ExpiresByType image/jpg "access plus 14 days"
ExpiresByType image/jpeg "access plus 14 days"
Header set Cache-Control "public,max-age=1209600"
Header unset Pragma

No comments:

Free Domain Names @!