简体   繁体   中英

app with sails.js no cache browser

I want my app is not saved in the browser cache. I have this policy, but how do I apply all my views?

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();

In config/http.coffee , add a "nocache" middleware. In coffeescript:

module.exports.http = middleware:
  order: [
    ...
    "nocache"
    "router"
    "www"
    "favicon"
    "404"
    "500"
  ]

  nocache: (req, res, next) ->
    if req.path.indexOf("/api") is 0
      res.header "Cache-Control", "private, no-cache, no-store, must-revalidate"
      res.header "Expires", "-1"
      res.header "Pragma", "no-cache"
    next()

In this case, it caches everything except calls under the /api path. You can simply remove the if and disable caching for everything.

Policies can be added in config/policies.js

'*': 'noCachePolicy'

http://sailsjs.org/#/documentation/concepts/Policies However, the above approach requires that all your views have an action defined in your controllers. Otherwise policies do not work in this manner.

  • OR -

They can be attached directly in routes http://sailsjs.org/#/documentation/concepts/Routes/RouteTargetSyntax.html However, this method requires you to write out all your routes.

  • OR -

I think your best method if you want this attached to ALL routes,views it to create your own middleware and place it in there. That way you don't have to deal with the two issues above. http://sailsjs.org/#/documentation/concepts/Middleware

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM