2

I am working on node js web application and I am doing a http get request which address a db and takes data via query.

The http get request is working fine on chrome but on IE each get is not updated but returned from some sort of cache. That makes the result of the db query to be not updated (beacuse it is taken from chache).

I can see that it is being taken from chache form F12 developer tool in IE:

enter image description here

My code below. I know that I should add something like:

res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");

to my request but I think that I maybe put this line in the wrong place beacuse the get reuqest still taken from chache and gives me bad result...

client

$http.get('/users')

.success(function(data) {
    $scope.usersNumber = data.length;

})
.error(function(data) {
    console.log('Error: ' + data);
});

server

app.get('/users', function(req, res){
    get_user(req, res);
});

var get_user= function(req, res){

   var query= User_session.find();
   query.exec( function(err, docs){
      res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");

      res.json(docs);
        //mongoose.connection.close();

    });  

}

1 Answer 1

2

Try adding max-age to your header:

Cache-Control:private, no-cache, no-store, must-revalidate, max-age=0
Pragma:no-cache

The response that you captured from the developer tools shows that the response is picked from cache. The cached response had a expires header which specified the duration for which the response can be picked from cache and is not considered stale.

You would want to include a no-cache directive for End-to-end reload

The request includes a "no-cache" cache-control directive or, for compatibility with HTTP/1.0 clients, "Pragma: no-cache". Field names MUST NOT be included with the no-cache directive in a request. The server MUST NOT use a cached copy when responding to such a request.

Reference: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

You can also try this another solution to force client reloads. Add a query string parameter to your url. Eg: If you have a /users, instead make the url as /users?uniqstamp=<timestamp>

Your browser always thinks that the entire path including the query string makes a URL. so you will always get a 200 ok that is served fresh. Make sure that timestamp that you use is from javascript time in millis so that the timestamp is always unique.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, tried it but I am still getting results from chache
I updated with the Pragma: no-cache for HTTP/1.0 clients.
Did it work? I also added another technique to prevent caching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.