views:

427

answers:

4

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

A: 

I don't think they are on by default.

My understanding is that they need to be explicitly set using stale?/fresh_when call or similar.

Toby Hede
I checked the controller there is no conditional get code.-daniel
Daniel
Yeah, it does too (Im using thin) .... never noticed that before
Toby Hede
A: 

There's an etag setter method on the ActionController::Response object, which deletes the ETag HTTP header if it's blank, so you should just be able to clear it in your controller (probably in a before filter):

response.etag = nil
John Topley
yes, but I need to do it for all responses, while I could do it for each controller, that would get tedious after a while and be mistake prone. What if someone missed one?
Daniel
A: 

Why not add a before_filter in your application controller which sets etag to nil?

Kris
I would prefer not to have to adjust some 30+ controllers and also make sure everyone sets it right in the future. :)
Daniel
If you put the before_filter in your application_controller it get run for all controllers, and if for any reason you have a controller that does need to generate etags just use skip_before_filter.
Kris
+1  A: 

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
gucki