还有一个解决方案:nginx配置实现,没研究过:https://github.com/ant-design/ant-design-pro/issues/1365#issuecomment-384496088解决什么问题?spa项目上线后,在有效期内登录的用户可以立即使用新上线的资源。原理:问:为什么新上线的资源不能马上访问?答:因为设置了浏览器缓存;如果不刷新,仍然访问缓存的静态文件。问题:刷新浏览器后,可以使用新启动的资源,请问Vue项目中是哪个文件在工作?答:归根结底刷新的是dist/index.html文件,指的是manifest文件(dist/static/js/manifest.[chunkhash].js,里面包含路由和打包js的对应关系文件);refreshdist/index.html文件创建后,点击路由时,会根据新的manifest文件按需加载对应的js文件。实现步骤:打包时生成一个json文件:static/json/build_str.json在localStorage中存储值:build_str每次路由切换时,从接口中获取新打包的json中的字符串,与localStorage中存储的最后一次打包的字符进行比较字符串,不同字符串不同时刷新。该技术方案的好处:后端不提供接口,前端请求打包好的json文件速度快,json文件请求时间短,需要对包字符串进行判断,添加代码,请求Vue项目代码修改:1、在对应目录下,新建文件:static/json/build_str.json2、build/build.js修改://将当前写入时间戳更改为json文件letjson_obj={"build_str":newDate().getTime().toString()}fs.writeFile(path.resolve(__dirname,'../static/json/build_str.json'),JSON.stringify(json_obj),function(err){if(err){returnconsole.error(err);}console.log("将打包好的字符串写入文件:static/json/build_str.json,成功!");realBuild()})3.src/main.js修改:router.beforeEach((to,from,next)=>{axios.get('/static/json/build_str.json?v='+newDate().getTime().toString()).then(res=>{letnewBuildStr=res.data.build_strletoldBuildStr=localStorage.getItem('build_str')||''if(oldBuildStr!==newBuildStr){console.log('autorefresh')localStorage.setItem('build_str',newBuildStr)location.reload()}})next()})项目演示:VueCLI2.x版本:https://github.com/cag2050/vue_cli_2.x_webpack_3_upgrade_4VueCLI高版本:https://github.com/cag2050/spa_deploy_refresh
