如何防止 Angular 2 站点上的浏览器缓存? [英] How to prevent Browser cache on Angular 2 site?
问题描述
我们目前正在开发一个定期更新的新项目,我们的一位客户每天都在使用该项目.此项目是使用 angular 2 开发的,我们面临缓存问题,即我们的客户没有看到他们机器上的最新更改.
We're currently working on a new project with regular updates that's being used daily by one of our clients. This project is being developed using angular 2 and we're facing cache issues, that is our clients are not seeing the latest changes on their machines.
主要是 js 文件的 html/css 文件似乎得到了正确更新而没有带来太多麻烦.
Mainly the html/css files for the js files seem to get updated properly without giving much trouble.
推荐答案
angular-cli 通过提供构建的 --output-hashing
标志命令(版本 6/7,对于更高版本,请参见此处).示例用法:
angular-cli resolves this by providing an --output-hashing
flag for the build command (versions 6/7, for later versions see here). Example usage:
ng build --output-hashing=all
捆绑&Tree-Shaking 提供了一些细节和上下文.运行 ng help build
,记录标志:
Bundling & Tree-Shaking provides some details and context. Running ng help build
, documents the flag:
--output-hashing=none|all|media|bundles (String)
Define the output filename cache-busting hashing mode.
aliases: -oh <value>, --outputHashing <value>
虽然这仅适用于 angular-cli 的用户,但它工作得非常出色并且不需要任何代码更改或其他工具.
Although this is only applicable to users of angular-cli, it works brilliantly and doesn't require any code changes or additional tooling.
更新
许多评论有帮助并且正确指出这个答案向 .js
文件添加了一个哈希值,但对 <代码>index.html.因此,在 ng build
缓存破坏 .js
文件后,index.html
完全有可能保持缓存.
A number of comments have helpfully and correctly pointed out that this answer adds a hash to the .js
files but does nothing for index.html
. It is therefore entirely possible that index.html
remains cached after ng build
cache busts the .js
files.
此时我将遵循 如何我们是否在所有浏览器中控制网页缓存?
这篇关于如何防止 Angular 2 站点上的浏览器缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!