简体   繁体   English

Chrome&Expires Header - 图像缓存

[英]Chrome & Expires Header - Image Caching

I have a web application that contains a few hundred small images, and is performing quite badly on load. 我有一个包含几百个小图像的Web应用程序,并且在加载时表现非常糟糕。

To combat this, I would like to cache static files in the browser. 为了解决这个问题,我想在浏览器中缓存静态文件。

Using a servlet filter on Tomcat 7 , I now set the expires header correctly on static files, and can see that this is returned to Chrome: 在Tomcat 7上使用servlet过滤器 ,我现在在静态文件上正确设置了expires标头,并且可以看到它返回给Chrome:

Accept-Ranges:bytes
Cache-Control:max-age=3600
Content-Length:40284
Content-Type:text/css
Date:Sat, 14 Apr 2012 09:37:04 GMT
ETag:W/"40284-1333964814000"
**Expires:Sat, 14 Apr 2012 10:37:05 GMT**
Last-Modified:Mon, 09 Apr 2012 09:46:54 GMT
Server:Apache-Coyote/1.1

However, I notice that Chrome is still doing a round trip to the server for each static resource on reloads, sending an if-modified header and getting a correct 304 Not Modified response from Tomcat. 但是,我注意到Chrome仍然在重新加载时为每个静态资源进行服务器往返,发送if-modified标头并从Tomcat获取正确的304 Not Modified响应。

Is there any way to make Chrome avoid these 100+ requests to the server until the expiry has genuinely passed? 有没有办法让Chrome在服务器真正通过到期之前避免这些100多个请求到服务器?

There are 3 ways of loading a page - 有3种加载页面的方法 -

  1. Putting the url in the address bar and pressing enter which is equivalent to navigating from a hyper link (Default browsing behaviour). 将url放在地址栏中并按Enter键,这相当于从超链接导航(默认浏览行为)。 This will honour the Expires headers and will first check the cache of the static content to be valid and then if the Expires header time is in future it will load it directly from the cache. 这将遵循Expires标头,并将首先检查静态内容的缓存是否有效,然后如果Expires标头时间是将来它将直接从缓存加载它。 In this case the browser will make no request at all to the server. 在这种情况下,浏览器根本不会向服务器发出任何请求。 In case the cached content is in-valid it will make a request to the server. 如果缓存的内容无效,它将向服务器发出请求。

  2. Pressing f5 to refresh the page. 按f5刷新页面。 This would basically send a if-modified header to the server and verify if the content has changed. 这基本上会向服务器发送if-modified标头并验证内容是否已更改。 If it has changed you would get a 200 response else if not then a 304 response. 如果它已经改变,你会得到200响应,否则304响应。 In both cases the image is not loaded on the page until a response is received from the server. 在这两种情况下,在从服务器收到响应之前,图像不会加载到页面上。

  3. Pressing ctrl+f5 which would forcefully clear all the cache and reload all the images. 按ctrl + f5将强制清除所有缓存并重新加载所有图像。 It will not spend time in verifying if the images have changed or not using the headers. 它不会花时间验证图像是否已使用标题更改。

I guess the behaviour you are expecting is the first kind. 我想你期待的行为是第一种。 The only thing that you should be looking at is the way you are loading the page. 您应该关注的唯一事情是您加载页面的方式。 Normally people are not going to press f5 or ctrl+f5 thus your static content will not be re-validated every time. 通常人们不会按f5或ctrl + f5,因此每次都不会重新验证静态内容。 It will forcefully clear the cache and reload every static item on the page. 它将强制清除缓存并重新加载页面上的每个静态项。

In short just remember - reload the page by pressing enter in the address bar instead. 简而言之,请记住 - 通过在地址栏中输入来重新加载页面。 The browser will honour the headers that you have provided. 浏览器将遵循您提供的标头。 This is not specific to chrome, its a W3C standard. 这不是特定于chrome,它是W3C标准。

Be carefull when you are testing. 在测试时要小心。 I noticed that in Chrome version 20 if I hit F5 to reload the page then in the network panel I see new requests. 我注意到在Chrome版本20中,如果我按F5重新加载页面,那么在网络面板中我会看到新的请求。 Hoewer if I place the cursor to the title bar, after the current page url, and hit enter, I get resources from cache, whitch header was set to cache. 如果我将光标放在标题栏上,在当前页面网址之后,然后点击回车,我从缓存中获取资源,将whitch标头设置为缓存。

Also a good reading: 也是一个很好的阅读:

http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/ http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/

Assuming you have ruled out the various gotchas that have already been suggested, I found that Google Chrome can ignore the Cache-Control directive unless it includes public , and that it has to be first. 假设你已经排除了已经提出的各种陷阱,我发现Google Chrome可以忽略Cache-Control指令,除非它包含public ,并且它必须是第一个。 For example: 例如:

Cache-Control: public, max-age=3600

In my experiments I also removed ETags from the server response, so that could be a factor, but I didn't go back and check. 在我的实验中,我还从服务器响应中删除了ETag,因此这可能是一个因素,但我没有回去检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM