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

Update cache stored via HTTP2 requests

通常,我会像这样通过 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 请求来更新它们?如果没有,我该如何更新它们?

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

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

在此处阅读详细信息:

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

另见