Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference Expires and Cache-control:max-age?

Could you tell me the difference of Expires and Cache-control:max-age?

like image 950
freddiefujiwara Avatar asked May 10 '11 07:05

freddiefujiwara


People also ask

Should I use Max-age or expires?

Quick Answer: Expires sets an expiry date for when a cookie gets deleted. Max-age sets the time in seconds for when a cookie will be deleted (use this, it's no longer 2009)

What is Cache-Control max-age?

Cache-Control: Max-AgeAfter expiring, a browser must refresh its version of the resource by sending another request to a server. For example, cache-control: max-age=120 means that the returned resource is valid for 120 seconds, after which the browser has to request a newer version.

What's the difference between Cache-Control max-age 0 and no cache?

When max-age=0 is used, the browser will use the last version when viewing a resource on a back/forward press. If no-cache is used, the resource will be refetched.

What is cache expiry?

The value of the Expires date/time can cause the following specific cache behavior: When the Expires date is equal to the Date header value, the response is considered to be expired. When a response has an Expires header field with a date/time that is in the future, then that response is considered "cacheable".


2 Answers

Expires was defined in the HTTP/1.0 specifications, and Cache-Control in the HTTP/1.1 specifications.

I would suggest defining both so you cater to both, the older clients that only understand HTTP/1.0, and the newer ones.

like image 107
Druid Avatar answered Sep 29 '22 15:09

Druid


Expires was specified in HTTP 1.0 specification as compared to Cache-Control: max-age, which was introduced in the early HTTP 1.1 specification. The value of the Expires header has to be in a very specific date and time format, any error in which will make your resources non-cacheable. The Cache-Control: max-age header's value when sent to the browser is in seconds, the chances of any error happening in which is quite less.

Since you can specify only one of the two headers in your web.config file, I'd suggest going with the Cache-Control: max-age header because of the flexibility it offers in setting a relative timespan from the present date to a date in the future. You can basically set and forget, as compared to the case with Expires header, whose value you will have to remember to update at least once every year. And if you set both headers programmatically from within your code, know that the value of Cache-Control: max-age header will take precedence over Expires header. So, something to keep in mind there as well.

From Setting Expires and Cache-Control: max-age headers for static resources in ASP.NET

like image 38
Jeff Lewis Avatar answered Sep 29 '22 16:09

Jeff Lewis