2

I'm requesting a css file over HTTP from Chrome (Version 93.0.4577.63 (Official Build) (64-bit)). The initial request looks like this:

enter image description here

As you can see, it has Cache-Control: no-cache in the request.

Doesn't this mean it first has to check with the server whether or not its version of the css file is up to date?

In which case it should get a 304 Not Modified (assuming the file hasn't been modified) on the next request?

Here's the request/response when I did a refresh of the page:

Why am I getting a 200 (from disk cache) and not a 304 Not Modified?

enter image description here

The issue is that some users of this website are getting an out of date version of the css file.

I suspect it's because the cached version is being returned before being revalidated with the server which is what I thought no-cache was meant to do.

Update:

I'm not sure why, but when I did refresh again, I got fewer headers in the request:enter image description here

I didn't have the Disable cache button checked

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
  • 1
    The only way the web server can return 304 Not Modified is if it knows you already have the proper version, and to do that you have to send the ETag of the version you have. It can then do the check and return the status. The web server does not maintain any state about what it has already sent you. – Tim Roberts Sep 09 '21 at 00:23
  • I agree that this is surprising. Does [this answer](https://stackoverflow.com/a/14842553/2395796) help? Try in Firefox or another browser, and try triggering it by something other than a reload. Also, did you mean to set `no-cache` on the request? That should work as you expect, but normally you'd set it on the response. – Kevin Christopher Henry Sep 09 '21 at 00:29
  • @KevinChristopherHenry I tried copying the URL and opening it in another tab, and I get the same as the last screenshot in the question. Firefox doesn't show `200 (from cache)` responses unfortunately. But other users are using Firefox and are getting a stale version because of `200 (from disk cache)`. I'm not sure how the request is made to be honest. I thought `no-cache` in the request would have the same effect as using it in the response? Could it be what Tim Roberts said, it's because the request has no `If-None-Match` header? – David Klempfner Sep 09 '21 at 00:55
  • 1
    I pointed out in [this answer](https://stackoverflow.com/a/69024330/2395796) that the meaning is not the same in the request and the response. Since the response to your original request doesn't have a `Cache-Control` header, the browser is free to set its own freshness time. When the second request is made, the resource is in the cache and considered fresh. The question is why the browser still serves it in the presence of the `no-cache` request directive. I'm not sure, but the simplest thing would be to switch to sending `no-cache` in the response. – Kevin Christopher Henry Sep 09 '21 at 07:49
  • `If-None-Match` will be added by the browser when it decides to revalidate. Since the resource is being served from the cache, there's no reason to add it. – Kevin Christopher Henry Sep 09 '21 at 07:54

1 Answers1

0

Do you have the cache disabled in the devtools?

In Firefox, I get 304 only when the cache is not disabled.

ouflak
  • 2,458
  • 10
  • 44
  • 49
miniton
  • 23
  • 7