簡體   English   中英

帶sails.js的應用程序沒有緩存瀏覽器

[英]app with sails.js no cache browser

我希望我的應用程序不保存在瀏覽器緩存中。 我有這項政策,但是如何應用我的所有觀點?

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

config/http.coffee ,添加一個“ nocache”中間件。 在咖啡腳本中:

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

在這種情況下,它將緩存/api路徑下的調用以外的所有內容。 您可以簡單地刪除if並禁用所有內容的緩存。

可以在config / policies.js中添加策略

'*': 'noCachePolicy'

http://sailsjs.org/#/documentation/concepts/Policies但是,以上方法要求您的所有視圖都必須在控制器中定義一個操作。 否則,策略將無法以這種方式工作。

  • 要么 -

它們可以直接附加在路由http://sailsjs.org/#/documentation/concepts/Routes/RouteTargetSyntax.html中。但是,此方法要求您寫出所有路由。

  • 要么 -

我認為,最好的方法是將其附加到所有路由,查看它以創建自己的中間件並將其放置在其中。 這樣,您就不必處理上述兩個問題。 http://sailsjs.org/#/documentation/concepts/Middleware

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM