1

I have a mobile app that will periodically pull a JSON file from a Node.js server.

THE JSON response is dynamically generated with random values. It should not be cached at all.

I tried to prevent caching:

app.get('/important.json', function(req, res) {
        res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        res.setHeader("Pragma", "no-cache");
        res.setHeader("Expires", 0);
        res.json(...);
});

However, when the mobile app reloads the json, it is still getting 304 Not Modified response from the server (Observed from Fiddler).

enter image description here

Can someone advise if the anti-cache is done correctly? Is it due to the etag? If etag is the problem, can I disable etag specifically for this api/endpoint?

2
  • I tried this on a simple express server and I get 200 when I refresh the page multiple times, so it works for me. if I comment out res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); res.setHeader("Pragma", "no-cache"); res.setHeader("Expires", 0); I get 304 not modified status so you are using the cache controls correctly, it might have something to do with fiddler, try it on chrome Commented Sep 9, 2016 at 5:55
  • etag is fine, etag does not change for subsequent requests even if you tell the client to not cache it. etag only changes if the content of what you are sending changes, either way it does not matter if you set the cache controls correctly which you did here Commented Sep 9, 2016 at 6:03

1 Answer 1

4

First of all, try to disable etag:

app.set('etag', false);

Secondly, you could intercept all requests made to node with middleware placed in the top of all other handlers.

app.use(function(req, res, next) {
  //delete all headers related to cache
  req.headers['if-none-match'] = '';
  req.headers['if-modified-since'] = '';
  next();    
});
Sign up to request clarification or add additional context in comments.

Comments

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.