简体   繁体   English

通过HTTP2请求更新存储的缓存

[英]Update cache stored via HTTP2 requests

Normally, I would serve files via http2 requests like this: 通常,我会通过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>
`);
}

I got a problem with updating those 2 files main.css and main.js when their new contents are available. 当有新内容可用时,更新这两个文件main.cssmain.js时出现问题。 Will they be updated just by sending another /main.html request? 是否仅通过发送另一个/main.html请求来更新它们? If not, how can I update them? 如果没有,我该如何更新它们?

No, they won't get updated. 不,他们不会更新。 When you try to push new versions of the assets, the browser will reset the streams because it considers that those assets are still fresh. 当您尝试推送资产的新版本时,浏览器将重置流,因为它认为这些资产仍然是最新的。 And it will continue using the old version of the assets. 并且它将继续使用旧版本的资产。

For now at least, the solution is to use a cache digest mechanism and cache busting. 至少目前,解决方案是使用缓存摘要机制缓存清除。

Read the details here: 在此处阅读详细信息:

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

See also this answer . 另请参阅此答案

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

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