一个奇怪的js问题:
相关文档: http://www.akmattie.net/blog/2007/11/10/javascript-frames-ie-6-gzip-no-cache-header-trouble/
问题描述:在页面b.html中存在如下
其中b.do返回的http header如下:
HTTP/1.1 200 OK
Server: nginx/0.6.34
Date: Fri, 27 Feb 2009 02:59:41 GMT
Content-Type: text/html;charset=GBK
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Fri, 27 Feb 1970 02:59:41 GMT
Vary: Accept-Encoding
Content-Encoding: gzip
直接在地址栏输入b.html或者刷新b.html, b.do这个js 都是可以正常加载并执行的
而现在的情况是在 a.html中点击一个链接当前页面刷新为b.html,这个时候b.do可以加载,但是不执行
解决问题思路如下:
1. b.do返回的是header中content-type是:text/html改为
Content-Type: text/javascript;charset=GBK 或者Content-Type: application/x-javascript;charset=GBK
fail
2. 放一个b.js文件,返回header如下:
HTTP/1.1 200 OK
Server: nginx/0.6.34
Date: Fri, 27 Feb 2009 02:50:39 GMT
Content-Type: application/x-javascript
Last-Modified: Fri, 13 Feb 2009 09:22:41 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Fri, 06 Mar 2009 02:50:39 GMT
Cache-Control: max-age=604800
Content-Encoding: gzip
这个可以正常加载并执行。
应该gzip和Cache-Control、Expires的区别。
2.1. 将Expires header去掉, fail
2.2.将gzip压缩去掉, succ
2.3. 去除Cache-Control header或者设置Cache-Control为空, succ
不过这个存在问题,因为b.do是个动态请求不能被缓存的, b.do?rand 加个随机数
Content with "Content-Encoding: gzip" Is Always Cached Although You Use "Cache-Control: no-cache"