• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

caching problem with my avatar script

Jake Bunce

XenForo moderator
Staff member
#1
Regarding this resource (the file is also attached to this post):

http://xenforo.com/community/resources/avatar-php.634/

I can't get browser caching to work. The avatar images are re-downloaded by the browser on every request. It appears to be the same problem described here:

http://stackoverflow.com/questions/...rn-304-not-modified-if-it-hasnt-been-modified

I have experimented with various headers including:

Cache-Control
Expires
ETag
If-None-Match

I can manually return the 304 status code in the script, but on what condition? I have been unsuccessful in getting any kind of cache validation to work. And I can't arbitrarily return 304 on every request or it won't download on the initial request.

I welcome any ideas.
 

Attachments

Naatan

Well-known member
#3
Try using permanent redirects:

PHP:
header("HTTP/1.1 301 Moved Permanently");
header("Location: ...");
That way it should bypass your php script altogether on consequent requests.
 

digitalpoint

Well-known member
#4
The 304 doesn't matter a whole lot... that's only going to be a response if the browser asks if it's been modified. Ideally you don't even want the browser to ask. The HTTP request to check if it's been modified is the worst part of the request since it sends a bunch of cookies (overhead) and HTTP headers can't be compressed like the normal content can be.

Anyway... If you use a Cache-Control header with the *normal* response, you should be fine... you can essentially tell the browser to not even bother checking if the resource has been updated for xxxx seconds.

I have an avatar system I use for XF (stores them in the database), and I use the following header when it's returned:
PHP:
header('Cache-Control: public, max-age=31536000');
It tells the browser to not even bother checking if the resource has changed until a year from now (and more importantly, it works... no requests are sent to the web server to even check if it's changed).