Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does browser decide when to request a cached file again?

I have a webpage that I am caching for 12 hours. These are two screenshots of the response headers.

enter image description here

and

enter image description here

As you can see both the 'Date' and 'Expires' header have changed in values. My understanding is that 'Date' is the time when file is requested by us and 'Expires' is the time when the browser is supposed to check for a newer version. After an interval of four minutes both the values have updated. So, how does browser decide when it is time to request newer version of file? Will it not cause the file to be cached indefinitely?

I understand that the 'Expires' value is just a suggestion for the browser. But how does browser know the time when the file was requested for first time because 'Date' header also gets updated each time.

I have one more question. My webpages have .php extension. However, my header shows:

Content Type : "text/html"

Why is this happening? Content Type : "text/html" causes my webpages to be cached. Caching does not happen on pages that have session_start()? Is it because of the session_start() or is it just a coincidence?

like image 415
SanJeet Singh Avatar asked Oct 19 '22 18:10

SanJeet Singh


1 Answers

  1. HTTP 'Date' header is just the current date and time on the server. See the RFC for more details. It has no relation whatsoever to the resource (file/page) being served

  2. HTTP 'Expires' header is an advice for browser, usually it's set in your web server config or directly in the application code. In most cases it's current time + some extra hours/days as seen reasonable by webmaster. So if expiration is set to, let's say, 3 days – 'Expires' will always show current time in 'Date' field and current time + 3 days in 'Expires' field. Doesn't really matter when the file was updated, it has no effect.

If your browser has this file in cache and the original 'Expires' moment hasn't arrived yet – browser will use the cached version. There are some ways to force update though.

  1. If your PHP outputs HTML (which is true in many cases), content type of 'text/html' is totally correct. It could be 'application/json', 'image/png' and anything else when appropriate. PHP is the programming language, and content type refers to the format of data received from HTTP server, it could be just anything.
like image 141
Denis Mysenko Avatar answered Oct 31 '22 19:10

Denis Mysenko