Is there a way to force browsers to refresh/download images?

后端 未结 14 2214
囚心锁ツ
囚心锁ツ 2020-11-29 01:21

I have a problem where users are reporting that their images aren\'t being uploaded and the old ones are still there. On closer inspection the new images are there, they jus

14条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 02:07

    It's tough. You really want images to be cached, but then you don't want to cache them once a new ones available:

    • Using expires header with a date in the past prevents any caching. Bad
    • Adding a "cache busting" parameter ?342038402 can fix the problem, but can also prevent the image ever from being cached, which is not what you want. Bad.
    • Using expires header with a short (lets say 1 hr) expires is better... after an hour the user will see the image, but your web server won't have to serve it every time. Compromise, but what time works? Not really feasible.

    The solution? I can think of two good options:

    • Look into eTags, and your ability to use them. These are designed for this situation. The browser will explicitly ask your server whether the file is up-to-date or not. You can just turn this on in apache if you don't have it aleady.
    • Create a new URL for each new image (and use a far-future expires header). This is what you are working on.

提交回复
热议问题