简体   繁体   English

AngularJs强制浏览器清除缓存

[英]AngularJs force browser to clear cache

My angular application is constantly changing these days because our team runs rapid updates right now. 我的angular应用程序现在不断变化,因为我们的团队现在正在快速更新。

Because of cache our clients does not always have the newest version of our code. 由于缓存,我们的客户并不总是拥有我们代码的最新版本。

So is there a way in angular to force the browser to clear cache? 那么有一种angular来强制浏览器清除缓存吗?

You can use a very simple solution that consist in append a hash to your scripts files. 您可以使用一个非常简单的解决方案,包括在脚本文件中附加哈希值。 Each time your App is deployed you serve your files with a different hash automatically via a gulp/grunt task. 每次部署应用程序时,您都会通过gulp / grunt任务自动使用不同的哈希值来提供文件。 As an example you can use gulp-rev . 举个例子,你可以使用gulp-rev I use this technique in all my projects and works just fine, this automatized in your build/deploy process can be a solution for all your projects. 我在所有项目中使用这种技术并且工作正常,在构建/部署过程中自动化可以成为所有项目的解决方案。

Yeoman generator for AngularJS generator-gulp-angular (this was my generator of choice) use this solution to ensure that the browser load the new optimazed file and not the old one in cache. 用于AngularJS generator-gulp-angular的 Yeoman生成 (这是我选择的生成器)使用此解决方案来确保浏览器加载新的优化文件而不是缓存中的旧文件。 Please create a demo project and play with it and you will see it in action. 请创建一个演示项目并使用它,您将看到它正在运行。

As mentioned above the common solution to solve browser cache issues is adding some kind of version token (version number, timestamp, hash and so on) to loaded resource files. 如上所述,解决浏览器缓存问题的常见解决方案是向加载的资源文件添加某种版本标记(版本号,时间戳,哈希等)。 This covers cases when user load page or reload it. 这包括用户加载页面或重新加载页面的情况。 As already told gulp task, WebPack, some backend frameworks as Asp.net MVC and so on support this feature together with bundling, minimization, obfuscation and so on. 正如已经告知gulp任务,WebPack,Asp.net MVC等一些后端框架支持此功能以及捆绑,最小化,混淆等。 It's better to use them resolving another related issues too. 最好还用它们解决另一个相关问题。

But one think they can't resolve it's updating the main page itself and loaded already files when they were changed (deployed) on backend side. 但有人认为他们无法解决它正在更新主页本身并在后端更改(部署)时加载已经存档的文件。 For instance you deploy app while another users work with your single page without reloading it. 例如,您部署应用程序,而其他用户使用您的单个页面而不重新加载它。 Or a user left app open in browser tab and in an hour comes back to this page. 或者用户在浏览器选项卡中打开应用程序,并在一小时内返回此页面。 In this case some loaded already files including main page are old and some on backend side are new ones. 在这种情况下,一些已经加载的已经包含主页的文件是旧的,而在后端的一些是新的。 Also all loaded already files have old references to files which are may not exist in backend but cached in browser. 此外,所有已加载的文件都具有对文件的旧引用,这些文件可能不存在于后端但在浏览器中缓存。 So, in general you have broken application here and it's actually more general problem which Angular can't solve itself. 所以,一般来说,你已经破坏了应用程序,这实际上是Angular无法解决的更普遍的问题。

To solve this you need to notify your user that a new app version exists and they need to reload page or to reload it by force. 要解决此问题,您需要通知您的用户存在新的应用程序版本,他们需要重新加载页面或强制重新加载。 The second approach is not good from user experience perspective. 从用户体验的角度来看,第二种方法并不好。 Let's imagine you are working and at some moment page starts reload itself. 让我们假设您正在工作,并且在某个时刻页面开始重新加载。 Weird, right? 很奇怪,对吗?

In order to notify user about new version you can use websokets message to app about new version, pass version in every response (not a good solution) or pull backend from time to time about a new version (not a good too). 为了通知用户有关新版本的信息,你可以使用websokets消息来关于新版本的应用程序,在每个响应中传递版本(不是一个好的解决方案)或不时拉出关于新版本的后端(也不是很好)。 But they all are not trivial. 但它们都不是微不足道的。 If your app login session is short you can check version while relogin, refreshing auth cookies and so on. 如果您的应用程序登录会话很短,您可以在重新登录时刷新版本,刷新身份验证cookie等等。

So, to solve this problem fully you need to implement files bundling + new version user notification mechanism. 因此,要完全解决这个问题,您需要实现文件捆绑+新版本用户通知机制。

If you are looking for a really simple solution and your IDE is visual studio under C# you can get app build Id and concatenate on your js file url. 如果您正在寻找一个非常简单的解决方案,并且您的IDE是C#下的visual studio,您可以获得app build Id并在您的js文件URL上连接。

First you should activate incrementation for minor versions like that: 首先,你应该为那些次要版本激活增量: 在此输入图像描述

Go to your project properties, under Application on the new window, click Assembly Information and add a "*" to Assembly version (last digit as described on image above). 转到项目属性,在新窗口的Application下,单击Assembly Information并在Assembly版本中添加“*”(如上图所示,最后一位数字)。

After that, add a new property to your codebehind aspx, or web service or webapi, etc... for this piece of code I'm using aspx: 之后,将新属性添加到您的代码隐藏的aspx,或Web服务或webapi等...对于这段代码,我正在使用aspx:

    public string GetVersionApp => System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

Then, call the property through your html and concatenate the value as param on your url file, like this: 然后,通过您的html调用属性并将值作为param连接到您的url文件,如下所示:

<script type="text/javascript" src="App/RequestsList/directives/comment-directive.js?<%=GetVersionApp%>"></script>

With this solution, your files only be reloaded if a new build occurs. 使用此解决方案,只有在发生新构建时才会重新加载文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM