我试图了解http缓存和指纹识别 作品。我已经设置了我的快速服务器来永久缓存资产:
router.use('/public',
express.static(path.join(__dirname, '..', 'public'),
{ maxAge: 864000000 }));
我希望这能永远缓存资产,即使我改变了 文件的内容,因此我需要指纹文件名 破坏缓存。然而;
这是重新加载后静态资产common.js的Google Chrome标头输出
Remote Address:192.168.56.101:3000
Request URL:http://192.168.56.101:3000/public/assets2/scripts/app/common.js
Request Method:GET
Status Code:304 Not Modified
Request Headers
GET /public/assets2/scripts/app/common.js HTTP/1.1
Host: 192.168.56.101:3000
Connection: keep-alive
Cache-Control: max-age=0
Accept: */ *
If-None-Match: W/"ogrxaeWybJBlXMTTr2leWA=="
If-Modified-Since: Fri, 11 Jul 2014 13:46:01 GMT
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Referer: http://192.168.56.101:3000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Response Headers
HTTP/1.1 304 Not Modified
X-Powered-By: Express
Accept-Ranges: bytes
Date: Fri, 11 Jul 2014 13:48:34 GMT
Cache-Control: public, max-age=864000
Last-Modified: Fri, 11 Jul 2014 13:46:01 GMT
ETag: W/"ogrxaeWybJBlXMTTr2leWA=="
Connection: keep-alive
很好,我得到304。 现在我改变common.js的内容并再次重新加载,这是输出:
Remote Address:192.168.56.101:3000
Request URL:http://192.168.56.101:3000/public/assets2/scripts/app/common.js
Request Method:GET
Status Code:200 OK
Request Headers
GET /public/assets2/scripts/app/common.js HTTP/1.1
Host: 192.168.56.101:3000
Connection: keep-alive
Cache-Control: max-age=0
Accept: */ *
If-None-Match: W/"ogrxaeWybJBlXMTTr2leWA=="
If-Modified-Since: Fri, 11 Jul 2014 13:46:01 GMT
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Referer: http://192.168.56.101:3000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Response Headers
HTTP/1.1 200 OK
X-Powered-By: Express
Accept-Ranges: bytes
Date: Fri, 11 Jul 2014 13:50:35 GMT
Cache-Control: public, max-age=864000
Last-Modified: Fri, 11 Jul 2014 13:50:33 GMT
ETag: W/"o65+0J5C8swpsmHMxNPH+w=="
Content-Type: application/javascript
Content-Length: 1908322
Connection: keep-alive
此时,我期待得到304但是 显然服务器检测到更改并发送了200。
所以我不必使用指纹识别。我哪里出错?
答案 0 :(得分:0)
我认为问题出在谷歌浏览器上,显然当我点击重新加载时,或者在网址栏上输入时,Chrome仍会向服务器发送If-None-Match请求,并获得200.我尝试使用Internet Explorer,并且它成功地从缓存服务器而不会访问服务器。我仍然想知道Chrome有什么问题,如何在没有命中服务器的情况下从缓存中提供服务。
答案 1 :(得分:0)
Express通过将其保留在内存中来永久缓存它在服务器端。我的猜测是,快速框架通过检查缓存资源是否已被修改来维护缓存一致性。
使用if-none-match和/或if-modified-since标头发送请求是用户代理的正确行为。 IE正在尝试通过跳过网络往返来尝试优化,这可能导致页面加载不正确。
您需要做的是使用指纹识别 - 为每个已修改的资源分配新的通用名称 - 或者对服务器如何提供资源进行更低级别的控制,即自己解析网址并定义有关响应如何的规则形成,在你的案件中回复304。