Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure sails.js not to cache the response

I have an application on node.js using sails.js as a framework. For displaying the informations to the user I have implemented some .ejs files.
When navigating using links within the app menu, I receive a "304 - Nod Modified" response. Also, within headers in the response I see Etag, If-None-Match or Expires.
After reading some posts, I have added the "app.disable('etag')" statement within /config/express.js file in the customMiddleware function and etag and if-none-match are not sent anymore.
Anyway, now I do not see any request made to the server (just the first one is made) when accessing different pages (not even 304 response).
How can I tell sails.js to stop caching my pages?

like image 575
Victor Avatar asked Jun 22 '14 15:06

Victor


1 Answers

You can add this to a policy instead and then just apply it to the specific pages/requests you want - if you do not want to set this for the entire application.

/api/policies/nocache.js:

/**
 * Sets no-cache header in response.
 */
module.exports = function (req, res, next) {
    sails.log.info("Applying disable cache policy");
    res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
    res.header('Expires', '-1');
    res.header('Pragma', 'no-cache');  
    next();
};
like image 92
c1freitas Avatar answered Oct 18 '22 15:10

c1freitas