16 Jul 2010

Page speed: optimize caching

, , ,

Again, the website speed optimization freak in me speaks ! Maybe Page Speed is telling you to leverage browser caching? Or YSlow to configure entity tags? So how do we do it ? It's pretty simple, open your .htaccess file (or create it if you don't have one) and add these lines :

Turn off unused entity tags

FileETag none

Now we have to add an file type for favicons

AddType image/x-icon .ico

Icons, images, flash objects are things that don't change often, so we can cache them for 2 months (or even more)

<FilesMatch "\.(ico|jpg|jpeg|png|gif|swf)$">
ExpiresActive on
ExpiresDefault "access plus 2 months"
</FilesMatch>

On the other hand, JavaScript and CSS files change often, so let's cache them for 3 days only. If you don't make changes to your design often, you can set this value longer.

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

In the end, you have this in your .htaccess file !

FileETag none
AddType image/x-icon .ico
<FilesMatch "\.(ico|jpg|jpeg|png|gif|swf)$">
ExpiresActive on
ExpiresDefault "access plus 2 months"
</FilesMatch>
<FilesMatch "\.(js|css)$">
ExpiresActive on
ExpiresDefault "access plus 3 days"
</FilesMatch>

Leave a Reply

get in touch

Always happy to receive an email!