我有一个 VueJS 应用程序。每当我运行 npm run build
它会创建一组新的 dist/*
文件,但是,当我将它们加载到服务器上(删除旧版本后)并在浏览器中打开页面时,它加载旧版本(我假设来自缓存)。当我刷新页面时,它加载新代码没问题。
这是我的 index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
<link rel="stylesheet" href="/static/css/bootstrap.min.css"/>
</head>
<body>
<div id="app"></div>
</body>
</html>
有没有办法强制它每次都加载新代码或(理想情况下)检查旧文件是否从服务器中消失,然后刷新浏览器?
原文由 ierdna 发布,翻译遵循 CC BY-SA 4.0 许可协议
毫无疑问,这个问题很烦人。必须使用在标头中发送的自定义前端版本端点来解决它。我使用 rails 后端和 Vue + Axios 作为前端。请注意,我不需要服务人员,因此不使用服务人员。
本质上,只要有获取请求并且应用程序的版本已更改(服务器可以通知相同),我就会重新加载
axiosConfig.js
Rails 后端
application_controller.rb
application.rb
(CORS 配置假设 Vue 在端口 8080 上运行)在这里写了一篇详细的文章: https ://blog.francium.tech/vue-js-cache-not-getting-cleared-in-production-on-deploy-656fcc5a85fe