Daniel Daniel - 1 month ago 10
HTTP Question

Ruby on Rails, how to disable/turn off ETag

Greetings,

How do I turn off ETag(s) in Ruby on Rails v2.3.5

When I do a direct request to to the RoR/Mongrel an ETag header is present.

TIA,

-daniel

Answer

Putting response.etag = nil in a before_filter does not work. The etag is generated just before the response is send (it's caluculated from the body so after all rendering has been done).

The proper workaround to disable etag use and generation (and so save the time spend in md5) it this monkey patch:

module ActionController
  class Request
    # never match any incomming etag
    def etag_matches?(etag)
      false
    end
  end

  class Response
    # fake rails that our response already has an etag set and so none is generated automatically
    def etag?
      true
    end
  end
end
Comments