Stop images from caching in Rails and browser?

大兔子大兔子 提交于 2021-02-07 08:22:35

问题


I have created a image crop facility that I can click on an image and crop it and it saves the new cropped image over the old one, then redirects back to the original page where the image was show.

But it still shows the old image even after a redirect and doesn't display the new one till I refresh the page.

I have tried just using an image tag and removing the asset timestamp after the image but it still displays the old image and I have also tried adding meta tags to stop browser caching but not working.

How can I solve this without having to do a refresh page?


回答1:


It's probably not the best way, but I've solved this problem in the past by simply appending a timestamp to the image URL using JavaScript:

http://www.mysite.com/img/some_image.jpg?timestamp=12342312412

Next time it loads, the timestamp is set to the current time and the URL is different, so the browser does a GET for the image instead of using the cached version.




回答2:


Are you using the rails image helper tag: image_tag in your views?

Rails automatically appends a timestamp based on the last modified date of the image file which is handy because it should change when you do your crop and write out the new image.

More information in the docs: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html



来源:https://stackoverflow.com/questions/1915617/stop-images-from-caching-in-rails-and-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!