Reputation: 4711
What’s the difference between Expires
and Cache-Control
headers?
Upvotes: 230
Views: 86698
Reputation: 8066
They can both be used to control the lifetime of a cached response but Cache-Control
, introduced in HTTP/1.1, offers many more features and should generally be favored over Expires
, defined in HTTP/1.0.
In modern browsers a Cache-Control: max-age
or Cache-Control: s-maxage
directive will also override any Expires header in the response.
As a concrete example, Cache-Control: max-age=604800
indicates the number of seconds that the given HTTP response should be considered fresh. In other words, the response should be considered stale and evictable from the cache 604800 seconds (7 days) after the HTTP response was generated.
Expires
uses explicit datetime strings like "Friday, 28 Feb 2020 20:20:20 GMT"
and is obviously more challenging to parse (and generate!) than a simple number of seconds integer.
Other good resources for understanding how HTTP caching works:
Upvotes: 152
Reputation: 19
Except for the private/public options of CC, I can't see any difference. When using Expires like "access plus 1 year/month/week/day", it works in exactly the same way as CC does.
Upvotes: 0
Reputation: 171
If you are still interested, I leave this recommendation directly from google's boys. https://developers.google.com/speed/docs/insights/LeverageBrowserCaching They prefer Expires before than Cache-Control
Upvotes: -1
Reputation: 8008
Cache-Control was defined in HTTP/1.1, tells all caching mechanisms from server to client whether they may cache this object. It is measured in seconds: Cache-Control: max-age=3600
.
The Expires
header field gives the date/time after which the response is considered stale. The Expires value is an HTTP-date timestamp: Expires: Tue, 18 Jul 2017 16:07:23 GMT
.
If a response includes a Cache-Control
field with the max-age
directive, a recipient MUST ignore the Expires
field.
Upvotes: 12
Reputation: 5453
Heroku devcenter has an excellent article on this subject.
Quoting from it,
While the Cache-Control header turns on client-side caching and sets the max-age of a resource, the Expires header is used to specify a specific point in time the resource is no longer valid.
Upvotes: 4
Reputation: 20091
According to this Google Developers article, HTTP Caching:
Cache-Control header was defined as part of the HTTP/1.1 specification and supersedes previous headers (e.g. Expires) used to define response caching policies. All modern browsers support Cache-Control, hence that is all we will need.
Upvotes: 25
Reputation: 535
If you are using a CDN (Cloud Delivery Network) I recommend to use Cache-Control with a max-age time in seconds. For example Cache-Control: max-age=604800. This prevents request-peaks to your origin-server: With "Expires Wed, 30 Oct 20xx 04:37:07 GMT" all browsers will request you at the same time.
Upvotes: 47