what’s the difference between Expires and Cache-Control headers?

Http HeadersCache Control

Http Headers Problem Overview


What’s the difference between Expires and Cache-Control headers?

Http Headers Solutions


Solution 1 - Http Headers

Cache-Control was introduced in HTTP/1.1 and offers more options than Expires. They can be used to accomplish the same thing but the data value for Expires is an HTTP date whereas Cache-Control max-age lets you specify a relative amount of time so you could specify "X hours after the page was requested".

https://stackoverflow.com/questions/3099526/html-cache-control is a very similar question and has a good link to a caching tutorial that should answer most of your questions (e.g., http://www.mnot.net/cache_docs/#EXPIRES). To sum up though, Expires is recommended for static resources like images and Cache-Control when you need more control over how caching is done.

Solution 2 - Http Headers

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.

Solution 3 - Http Headers

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.

Solution 4 - Http Headers

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.

Solution 5 - Http Headers

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.

Solution 6 - Http Headers

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.

Solution 7 - Http Headers

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwhiteroseView Question on Stackoverflow
Solution 1 - Http HeadersA LeeView Answer on Stackoverflow
Solution 2 - Http Headersuser3241258View Answer on Stackoverflow
Solution 3 - Http HeadersPaulView Answer on Stackoverflow
Solution 4 - Http HeadersnodejhView Answer on Stackoverflow
Solution 5 - Http HeadersDeviView Answer on Stackoverflow
Solution 6 - Http HeadersRobView Answer on Stackoverflow
Solution 7 - Http HeadersrayView Answer on Stackoverflow