How can I increase the time that Web browsers cache my files for?
By default, Web browsers that visit your site will "cache", or "remember", files they download for a few minutes.
If a visitor looks at the same page twice, the browser will show the version it cached, without contacting the server again. This speeds up visitors' computers when they visit your site twice in a short time.
If you have files on your site that don't change often (particularly image files), you can increase the length of time that browsers cache those files for.
For example, if you set your JPEG files to be cached for a month, people who visit your site every day will find that it's faster because they don't need to download all the unchanging images every day.
On this page:
- Setting a longer cache time
- Be careful about what you cache
- Why doesn't Tiger Technologies automatically set high caching times to speed up my site?
- You can't control everything
Setting a longer cache time
Our servers offer the Apache Web server "mod_expires" feature.
If you decide you want to set the cache time to one month for all image files, for example, you could add these lines to a .htaccess file:
ExpiresActive On ExpiresByType image/gif "now plus 1 month" ExpiresByType image/png "now plus 1 month" ExpiresByType image/jpeg "now plus 1 month" ExpiresByType image/svg+xml "now plus 1 month" ExpiresByType image/x-icon "now plus 1 month" ExpiresByType application/font-woff "now plus 1 month"
You can also increase the time for which CSS and JavaScript files are cached. Here's an example of caching those for two days:
ExpiresActive On ExpiresByType text/css "now plus 2 days" ExpiresByType application/javascript "now plus 2 days"
And here's an example of caching HTML pages for just a few minutes:
ExpiresActive On ExpiresByType text/html "now plus 5 minutes" ExpiresByType text/plain "now plus 5 minutes"
Be careful about what you cache
In the example above, we chose to cache images for a month, but we only cached CSS and JavaScript files for two days — and HTML pages would only be cached for a few minutes.
That's because CSS and JavaScript files, and particularly HTML pages, are more likely to change. If you do need to change them, you don't want to wait a full month for visitors to see the difference.
Think carefully about the worst case: don't set a very long cache time on a file you might need to change. In particular, it's usually a bad idea to set a long cache time on HTML pages.
(If you do need to change a file that you've set a long cache time for, the only thing you can do is give the file a new name and update any links that point to it.)
Why doesn't Tiger Technologies automatically set high caching times to speed up my site?
Unfortunately, we don't know how long it's appropriate to cache your files for. Some people use images that change every minute, for example; if we told browsers it was okay to cache those for hours, it would cause problems.
Only you know how often your content changes, so only you can choose the right values.
You can't control everything
The techniques we suggest above almost always work. But remember that the headers you can set on the Web server are merely asking the browser to do something. They can't force the browser to do something it doesn't want to do.
A common Internet complaint is that a small handful of browsers or "proxy caches" don't cache things properly no matter what you do, due to bugs or misconfigurations. The tips above will work in almost all cases, but the nature of the Internet means you can't guarantee it will always work the way you hope.
Copyright © 2000-2024 Tiger Technologies LLC