Last Updated: February 25, 2016
·
289
· arnaldog

Disable Rack Cache lookup in presence of session cookie

config/environments/production.rb

config.action_dispatch.rack_cache[:allow_reload] = true

config/application.rb

config.middleware.insert_before Rack::Cache, ":RackCacheInvalidator"

lib/rackcacheinvalidator

class RackCacheInvalidator
  def initialize application
    @application = application
  end

  def call(environment)
    if environment['HTTP_COOKIE'].include?(session_key)
      environment['HTTP_PRAGMA'] = 'no-cache'
    end

    @application.call environment
  end

  def session_key
    Rails.application.config.session_options[:key]
  end
end