当 AngularJS 网站中的网站内容发生变化时,如何使 index.html 不缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34851243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:48:38  来源:igfitidea点击:

How to make index.html not to cache when the site contents are changes in AngularJS website?

htmlangularjscachingdeployment

提问by Rongyan Xia

Normally for .jsand .cssfile we will append a version during build like xx.js?v=123, and then after website deploy, we can get the new version of js and CSS. But I don't see a place talking about how to make the index.htmlfile upgrade when website deployment happen. And we do see in IE that the HTML content should have been changed but it still use the old HTML content.

通常for.js.cssfile我们会在构建时附加一个版本 xx.js?v=123,然后在网站部署后,我们可以获得新版本的js和CSS。但是我没有看到一个地方谈论如何index.html在网站部署发生时进行文件升级。我们确实在 IE 中看到 HTML 内容应该已更改,但它仍然使用旧的 HTML 内容。

One solution I find from google is to

我从谷歌找到的一种解决方案是

<meta http-equiv="Cache-control" content="no-cache">

However, I am not sure whether this is the best solution?

但是,我不确定这是否是最佳解决方案?

回答by Shashank Agrawal

Yes, that is the correct way. You have to set the Cache-Controlheader to let the browsers know that they don't have to cache any content for that request.

是的,这是正确的方法。您必须设置Cache-Control标头以让浏览器知道他们不必为该请求缓存任何内容。

<meta http-equiv="Cache-control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">

(Pragma& Cache-Controlis one and the same thing but from the different HTTP specification. See the answer here: Difference between Pragma and Cache-control headers?)

Pragma&Cache-Control是一回事,但来自不同的 HTTP 规范。请参阅此处的答案:Difference between Pragma and Cache-control headers?

See one of the related answer here: How to burst yeoman index.html cache

请参阅此处的相关答案之一:How to burst yeoman index.html cache

回答by Michael

As mentioned in the comments it's really the server configuration you want to be looking at but you haven't mentioned your setup. We run our AngularJS site with a .NET backend using IIS.

正如评论中提到的,它确实是您想要查看的服务器配置,但您没有提到您的设置。我们使用 IIS 使用 .NET 后端运行我们的 AngularJS 站点。

We update our site regularly and have had issues in the past with JS and CSS resources caching but we've solved it through the following settings:

我们会定期更新我们的网站,并且过去在 JS 和 CSS 资源缓存方面遇到过问题,但我们已经通过以下设置解决了这个问题:

Build

建造

We use gulp to build our AngularJS application and as part of that we append a version number to the querystring of our main application CSS and Javascript files that change frequently. For example:

我们使用 gulp 来构建我们的 AngularJS 应用程序,作为其中的一部分,我们将一个版本号附加到我们的主应用程序 CSS 和 Javascript 文件的查询字符串中,这些文件经常更改。例如:

<link href="css/default.min.css?v=2" rel="stylesheet" />

Server Configuration

服务器配置

In the root web.config we specify that we don't want to the index.html to cache by setting the cache-control, Pragmaand Expiresrequest headers as well as the max-age to 0.

在根 web.config 中,我们通过将cache-control,PragmaExpires请求标头以及 max-age 设置为 0来指定我们不想缓存 index.html 。

<location path="index.html">
<system.webServer>
  <staticContent>
    <clientCache cacheControlMode="DisableCache" cacheControlMaxAge="0.00:00:00" />
  </staticContent>
    <httpProtocol>
        <customHeaders>
            <add name="Cache-Control" value="no-cache, no-store, must-revalidate" />
            <add name="Pragma" value="no-cache" />
            <add name="Expires" value="-1" />
        </customHeaders>
    </httpProtocol>  
</system.webServer>
</location>

References:

参考:

回答by Nikhil Mahirrao

By setting contentvalue to 0, browsers will always load the page from the web server.

通过将contentvalue设置为0,浏览器将始终从 Web 服务器加载页面。

<meta http-equiv="expires" content="0">

回答by Kurosh Farsimadan

You can specify IIS from the specific sites advanced settings that preloading is not enabled. Your site performance will however suffer. Also check that the application pool for the specific website has integrated pipeline mode on and .NET CLR version.

您可以从未启用预加载的特定站点高级设置中指定 IIS。但是,您的网站性能会受到影响。还要检查特定网站的应用程序池是否已集成管道模式和 .NET CLR 版本。