Question

I'm having trouble with a few images on a site that I'm developing. Some images are being cached correctly and their headers seem correct (strong etags, correct last-modified). But other images have weak etags (preceded with W/) and a last-modified header that is always equal to the current time. Does anyone know how I can resolve this issue? I've looked around quite a bit and I can't seem to find anything that works. The image that isn't being cached is a background image and it causes a background flash on every page reload, which is annoying.

Headers:

Accept-Ranges:bytes
Connection:Keep-Alive
Content-Length:6879
Content-Type:image/jpeg
Date:Wed, 15 Dec 2010 07:41:44 GMT
Etag:W/"4e863ea-1adf-49772cd85b340"
Keep-Alive:timeout=15, max=98
Last-Modified:Wed, 15 Dec 2010 07:41:44 GMT
Server:Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.6 with Suhosin-Patch

Thank you in advance for any help. I'm not an expert in this sort of stuff, so I apologize if I'm missing something obvious.

Was it helpful?

Solution

If you want the ensure that your images are cached you can not rely on validation headers (last-modified, etag). For ensuring caching you need to use Expires or Cache-control. Validation is used only after cache expiration mechanism says that content is stale. If you do not use Expires or Cache-control headers, content freshness is not defined and depends only on the specific implementation of the browser so in your case it can behave unexpectedly.

OTHER TIPS

I'm not sure what changed, but it looks like the problem is gone.

Headers:

Accept-Ranges:bytes
Connection:Keep-Alive
Content-Length:6879
Content-Type:image/jpeg
Date:Wed, 15 Dec 2010 16:46:14 GMT
Etag:"4e863ea-1adf-49772cd85b340"
Keep-Alive:timeout=15, max=99
Last-Modified:Wed, 15 Dec 2010 13:20:37 GMT
Server:Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.6 with Suhosin-Patch
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top