0

I have a mock project where I am testing Etag and If-None-Match behavior on different browsers. A Get request is sent on page load and I will always respond with 200 and assign a new Etag value.

window.addEventListener('load', () => getEtag(false));

function getEtag() {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            console.log("Success");
        }
    };

    xhr.onerror = function () {
        console.log("Failure");
    };

    xhr.open("Get", "http://localhost:8080/etag", true);
    xhr.send();
}

Java server side:

   @GetMapping("/etag")
    public String getEtag(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("ETag", UUID.randomUUID());
        return "";
    }

I understand that this not the intended use for Etags but bear with me. On Chrome and Firefox this works as expected. On every page load the request includes the If-None-Match header. However on Safari the header is included only in every other request. I know there are several questions pertaining to Etags and If-None-Match and Safari but they are all about its complete absence. In my case the first request does not contain the If-None-Match (as expected), the second request will but then the third request will not. It then oscillates between sending the header and not. I modified my code to return 304 if the If-None-Match header was present which prompted Safari to send the header with every request but it would not update the Etag value with the new one assigned. I understand that that is expected behavior, but is there a way to make Safari behave like chrome and Firefox when I return 200 and a new Etag?

Dean
  • 1,833
  • 10
  • 28
  • Are you sure the requests without the header aren't being served out of the cache? For your test to be effective you need to disable caching with `no-cache` or something similar. – Kevin Christopher Henry Apr 01 '19 at 12:47
  • Without a cache there is no way the browser can send the `if-none-match` header, that is a cached detail after all. I am certain that the request hit the server as I am logging them. – Dean Apr 01 '19 at 14:38
  • Please see [the difference between `no-cache` and `no-store`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Cacheability). To conduct your test properly you need to allow storage but disable serving from the cache without revalidation. That said, you appear to be running into some other problem. – Kevin Christopher Henry Apr 01 '19 at 15:45
  • adding `Cache-Control : no-cache` makes no difference. – Dean Apr 02 '19 at 10:29

0 Answers0