【问题标题】:Update cache stored via HTTP2 requests更新通过 HTTP2 请求存储的缓存
【发布时间】:2016-09-20 18:41:05
【问题描述】:

通常,我会像这样通过 http2 请求提供文件:

if (req.url === '/main.html') {
  let files = {
    'main.css': {
      type: 'text/css'
    },
    'main.js': {
      type: 'application/javascript'
    }
  };
  for (let name in files) {
    let push = res.push('/' + name, {
      response: {
        'Content-Type': files[name].type,
        'Cache-Control': 'public, max-age=31556926'
      }
    });
    push.on('error', er => console.log(er));
    push.end(fs.readFileSync('/home/src/' + name));
  }
  res.writeHead(200, {
    'Content-Type': 'text/html'
  });
  res.end(`
   <html>
     <head>
       <link rel="stylesheet" href="/main.css">
       <script src="/main.js"></script>
     </head>
     <body></body>
   </html>
`);
}

我在更新这两个文件main.cssmain.js 时遇到了问题,因为它们的新内容可用。 它们会通过发送另一个/main.html 请求来更新吗?如果没有,我该如何更新它们?

【问题讨论】:

    标签: node.js browser-cache http2


    【解决方案1】:

    不,它们不会得到更新。当您尝试推送资产的新版本时,浏览器将重置流,因为它认为这些资产仍然是新鲜的。它将继续使用旧版本的资产。

    至少目前,解决方案是使用缓存摘要机制缓存清除。

    在此处阅读详细信息:

    https://www.shimmercat.com/en/info/articles/caching/

    另见this answer

    【讨论】:

    • 非常感谢您的回答。我还考虑通过max-age 设置一个非常短的生命周期来删除旧缓存。你试过这种方法吗?它适用于所有浏览器吗?
    • 嗨!是的,这种方法在 Internet Explorer、Chrome 和 Firefox 中运行良好。 Safari 不支持 HTTP/2 Push,所以那里不需要它。
    猜你喜欢
    • 2021-11-11
    • 2021-10-13
    • 2020-06-03
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多