Welcome to WebmasterWorld Guest from 54.167.185.18

Forum Moderators: incrediBILL

Message Too Old, No Replies

How to prevent server request when using cached image

   
8:50 pm on Jan 20, 2009 (gmt 0)

10+ Year Member



I am using YSlow to analyse my site and noticed that even when the browser (Firefox) has access to a cached file, it is making a server request which returns a 304 header, thus forcing it to use that cached image. Obviously, if the server response is slow, this adds to the time required to display the image. For a dynamically-generated image (e.g. as a result of a databse lookup), this adds to server load and impacts on site responsiveness.

Is there a way to prevent the browser making that server request every time? I am using Apache and PHP. Could this be a Firefox-specific or YSlow-specific issue?

Here is a sample header sent by the PHP script when it serves the image:

=======
Date: Tue, 20 Jan 2009 18:17:38 GMT
Server: Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8b mod_bwlimited/1.4 PHP/5.2.6
X-Powered-By: PHP/5.2.6
Cache-Control: max-age=86400
Pragma: cache
Expires: Tue, 20 Jan 2009 23:59:59 GMT
Last-Modified: Tue, 20 Jan 2009 00:00:01 GMT
Content-Length: 2109
Content-Type: image/jpeg

200 OK

=======

I have noticed that this server request happens with "normal" images too, and here is a sample header:

=======
Date: Tue, 20 Jan 2009 18:17:38 GMT
Server: Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8b mod_bwlimited/1.4 PHP/5.2.6
Last-Modified: Thu, 25 Mar 2004 12:47:50 GMT
Etag: "2d73cd-4969-e06a980"
Accept-Ranges: bytes
Content-Length: 18793
Cache-Control: max-age=2592000
Expires: Thu, 19 Feb 2009 18:17:38 GMT
Content-Type: image/gif

200 OK

=======

9:09 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



When and how often to check with the server for a newer version of any content is under the user's control, not the server's. As far as I know there's nothing you can do to limit repeated requests from user who have set their browser preferences to check "every time".
9:12 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Try using the "Live HTTP Headers" add-on for Firefox/Mozilla, and see if Firefox still sends the cached-image requests. Since YSlow is intended to demonstrate the worst-case scenario of a visitor who has never before visited your site and therefore won't have any of your images cached locally, it may in fact be forcing these requests to your server. Live Headers will report the headers sent by Firefox and those returned by your server, but won't otherwise affect the transaction.

Looking at your server headers, I suspect you'll find that all is well, and that this cache behavior is a result of the tool you're using -- In other words, either incorrect expectations while using the YSlow tool, or an invalid test case, depending on how you want to look at it...

Jim

3:04 am on Jan 25, 2009 (gmt 0)

10+ Year Member



Thanks for this. It was my mistake... YSlow requests the pages every time you ask for a reload (which makes sense). If you just visit the same page again (without asking for a reload) it displays files from cache.