環境

クライアント:携帯
サーバ

  • apache
  • mongrel
  • mongrel_cluster
  • rails

+actionキャッシュを使用している。

   1  class BlobsController < ApplicationController
   2    layout nil
   3    caches_action :show
   4    session :off, :only => :show

状況

cacheがないページにアクセスすると、正しく表示される。どうやらcacheを返したときに、(携帯で)うまく表示できない様子。
PCではcacheがあっても、なくても正しく表示される:firefox。PCのブラウザは賢い。

ログを確認:cacheがあるのでhaltedしたというメッセージ

これはcacheがあるので、ActionCacheFilterを中止して、cacheを返す時のメッセージ。なのでエラーではない。

   1  Processing BlobsController#show (for 219.125.148.54 at 2008-07-02 10:49:02) [GET]
   2    Parameters: {"digest"=>"d8de32d0ad4bcf08016c6cedd31d426b", "action"=>"show", "controller"=>"blobs", "formats"=>[]}
   3  Filter chain halted as [#<ActionController::Caching::Actions::ActionCacheFilter:0xb72d10c4 @actions=#<Set: {:show}>, @options={}>] rendered_or_redirected.
   4  Filter chain halted as [#<ActionController::Filters::ClassMethods::ProcWithCallFilter:0xb72d0de0 @filter=#<Proc:0xb751d184@/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:657>>] did_not_yield.
   5  Completed in 0.00035 (2833 reqs/sec) | DB: 0.00000 (0%) | 200 OK [http://mmmuji.s21g.com/blobs/d8de32d0ad4bcf08016c6cedd31d426b]

詳しく状況を確認:Content-Typeが正しくない

サーバ、クライアントの両方のキャッシュを削除して、サイドブラウザからアクセス、画像を表示してfirefubのNetタブで確認すると、Content-Type: jpg; charset=utf8となっている。
瀧内さんからもらったアドバイス:caches_actionはヘッダーをキャッシュしないmongrelはヘッダーについてKY(空気読まない)
mongrel_jpg_cache.png

Content-Typeを設定するいくつかの方法

  1. mongrelで設定
  2. plugin(cache_fuなど)を使う
  3. railsで設定:config/initializers/mime_types.rb
  4. 表示時に設定(これはaction_cachesはヘッダーをキャッシュしないので、キャッシュ表示時には意味がない)

結論:正しいContent-Typeは(特に携帯には)大事

上記の方法以外にも、mongrelをやめるという選択肢もあるのですが、とりあえず、railsでヘッダを設定するのはmongrelをやめても邪魔にならない設定であるのでそれをやってみました。
mongrel_jpg_cache_with_mime_type.png
正しくContent-Typeが設定され、携帯からでもちゃんと表示されました!

後mime_types.rbでimage/jpgなどと致命的ミスをしてハマったというのもありました。

Refs

http://rubyforge.org/pipermail/mongrel-users/2006-May/000237.html
http://www.igvita.com/2007/06/13/caching-and-mime-types-in-rails/:cache_fuでのmime_typeキャッシュ

posted by Png satoko on Wed 2 Jul 2008 at 17:02
Contents
caches_actionで携帯で画像が表示されない
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