我正在使用GWT开发Web应用程序,尽管Web服务器发送了文件的新副本,但在浏览器中缓存app.nocache.js
文件却遇到了一个疯狂的问题!
我正在使用Eclipse编译应用程序,该应用程序可以在开发模式下工作。为了测试生产模式,我有一台虚拟机(Oracle VirtualBox),其主机(Windows 7)上运行了一个Ubuntu guest OS。我在VM中运行lighttpd Web服务器。 VM正在共享我项目的war目录,而Web服务器正在提供该目录。
我使用的是Chrome浏览器,但在Firefox中也会发生相同的情况。
这是场景:
6E89D5C912DD8F3F806083C8AA626B83.cache.html
(不存在)(404 not found
)。 app.nocache.js
已从Web服务器重新加载(200 OK),因为服务器上的文件比浏览器缓存新。我验证了服务器返回的新文件的文件大小和时间戳是正确的。 (这是有关服务器HTTP响应的Chrome浏览器报告的信息)app.nocache.js
,则javascript是指6E89D5C912DD8F3F806083C8AA626B83.cache.html
!也就是说,即使Web服务器发送了新的app.nocache.js
,浏览器似乎也忽略了它,并继续使用其缓存副本! app.nocache.js
已被覆盖并具有新的时间戳。 app.nocache.js
发送了200 OK响应。 6E89D5C912DD8F3F806083C8AA626B83.cache.html
并失败。浏览器仍在使用app.nocache.js
的旧缓存副本。 6E89D5C912DD8F3F806083C8AA626B83.cache.html
(通过find和grep)怎么了?为什么即使服务器正在向它发送新副本,浏览器仍会缓存此
nocache.js
文件?这是单击浏览器中的重新加载时HTTP请求/响应 header 的副本。在此跟踪中,自上一次GET以来未重新编译服务器内容(但请注意,nocache.js的缓存版本仍然错误!):
Request URL:http://192.168.2.4/xbts_ui/xbts_ui.nocache.js
Request Method:GET
Status Code:304 Not Modified
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:192.168.2.4
If-Modified-Since:Thu, 25 Oct 2012 17:55:26 GMT
If-None-Match:"2881105249"
Referer:http://192.168.2.4/XBTS_ui.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
Response Headersview source
Accept-Ranges:bytes
Content-Type:text/javascript
Date:Thu, 25 Oct 2012 20:27:55 GMT
ETag:"2881105249"
Last-Modified:Thu, 25 Oct 2012 17:55:26 GMT
Server:lighttpd/1.4.31
最佳答案
避免浏览器缓存的最佳方法是将到期时间设置为现在,并添加max-age = 0和必须重新验证的控件。
这是我与apache-httpd一起使用的配置
ExpiresActive on
<LocationMatch "nocache">
ExpiresDefault "now"
Header set Cache-Control "public, max-age=0, must-revalidate"
</LocationMatch>
<LocationMatch "\.cache\.">
ExpiresDefault "now plus 1 year"
</LocationMatch>
您的lighthttpd配置应为
server.modules = (
"mod_expire",
"mod_setenv",
)
...
$HTTP["url"] =~ "\.nocache\." {
setenv.add-response-header = ( "Cache-Control" => "public, max-age=0, must-revalidate" )
expire.url = ( "" => "access plus 0 days" )
}
$HTTP["url"] =~ "\.cache\." {
expire.url = ( "" => "access plus 1 years" )
}
关于gwt - 如何使浏览器停止缓存GWT nocache.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13074926/