Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing AJAX request to revalidate cache with server, without reloading completely

I have a web application that lets the browser cache AJAX requests result for a long time. I have found out how to make a request that bypasses the cache entirely, when probable modifications are detected. But I would want to let the user trigger a data refresh.

In this scenario, I'd like the browser to check with the server if the cache is stalled but use it if it is not (that is, if the server responds with a 304 code). The goal is to spare the loading time because the data is huge.

The server includes the following headers in all responses:

Cache-Control: private, max-age=604800
Last-Modified: ... # actual last modification date

I managed to burst the cached object entirely in Chrome (not tested other browsers yet) by using the following HTTP headers in the request:

Cache-Control: max-age=0
If-Last-Modified: Tue, 01 Jan 1970 01:00:00 +0100

The If-Last-Modified line is the one that really has an effect. Chrome seems to ignore the Cache-Control header in the request.

I have also found that using Cache-Control: must-revalidate in the server response forces the browser to validate its cache with the server for each request.

But is there any way to revalidate for just one precise request, decided on the client-side?

Note that I'm not specially attached to doing this with HTTP headers, so any other method that I would not be aware of is welcome!

like image 969
rixo Avatar asked Jun 25 '13 22:06

rixo


1 Answers

you can add a url parameter which value base on time to clean cache for just one precise request.

$.ajax({
    url:"/questions?nocache="+Date.now(),
    "success":function(data){
         console.log(data);
    }
});
like image 143
bronze man Avatar answered Nov 15 '22 19:11

bronze man