caches_actionで携帯で画像が表示されない
環境
クライアント:携帯
サーバ
- apache
- mongrel
- mongrel_cl
uster - rails
+actionキャッシュを使用している。
1 class BlobsController < Applicatio nControlle r 2 layout nil 3 caches_act ion :show 4 session :off, :only => :show
状況
cacheがないページにアクセスすると、正しく表示される。どうやらcacheを返したときに、(携帯で)うまく表示できない様子。
PCではcacheがあっても、なくても正しく表示される:firefox。PCのブラウザは賢い。
ログを確認:cacheがあるのでhaltedしたというメッセージ
これはcacheがあるので、ActionCach
1 ProcessingBlobsContr oller#show (for 219.125.14 8.54 at 2008-07-02 10:49:02) [GET] 2 Parameters : {"digest"=>"d8de32d0ad 4bcf08016c 6cedd31d42 6b", "action"=>"show", "controller "=>"blobs", "formats"=>[]} 3 Filter chain halted as [#<ActionCont roller::Ca ching::Act ions::Acti onCacheFil ter:0xb72d 10c4 @actions=#<Set: {:show}>, @options={}>] rendered_o r_redirect ed. 4 Filter chain halted as [#<ActionCont roller::Fi lters::Cla ssMethods: :ProcWithC allFilter: 0xb72d0de0 @filter=#<Proc:0xb75 1d184@/usr/local /lib/ruby/ gems/1.8/g ems/action pack-2.0.2 /lib/actio n_controll er/filters .rb:657>>] did_not_yi eld. 5 Completed in 0.00035 (2833 reqs/sec) | DB: 0.00000 (0%) | 200 OK [http://mmm uji.s21g.c om/blobs/d 8de32d0ad4 bcf08016c6 cedd31d426 b]
詳しく状況を確認:Content-Ty peが正しくない
サーバ、クライアントの両方のキャッシュを削除して、サイドブラウザからアクセス、画像を表示してfirefubのNetタブで確認すると、Content-Ty
瀧内さんからもらったアドバイス:caches_act
Content-Ty peを設定するいくつかの方法
- mongrelで設定
- plugin(cache_fuなど)を使う
- railsで設定:config/ini
tializers/ mime_types .rb - 表示時に設定(これはaction_cac
hesはヘッダーをキャッシュしないので、キャッシュ表示時には意味がない)
結論:正しいContent-Ty peは(特に携帯には)大事
上記の方法以外にも、mongrelをやめるという選択肢もあるのですが、とりあえず、railsでヘッダを設定するのはmongrelをやめても邪魔にならない設定であるのでそれをやってみました。
正しくContent-Ty
後mime_types
Refs
http://rub
http://www