Reputation: 9464
On my IIS server, I have a json file which gets updated at a variable frequency, between a day and 5 minutes.
The first load of the gives me the 200 OK as expected with all the right headers. Second load and so forth returns 304 Not Modified, also as expected. When the file is changed, I get a 200 and IIS re-uploads the file, all according to plan.
Sample 200 Response:
Accept-Ranges:bytes
Content-Encoding:gzip
Content-Type:application/json
Date:Tue, 20 Jan 2015 23:05:06 GMT
ETag:"604a75cb335d01:0"
Last-Modified:Tue, 20 Jan 2015 22:52:40 GMT
Server:Microsoft-IIS/7.5
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:ASP.NET
Sample 304 Response:
Accept-Ranges:bytes
Date:Tue, 20 Jan 2015 23:07:20 GMT
ETag:"604a75cb335d01:0"
Last-Modified:Tue, 20 Jan 2015 22:52:40 GMT
Server:Microsoft-IIS/7.5
X-Powered-By:ASP.NET
Problem
After the 4-6 loads or so, I start getting 200 OK (from cache)
with no file transfer (just getting the file from the cache) and I get the following Request Headers coming from Chrome:
Chrome isn't sending the request to the server and thus not getting the expected 304 (or 200 w/ new file download).
Attempts
I can't use cache: false
because this doesn't trigger the 304 which I want to occur if the file hasn't been updated.
I tried using ifModified: true
which gets me the 304 I want, but the success()
function receives undefined
as data.
This is the ajax call I'm using:
$.ajax({
url: "currentData.json",
//cache: false, // Not using this because I'll never get 304
//ifModified: true, // Doesn't populate data in success()
success: function (data) {console.log(data);}
});
Upvotes: 0
Views: 1540
Reputation: 4371
Without a cache control directive, the browser is free to do what it pleases. Since your content will change no more often than 5 minutes, it should be sufficient to add:
Cache-Control: max-age=300
to your headers. On the next page load, if 300 seconds have passed, it will check again with the If-Modified-Since header.
For static files in IIS it appears this can be accomplished using IIS7 Cache-Control
Upvotes: 1