简体   繁体   中英

Update cache stored via HTTP2 requests

Normally, I would serve files via http2 requests like this:

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. Will they be updated just by sending another /main.html request? 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/

See also this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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