2011-02-25
■[rails] image_tagで付加される画像のタイムスタンプを、production環境でも画像ファイルが更新されたタイミングで更新されるようにする
image_tagで画像を表示した際に、srcの後ろに付くタイムスタンプは
基本的には File.mtime(image_path) なので、画像ファイルの最終更新時刻です。
image_tag 'rails.png' #=> <img src="/images/rails.png?1298610241" alt="Rails" />
なので、developement環境では画像ファイルをtouchしてあげたりすると、タイムスタンプも更新されます。
しかし、production環境ではtouchしても更新されません。(passengerなり、サーバをリスタートすれば更新されます)
なぜこうなるかは、ActionView::Helpers::AssetTagHelper の rails_asset_id メソッドを見るとわかります。
# File actionpack/lib/action_view/helpers/asset_tag_helper.rb def rails_asset_id(source) if asset_id = ENV["RAILS_ASSET_ID"] asset_id else if @@cache_asset_timestamps && (asset_id = @@asset_timestamps_cache[source]) asset_id else path = File.join(ASSETS_DIR, source) asset_id = File.exist?(path) ? File.mtime(path).to_i.to_s : '' if @@cache_asset_timestamps @@asset_timestamps_cache_guard.synchronize do @@asset_timestamps_cache[source] = asset_id end end asset_id end end end
最初1回は、File.mtime(path) をちゃんと取ってくれるのですが、
その際に、@@asset_timestamps_cache[source] = asset_id で、キャッシュして、
以降は、そのキャッシュが使われます。
で、@@cache_asset_timestamps が何かを見ると
# File actionpack/lib/action_view/helpers/asset_tag_helper.rb def self.cache_asset_timestamps @@cache_asset_timestamps end # You can enable or disable the asset tag timestamps cache. # With the cache enabled, the asset tag helper methods will make fewer # expense file system calls. However this prevents you from modifying # any asset files while the server is running. # # ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false def self.cache_asset_timestamps=(value) @@cache_asset_timestamps = value end @@cache_asset_timestamps = true
booleanの値です。
ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false すれば、
毎回 File.mtime(path) してくれるようになります。
なので、initializersとかに、適当なファイルを作って、
# config/intializers/cache_asset_timestamp.rb とか ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
だけ書いておけば、production環境でもtouchしたらタイムスタンプが更新されるようになります。
ちなみに、development環境は、config/environments/development.rb で
config.cache_classes = false
になってると思いますが、cache_classesがfalseだと、
ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
- 17 http://www.google.co.jp/url?sa=t&rct=j&q=rails image_tag&source=web&cd=5&ved=0CDwQFjAE&url=http://d.hatena.ne.jp/favril/20110225/1298614248&ei=skiEToe7G8SAmQX5t8T3Dw&usg=AFQjCNE_FqcX5eNvYbng1MTwvigo7e_Ysw
- 16 http://ezsch.ezweb.ne.jp/search/?query=画像スタンプ&start-index=26&adpage=5&ct=1301&sr=0000&t=20110315024136&filter=1
- 10 http://www.google.co.jp/url?sa=t&source=web&cd=1&ved=0CBkQFjAA&url=http://d.hatena.ne.jp/favril/?sid=c3b4e005c77d7921&of=13&rct=j&q=mobile_filterが効かない&ei=v9q4TfyEEJCEvgP4w_yhAw&usg=AFQjCNFeMfThwbg0
- 8 http://ezsch.ezweb.ne.jp/search/?query=スタンプ画像&start-index=26&adpage=5&ct=1301&sr=0101&t=20110701001823&filter=1
- 8 http://ezsch.ezweb.ne.jp/search/?query=スタンプ+画像+&start-index=26&adpage=5&ct=1301&sr=0000&t=20110615023311&filter=1
- 8 http://ezsch.ezweb.ne.jp/search/?query=画像+スタンプ&start-index=36&adpage=6&ct=1301&sr=0000&t=20110516154220&filter=1
- 8 http://ezsch.ezweb.ne.jp/search/?sr=0101&query=画像にはるスタンプ
- 8 http://www.google.co.jp/search?aq=f&sourceid=chrome&ie=UTF-8&q=rails+image_tag
- 7 http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=Rails+image_tag+パス
- 7 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0CFIQFjAE&url=http://d.hatena.ne.jp/favril/20110225/1298614248&ei=GpeDT7rBK4TqmAX12sjOBw&usg=AFQjCNE_FqcX5eNvYbng1MTwvigo7e_Ysw&sig2=x-ucQuiTNwVI5ZpuFkgrqg