Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leverage browser caching - expires or max-age, last-modified or etag

I'm having difficulty finding a clear-cut, practical explanation for what is the correct way to leverage browser caching to increase page speed.

According to this site:

It is important to specify one of Expires or Cache-Control max-age, and one of Last-Modified or ETag, for all cacheable resources. It is redundant to specify both Expires and Cache-Control: max-age, or to specify both Last-Modified and ETag.

Is this correct? If so, should I use Expires or max-age? I think I have a general understanding of what both of those are but don't know which is usually best to use.

If I have to also do Last-Modified or ETag, which one of those? I think I get Last-Modified but am still very fuzzy on this ETag concept.

Also, which files should I enable browser caching for?

like image 750
Andy Avatar asked May 01 '11 21:05

Andy


People also ask

What does leverage browser caching mean?

To leverage your browser's caching generally means that you can specify how long web browsers should keep images, CSS and JS stored locally. That way the user's browser will download less data while navigating through your pages, which will improve the loading speed of your website.

How do I fix leverage browser caching in WordPress?

Fixing Leverage Browser Caching in WordPress with the W3 Total Cache can be done by following the steps listed below: Download and install the W3 Total Cache WordPress plugin. Navigate to General Settings and select Enable Browser Cache. Click save and navigate to Browser Cache Settings (top of the page)

How do I fix the leverage browser caching warning in HTML?

To fix the leverage browser caching warning with WP Rocket, all you have to do is install and activate the plugin. That's it. For more details, see our guide on how to properly install and setup WP Rocket in WordPress. WP Rocket will automatically enable browser caching and modify your .

How long does browser cache last?

The response can be cached by browsers and intermediary caches for up to 1 day (60 seconds x 60 minutes x 24 hours). The response can be cached by the browser (but not intermediary caches) for up to 10 minutes (60 seconds x 10 minutes). The response can be stored by any cache for 1 year.


1 Answers

Is this correct?

Yes, Expires and max-age do the same thing, but in two different ways. Same thing with Last-Modified and Etag

If so, should I do expires or max-age?

Expires depends on accuracy of user's clock, so it's mostly a bad choice (as most browsers support HTTP/1.1). Use max-age, to tell the browser that the file is good for that many seconds. For example, a 1 day cache would be:

Cache-Control: max-age=86400

Note that when both Cache-Control and Expires are present, Cache-Control takes precedence. read

If I have to also do Last-Modified or ETag, which one of those? I think I get Last-Modified

You're right, Last-Modified should be better. Although it's a time, it's sent by the server. Hence no issue with user's clock. It's the reason why Last-Modified is better than Expires. The browser sends the Last-Modified the server sent last time it asked for the file, and if it's the same, the server answsers with an empty response «304 Not Modified»

Also, you must note that ETag can be useful too, because Last-Modified has a time window of one second. So you can't distinguish two different sources with the same Last-Modified value. [2]

Etag needs some more computing than Last-Modified, as it's a signature of the current state of the file (similar to a md5 sum or a CRC32).

Also, which files should I enable browser caching for?

All files can benefit caching. You've got two different approaches:

  • with max-age: useful for files that never change (images, CSS, javascript). For as long as the max-age value, the browser won't send any request to the server. So you'll see the page loading really fast on the second load. If you need to update files, just append a question mark, and the date of change (for example /image.png?20110602, or for better proxies caching, something like /20110602/image.png or /image.20110602.png). This way you can make files expire if it's urgent (remember that the browser almost never hits the server once it has a max-age file). Main use is to speed things up and limit requests sent to the server.
  • with Last-Modified: can be set on all files (including those with max-age). Even if you have dynamic pages, you may not change the content of the file for a while (even if it's 10 min), so that could be useful. The main use here is to tell the browser «keep asking me for this file, if it's new, I'll send you the new one». So, there's a request sent on each page load, but the answer is empty if the file is already good (304 Not Modified), so you save on bandwidth.

The more you cache, the faster your pages will show up. But it's a difficult task to flush caches, so use with care.

A good place to learn all this with many explanations: http://www.mnot.net/cache_docs/

[2]: rfc7232 Etag https://www.rfc-editor.org/rfc/rfc7232#section-2.3

like image 193
Yvan Avatar answered Oct 19 '22 13:10

Yvan