Mod*_*man 5 caching node.js express
确保所有用户获得新鲜的index.html而不是缓存的index.html的最佳方法是什么?
所以上周我尝试让用户缓存单页应用程序的 js 和 css 包。我在 server.js 文件中添加了最大年龄:
app.use(express.static('build', { maxAge: '365d' }));
Run Code Online (Sandbox Code Playgroud)
问题是index.html 文件也在build 文件夹中。这意味着它会在客户端缓存 365 天......
因此,即使我对 js 和 css 包进行了缓存清除,index.html 文件始终是相同的,因此由于旧的哈希值,这些包保持不变。
我现在已将服务器更改为使用 Etag:
app.use(express.static('build', { etag: true }));
Run Code Online (Sandbox Code Playgroud)
这很好用。
问题是我将其投入生产,现在所有用户都有一个过时的index.html。
我使用 Node Express 和 AWS 应用程序负载均衡器。
发送index.html的端点如下所示:
app.get('*', (request, response) => {
response.sendFile(path.join(__dirname, 'build/index.html'));
});
Run Code Online (Sandbox Code Playgroud)
有人有聪明的解决方案吗?
我研究过使用这样的 php 文件:https://github.com/facebook/create-react-app/issues/1910。这是最好的解决方案吗?
我遇到了一个相关的问题,部署后,用户需要在某些浏览器中进行硬刷新。这是由于盲目缓存所有构建文件,包括index.html。
我们是这样解决的:
app
.use(express.static(BUILD, {
maxAge: '30 days',
setHeaders: (res, path) => {
if (express.static.mime.lookup(path) === 'text/html') {
// Skip cache on html to load new builds.
res.setHeader('Cache-Control', 'no-cache');
}
}
}))
.get('/*', (req, res) => res.sendFile(HTML));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3245 次 |
| 最近记录: |