David Klempfner
David Klempfner

Reputation: 9920

Why am I getting 200 (from disk cache) instead of 304 Not Modified?

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

Upvotes: 4

Views: 12203

Answers (2)

Md Atiqur
Md Atiqur

Reputation: 683

If you want to solve 200 (from disk cache) issue here is the solution

Add these headers to your request call

[
    'Cache-Control' => 'no-cache, no-store, must-revalidate',
    'Pragma' => 'no-cache',
    'Expires', 0
]

From My understand the request saving the response in browser cache that's why it's returning the 200 (from disk cache) and it's return the old loaded data

Note: I faced same error for file download api. wher First time it's return 200 but after that same api getting 200 (from disk cache)

My Solution was for api download response:

return response()->download($attachment->getPath(), $attachment->name . '.' . $extention, [
    'Cache-Control' => 'no-cache, no-store, must-revalidate',
    'Pragma' => 'no-cache',
    'Expires', 0
]);

But maybe in your case this isn't right answer but so far this is header issues

Upvotes: 0

miniton
miniton

Reputation: 31

Do you have the cache disabled in the devtools?

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

Upvotes: 0

Related Questions