How can caches_action be configured to work in multiple formats?

I have a rails action that responds to requests in various formats, including AJAX requests, for example:

   def index
    # do stuff
    respond_to do |format|
      format.html do
        # index.html.erb
      end
      format.js do
        render :update do |page|
          page.replace_html 'userlist', :partial => "userlist", :object=>@users
          page.hide('spinner')
          page.show('pageresults')
        end
      end
    end
   end

I set this action to cache using memcached using:

 caches_action :index, :expires_in=>1.hour, :cache_path => Proc.new { |c| "index/#{c.params[:page]}/#{c.request.format}" }

This template works fine for caching an HTML result, but not for a JS result. Part of JS always works great when it doesn't come from the cache. However, when the cache hits, the page does not refresh.

What can cause this and what is a fix?

Update: delving into this more, it looks like requests from the cache are getting the mime type "text / html" instead of "text / javascript". However, I'm not sure how to fix this - is this a memcached quirk? (Rails 2.3.2)

+3
3

, - .

caches_action :show,
              :cache_path => :post_cache_path.to_proc,
              :expires_in => 1.hour

protected

def post_cache_path
  if request.xhr?
    "#{request.url}.js"
  else
    "#{request.url}.html"
  end
end
+2

, , , : update rjs, . , 8 , rjs 80 . memcached, , , , , .

0

edge (3.0.1).

:

  caches_action :show, :cache_path => :show_cache_path.to_proc

  private

  def show_cache_path
    if request.accepts[0].to_sym == :html
      "#{request.host_with_port + request.request_uri}.html"
    else
      "#{request.host_with_port + request.request_uri}.js"
    end
  end
0

Source: https://habr.com/ru/post/1718644/


All Articles