Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

304 Not Modified with 200 (from cache)

Tags:

I'm trying to understand what exactly is the difference between "status 304 not modified" and "200 (from cache)" Here is the screenshot

I'm getting 304 on javascript files that I changed last. Why does this happen? Thanks for the assistance.

like image 434
Boltosaurus Avatar asked Oct 20 '13 14:10

Boltosaurus


People also ask

What is the difference between 200 and 304 status code?

200 (cache) means Firefox is simply using the locally cached version. This is the fastest because no request to the Web server is made. 304 means Firefox is sending a "If-Modified-Since" conditional request to the Web server.

What is 304 status code in HTTP?

The HTTP 304 Not Modified client redirection response code indicates that there is no need to retransmit the requested resources. It is an implicit redirection to a cached resource.

How do I return a 304?

If-None-Match : Allows a 304 Not Modified to be returned if ETag is unchanged. the server will return 304 when the if statement is False, and browser will use cache.


1 Answers

https://sookocheff.com/post/api/effective-caching/ is an excellent source to form the required understanding around these 2 HTTP status codes.

After reading this thoroughly, I had this understanding

In typical usage, when a URL is retrieved, the web server will return the resource's current representation along with its corresponding ETag value, which is placed in an HTTP response header "ETag" field. The client may then decide to cache the representation, along with its ETag. Later, if the client wants to retrieve the same URL resource again, it will first determine whether the local cached version of the URL has expired (through the Cache-Control and the Expire headers). If the URL has not expired, it will retrieve the local cached resource. If it determined that the URL has expired (is stale), then the client will contact the server and send its previously saved copy of the ETag along with the request in a "If-None-Match" field. (Source: https://en.wikipedia.org/wiki/HTTP_ETag)

But even when expires time for an asset is set in future, browser can still reach the server for a conditional GET using ETag as per the 'Vary' header. Details on 'vary' header: https://www.fastly.com/blog/best-practices-using-vary-header/

like image 140
gargkshitiz Avatar answered Oct 02 '22 05:10

gargkshitiz