简体   繁体   English

带sails.js的应用程序没有缓存浏览器

[英]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. config/http.coffee ,添加一个“ nocache”中间件。 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. 在这种情况下,它将缓存/api路径下的调用以外的所有内容。 You can simply remove the if and disable caching for everything. 您可以简单地删除if并禁用所有内容的缓存。

Policies can be added in config/policies.js 可以在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. http://sailsjs.org/#/documentation/concepts/Policies但是,以上方法要求您的所有视图都必须在控制器中定义一个操作。 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. 它们可以直接附加在路由http://sailsjs.org/#/documentation/concepts/Routes/RouteTargetSyntax.html中。但是,此方法要求您写出所有路由。

  • 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 http://sailsjs.org/#/documentation/concepts/Middleware

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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