Jens Segers on Apr 03 2011

Website speed tip: leverage browser caching

One of the most common suggestions on Google's page speed test is Leverage browser caching. This means that for those resources the browser does not know how long to keep these file in its cache.

HTTP/S supports local caching of static resources by the browser. Some of the newest browsers use a heuristic to decide how long to cache all resources that don't have explicit caching headers. Other older browsers may require that caching headers be set before they will fetch a resource from the cache; and some may never cache any resources sent over SSL.

So it is recommended that you explicitly set caching headers for all cacheable static resources like javascript, css files, mage files and other binary object files (media files, PDF's, Flash, etc.). In general, HTML is not static, and shouldn't be considered cacheable.

To specify headers for these files we can adjust the .htaccess file (or httpd.conf). The .htaccess file should be created/edited in your root if you want to use the settings for your whole domain or you can place it in a directory you want these settings to be active. This is an example of what you could/should add:

<IfModule mod_expires.c>
  ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest "access plus 0 seconds"

# Your document html
  ExpiresByType text/html "access plus 0 seconds"

# Data
  ExpiresByType text/xml "access plus 0 seconds"
  ExpiresByType application/xml "access plus 0 seconds"
  ExpiresByType application/json "access plus 0 seconds"

# Feed
  ExpiresByType application/rss+xml "access plus 1 hour"
  ExpiresByType application/atom+xml "access plus 1 hour"

# Favicon (cannot be renamed)
  ExpiresByType image/x-icon "access plus 1 week"

# Media: images, video, audio
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType video/ogg "access plus 1 month"
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"

# HTC files (css3pie)
  ExpiresByType text/x-component "access plus 1 month"

# Webfonts
  ExpiresByType application/x-font-ttf "access plus 1 month"
  ExpiresByType font/opentype "access plus 1 month"
  ExpiresByType application/x-font-woff "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

# CSS and JavaScript
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"

  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>

</IfModule>

Check your website now

Read more about these headers here:

http://code.google.com/intl/nl/speed/page-speed/docs/caching.html
http://www.askapache.com/htaccess/apache-speed-cache-control.html


Comments

Kunjan 1 year ago

Thank you very much!


NModi 1 year ago

It's really awesome.


Lokesh Sharma 2 years ago

Thank you very much!


ravindra 2 years ago

Nice


Roel 3 years ago

Thank you!


Lee Johnson 3 years ago

Just what I needed. Thanks for the heads up !


Hamed 5 years ago

that was what i want, thanks a lot you are a life saver