[英]angular2 disable browser caching
I am using angular2 for web development, and Jenkins for continuous integration, when i release code at the end of every sprint, it gets deployed on CI Server.我使用 angular2 进行 Web 开发,使用 Jenkins 进行持续集成,当我在每个冲刺结束时发布代码时,它会部署在 CI 服务器上。
But, when users load the UI, they do not get the new UI changes by default, they have to clear the cache ( I do not want users to clear the cache or disable there cache just for this UI)但是,当用户加载 UI 时,默认情况下他们不会获得新的 UI 更改,他们必须清除缓存(我不希望用户仅为此 UI 清除缓存或禁用缓存)
How can I handle programatically for the browser to not cache old files and reload the new changes by default (atleast in development)我如何以编程方式处理浏览器不缓存旧文件并默认重新加载新更改(至少在开发中)
Note: I presently set:注意:我目前设置:
import { enableProdMode } from '@angular/core';
enableProdMode();
None of the documentation states this to be the reason and removing it does not help either.没有任何文档说明这是原因,删除它也无济于事。
Two popular ways of accomplishing this "cache busting" are:完成这种“缓存破坏”的两种流行方法是:
<script type="text/javascript" src="my-file.js?v=1.0.0"></script>
When you make some changes to your file you update your request to:当您对文件进行一些更改时,您将请求更新为:
<script type="text/javascript" src="my-file.js?v=1.0.1"></script>
You can use whatever query string you want as long as you change it.只要您更改它,您就可以使用任何您想要的查询字符串。 The browser sees it as a different file, but it should have no effect on what file your server sends as a response.
浏览器将其视为不同的文件,但它应该不会影响您的服务器作为响应发送的文件。
A webpack example config to accomplish this is:完成此操作的 webpack 示例配置是:
output: {
path: 'dist',
publicPath: '/',
filename: 'js/[name].[chunkhash].js'
},
and then use the HtmlWebpackPlugin to auto-generate your index.html with the correct file names injected (with inject: true):然后使用 HtmlWebpackPlugin 自动生成您的 index.html 并注入正确的文件名(inject: true):
plugins: [
new HtmlWebpackPlugin({
filename: '../index.html',
template: './index.html',
inject: true
}), ...
More info on webpack file naming:关于 webpack 文件命名的更多信息:
https://github.com/webpack/docs/wiki/Configuration#output https://github.com/webpack/docs/wiki/Configuration#output
More info on html webpack plugin:有关 html webpack 插件的更多信息:
https://github.com/ampedandwired/html-webpack-plugin#basic-usage https://github.com/ampedandwired/html-webpack-plugin#basic-usage
I fixed it using a special .htaccess file.我使用特殊的 .htaccess 文件修复了它。 Just uncomment the "BROWSER CACHING" part: https://gist.github.com/julianpoemp/bcf277cb56d2420cc53ec630a04a3566
只需取消注释“浏览器缓存”部分: https ://gist.github.com/julianpoemp/bcf277cb56d2420cc53ec630a04a3566
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.