通常情况下,我会通过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.css
和main.js
时出现问题。
它们只是通过发送另一个/main.html
请求来更新吗?如果没有,我该如何更新它们?
答案 0 :(得分:1)
不,他们不会得到更新。当您尝试推送新版本的资产时,浏览器将重置流,因为它认为这些资产仍然是新鲜的。它将继续使用旧版本的资产。
至少目前,解决方案是使用缓存摘要机制和缓存清除。
请在此处阅读详细信息:
https://www.shimmercat.com/en/info/articles/caching/
另见this answer。