iis 缓存 静态文件_强制更新IIS中的缓存JavaScript文件

本文介绍如何强制客户端更新浏览器缓存的JavaScript文件。简单的方法是更改文件,因为浏览器会根据文件的修改时间和ETag来决定是否重新获取。通过IIS设置和文件的Last-Modified头,可以实现客户端缓存的高效管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iis 缓存 静态文件

iis 缓存 静态文件

Javascriptcaching

This might seem obvious to some folks, but to others it's not, so it's worth mentioning. Regardless, it's a good example of a "white box" attitude. Don't assume. Always assert your assumptions with good tests.

这在某些人看来似乎很明显,但在其他人看来却并非如此,因此值得一提。 无论如何,这是“白盒子”态度的一个很好的例子。 不要假设始终通过良好的测试来证明您的假设。

A client wanted to know how to 'force' a client to update some javascript that the browser had cached. The easy answer is "change the file."

客户端想知道如何“强制”客户端更新浏览器已缓存的一些JavaScript。 简单的答案是“更改文件”。

Here's what happens with a single HTML file and a single JavaScript file, running locally on my machine. The main directory is set to "Expire Immediately" via IIS's properties dialog. That means "keep it fresh."

这是在我的计算机上本地运行的单个HTML文件和单个JavaScript文件发生的情况。 通过IIS的属性对话框,主目录设置为“立即过期”。 那意味着“保持新鲜”。

Underneath the main directory is a directory called /js that is set to expire in 7 days, as seen at right.

如右图所示,主目录下面是一个名为/ js的目录,该目录设置为在7天后到期。

Here's an abridged HTTP Header view (via ieHttpHeaders) after hitting the page for the first time ever important stuff in bold.

这是有史以来第一次以粗体显示重要内容后的HTTP Header视图(通过ieHttpHeaders )。

GET /javascriptcachingtest/default.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)

GET /javascriptcachingtest/default.htm HTTP / 1.1 用户代理:Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)

HTTP/1.1 200 OKServer: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
Cache-Control: no-cache
Expires: Fri, 12 May 2006 19:03:59 GMT
Date: Fri, 12 May 2006 19:03:59 GMT
Content-Type: text/html
Last-Modified: Fri, 12 May 2006 18:53:33 GMT
ETag: "b01be5ef575c61:df3"
Content-Length: 115

HTTP / 1.1 200 OK 伺服器:Microsoft-IIS / 5.1 X-Powered-by:ASP.NET 缓存控制:无缓存过期:格林尼治标准时间2006年5月12日19:03:59 日期:2006年5月12日星期五19:03:59 GMT 内容类型:text / html 上次修改时间:2006年5月12日,星期五,格林尼治标准时间ETag:“ b01be5ef575c61:df3” 内容长度:115

GET /javascriptcachingtest/js/test.js HTTP/1.1Referer: http://localhost/javascriptcachingtest/default.htmUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)Connection: Keep-Alive

GET /javascriptcachingtest/js/test.js HTTP / 1.1引用者: http://localhost/javascriptcachingtest/default.htm用户代理:Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322 ; .NET CLR 2.0.50727; InfoPath.1)连接:保持活动

HTTP/1.1 200 OKServer: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
Cache-Control: max-age=604800
Expires: Fri, 19 May 2006 19:03:59 GMT
Date: Fri, 12 May 2006 19:03:59 GMT
Content-Type: application/x-javascript
Last-Modified: Fri, 12 May 2006 18:54:28 GMT
ETag: "50b1c1d4f775c61:df3"Content-Length: 151

HTTP / 1.1 200 OK 伺服器:Microsoft-IIS / 5.1 X-Powered-by:ASP.NET 快取控制:max-age = 604800 过期:格林尼治标准时间2006年5月19日19:03:59 日期:2006年5月12日星期五19:03:59 GMT 内容类型:application / x-javascript 上次修改时间:2006年5月12日,星期五,格林尼治标准时间ETag:“ 50b1c1d4f775c61:df3” 内容长度:151

Note that both files were returned with HTTP 200 OK and the Javascript file had a Last-Modified header returned and an Expires date a week in the future. Now I'll hit F5 to refresh.

请注意,两个文件都以HTTP 200 OK返回,并且Javascript文件返回了Last-Modified头,并且在未来一周的到期日期。 现在我按F5刷新。

GET /javascriptcachingtest/default.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)

GET /javascriptcachingtest/default.htm HTTP / 1.1 用户代理:Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
Cache-Control: no-cache
Expires: Fri, 12 May 2006 19:11:30 GMT
Date: Fri, 12 May 2006 19:11:30 GMT
Content-Type: text/html
Last-Modified: Fri, 12 May 2006 18:53:33 GMT
ETag: "b01be5ef575c61:df3"
Content-Length: 115

HTTP / 1.1 200 OK 伺服器:Microsoft-IIS / 5.1 X-Powered-by:ASP.NET 缓存控制:无缓存过期:格林尼治标准时间2006年5月12日19:11:30 日期:2006年5月12日星期五19:11:30 GMT 内容类型:text / html 上次修改时间:2006年5月12日,星期五,格林尼治标准时间ETag:“ b01be5ef575c61:df3” 内容长度:115

GET /javascriptcachingtest/js/test.js HTTP/1.1Referer: http://localhost/javascriptcachingtest/default.htmIf-Modified-Since: Fri, 12 May 2006 19:03:59 GMTIf-None-Match: W/"50b1c1d4f775c61:df3"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)Host: localhostConnection: Keep-Alive

GET /javascriptcachingtest/js/test.js HTTP / 1.1Referer: http://localhost/javascriptcachingtest/default.htm If-Modified-Since:Fri,12 May 2006 19:03:59 GMTIf-None-Match:W /“ 50b1c1d4f775c61:df3“用户代理:Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)主机:localhost连接:Keep-Alive

