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?
Yes, Expires and max-age do the same thing, but in two different ways. Same thing with Last-Modified and Etag
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
andExpires
are present,Cache-Control
takes precedence. readYou'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).
All files can benefit caching. You've got two different approaches:
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://tools.ietf.org/html/rfc7232#section-2.3