Are browsers supposed to handle 304 responses automatically?

Perhaps this is a stupid question, but I have not yet found a clear answer.

My server handles ETag caching for some fairly large JSON responses that we have, returning 304 NOT MODIFIEDwith an empty body if the header If-None-Matchcontains the same hash as the new one generated (shallow ETags).

Is it supposed that browsers should handle this automatically, or client applications in the browser that consume APIs must asynchronously implement some logic to handle such responses (for example, use the cached version if it responds 304, create / update the cached version otherwise)?

Because so far I have manually implemented this logical client side, but I wonder if I just invented the square wheel ...


In other words, for example, with a heading Cache-Control, client applications in the browser do not need to analyze the value, for example, check for max-age, somehow store it, set the timeout, etc.: directly in front of the browsers. The question is, should browsers behave the same when they receive 304?


Here is how I wrote my client so far (built using AngularJS, working in browsers):

myModule

  .factory("MyRepository", ($http) => {

    return {
      fetch: (etag) => {
        return $http.get(
          "/api/endpoint",
          etag ? { headers: { "If-None-Match": etag } } : undefined
        );
      }
    };

  })

  .factory("MyService", (MyRepository, $q) => {

    let latestEtag = null;
    let latestVersion = null;

    return {
      fetch: () => {
        return MyRepository
          .fetch(latestEtag)
          .then((response) => {
            latestEtag = response.headers("ETag");
            latestVersion = response.data;
            return angular.copy(latestVersion);
          })
          .catch((response) => {
            return 304 === error.status
              ? angular.copy(latestVersion)
              : $q.reject(response)
          });
      }
    };

  });

So basically, is this logic needed above, or should I just use it $http.get("/api/endpoint")directly?

, , , , , "" .

+4
2

304

,

<html>
<head>
<script src="./axios.min.js"></script>
<script src="./jquery-3.3.1.js"></script>
</head>
<body>
<h1>this is a test</page>
</body>
</html>

test.json

root@vagrant:/var/www/html# cat test.json
{

"name": "tarun"

}

nginx,

location ~*  \.(jpg|jpeg|png|gif|ico|css|js|json)$ {
   expires 365d;
}

AXIOS

Axios

, 200 304, JS-

JQuery

jQuery response

jQuery

curl , 304

$ curl -v 'http://vm/test.json' -H 'If-None-Match: "5ad71064-17"' -H 'DNT: 1' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' -H 'Accept: */*' -H 'Referer: http://vm/' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' -H 'If-Modified-Since: Wed, 18 Apr 2018 09:31:16 GMT' --compressed
*   Trying 192.168.33.100...
* TCP_NODELAY set
* Connected to vm (192.168.33.100) port 80 (#0)
> GET /test.json HTTP/1.1
> Host: vm
> If-None-Match: "5ad71064-17"
> DNT: 1
> Accept-Encoding: gzip, deflate
> Accept-Language: en-US,en;q=0.9
> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
> Accept: */*
> Referer: http://vm/
> X-Requested-With: XMLHttpRequest
> Connection: keep-alive
> If-Modified-Since: Wed, 18 Apr 2018 09:31:16 GMT
>
< HTTP/1.1 304 Not Modified
< Server: nginx
< Date: Wed, 18 Apr 2018 09:42:45 GMT
< Last-Modified: Wed, 18 Apr 2018 09:31:16 GMT
< Connection: keep-alive
< ETag: "5ad71064-17"
<
* Connection #0 to host vm left intact

, 304, .

+2

, , . , :

Validation , , .

ETag Last-Modified. - , 304 Not Modified ; , 304 .

304, , , .

; 304 , , .


HTTP Caching .

0

Source: https://habr.com/ru/post/1696014/


All Articles