HTTP/1.1 304 Not ModifiedServer: Microsoft-IIS/5.1
Date: Fri, 19 May 2006 19:03:59 GMT
X-Powered-By: ASP.NET
Cache-Control: max-age=604800
Expires: Fri, 19 May 2006 19:03:59 GMT
ETag: "50b1c1d4f775c61:df3"Content-Length: 0

HTTP / 1.1 304未修改服务器:Microsoft-IIS / 5.1 日期:2006年5月19日星期五19:03:59 GMT X-Powered-by:ASP.NET 快取控制:max-age = 604800 过期:格林尼治标准时间2006年5月19日19:03:59 ETag:“ 50b1c1d4f775c61:df3” 内容长度:0

Note that the JavaScript file wasn't return (Content-Length: 0), the ETag is the same, and instead a 304 Not Modified was returned. This is the essense of client side caching and is something you should be exploiting (Sadly, fewer folks than you think do this) to get good throughput, efficiency and save on bandwidth costs.

请注意,未返回JavaScript文件(Content-Length:0),ETag相同,而是返回了304 Not Modified。 这是客户端缓存的本质,是您应该利用的东西(可悲的是,比您想象的要少的人)来获得良好的吞吐量,效率并节省带宽成本。

Now, I'll "touch" the file - change it's modified date using the touch.exe I've got in my c:\utils folder (from http://unxutils.sourceforge.net/). Of course, there are other ways to do this, but you get the idea.

现在,我将“触摸”文件-使用c:\​​ utils文件夹(来自http://unxutils.sourceforge.net/ )中获得的touch.exe更改文件的修改日期。 当然,还有其他方法可以做到这一点,但是您可以理解。

We've touched the file, so we'll hit F5 again to refresh:

我们已经触摸了文件,因此我们将再次按F5刷新:

GET /javascriptcachingtest/default.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)

GET /javascriptcachingtest/default.htm HTTP / 1.1 用户代理:Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
Cache-Control: no-cache
Expires: Fri, 12 May 2006 19:11:30 GMT
Date: Fri, 12 May 2006 19:11:30 GMT
Content-Type: text/html
Last-Modified: Fri, 12 May 2006 18:53:33 GMT
ETag: "b01be5ef575c61:df3"
Content-Length: 115

HTTP / 1.1 200 OK 伺服器:Microsoft-IIS / 5.1 X-Powered-by:ASP.NET 缓存控制:无缓存过期:格林尼治标准时间2006年5月12日19:11:30 日期:2006年5月12日星期五19:11:30 GMT 内容类型:text / html 上次修改时间:2006年5月12日,星期五,格林尼治标准时间ETag:“ b01be5ef575c61:df3” 内容长度:115

GET /javascriptcachingtest/js/test.js HTTP/1.1Accept: */*Referer: http://localhost/javascriptcachingtest/default.htmAccept-Language: en-usAccept-Encoding: gzip, deflateIf-Modified-Since: Fri, 12 May 2006 19:03:59 GMTIf-None-Match: "50b1c1d4f775c61:df3"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)Host: localhostConnection: Keep-Alive

GET /javascriptcachingtest/js/test.js HTTP / 1.1接受:* / *引荐来源: http://localhost/javascriptcachingtest/default.htm接受语言:en-us接受编码:gzip,压缩If-Modified-Since:星期五,2006年5月12日19:03:59 GMTIf-None-Match:“ 50b1c1d4f775c61:df3”用户代理:Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0 .50727; InfoPath.1)主机:localhost连接:Keep-Alive

HTTP/1.1 200 OKServer: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
Cache-Control: max-age=604800
Expires: Fri, 19 May 2006 19:11:30 GMT
Date: Fri, 12 May 2006 19:11:30 GMT
Content-Type: application/x-javascript
Last-Modified: Fri, 12 May 2006 19:11:29 GMT
ETag: W/"804647dff775c61:df3"
Content-Length: 151

HTTP / 1.1 200 OK 伺服器:Microsoft-IIS / 5.1 X-Powered-by:ASP.NET 快取控制:max-age = 604800 过期:格林尼治标准时间2006年5月19日19:11:30 日期:2006年5月12日星期五19:11:30 GMT 内容类型:application / x-javascript 上次修改时间:2006年5月12日,星期五,格林尼治标准时间ETag:W /“ 804647dff775c61:df3” 内容长度:151

Notice that the browser asks for the JavaScript not only "by name" but also by date, and by ETag, a mostly unique identifier. The IIS server responds with an HTTP 200 OK, returning the freshly changed (in IIS's mind) file along with a new ETag and a new Last-Modified date.

请注意,浏览器不仅要求JavaScript“按名称”,还要求按日期和ETag(主要是唯一的标识符)。 IIS服务器以HTTP 200 OK响应,返回刚更改过的文件(在IIS的思想中)以及新的ETag和新的上次修改日期。

As an aside, DasBlog does a pretty good job in its RSS Syndication Code of programmatically managing If-Modified-Since behavior. Remember that ASP.NET's <%OutputCache%> is SERVER-SIDE. It's not what we've just seen here. If you want this kind of behavior in your ASP.NET code, you'll need to do it manually in code. I'll post examples of that later.

顺便说一句DasBlog的RSS联合代码在以编程方式管理If-Modified-Since行为方面做得很好。 请记住,ASP.NET的<%OutputCache%>是SERVER-SIDE。 这不是我们在这里看到的。 如果要在ASP.NET代码中进行这种行为,则需要在代码中手动进行。 我将在稍后发布示例。

翻译自: https://www.hanselman.com/blog/forcing-an-update-of-a-cached-javascript-file-in-iis

iis 缓存 静态文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值