Ruby on Rails how to disable / disable ETag

Hi,

How to disable ETag in Ruby on Rails v2.3.5

When I make a direct request to RoR / Mongrel, the ETag header is present.

TIA

-daniel

+7
ruby-on-rails etag
source share
6 answers

much easier:

config.middleware.delete Rack::ETag 
+19
source share

Putting response.etag = nil in the before_filter file does not work. This stage is generated immediately before sending the response (it is calculated from the body, after which all rendering is performed).

The correct workaround for disabling the use and generation of etag (and therefore saving time in md5) is the 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 
+5
source share

Here's the etag setter method on the ActionController::Response object, which removes the ETag HTTP header if it is empty, so you should just clear it in your controller (possibly in the front filter):

 response.etag = nil 
+1
source share

I do not think they are enabled by default.

I understand that they must be explicitly set using an obsolete call / fresh _when or the like.

0
source share

I work in Rails 4 on WEBrick, trying to get a response to the cache until it expires at a specific time every day. It seems like an automatically generated ETag is interfering with the expiration cache, so I was looking for this answer. I did not find anything useful here, but I solved the problem, so I will share it.

tl; dr Set Last-Modified Header

But install it for what? In my situation, I tried to optimize a web service that returned the results of a process that runs at the same time every day. My response headers looked like this:

 response.headers['Cache-Control'] = "max-age=86400" response.headers['Expires'] = getCacheTime response.headers['Last-Modified'] = getLastModified 

First, you want to explicitly write the Cache-Control header to overwrite all the default values. I found that I should be 24 hours to match the maximum of my expiration header. I set the expiration header with a function that looks something like this:

 def getCacheTime now = Time.now.utc cacheTime = Time.utc(now.year, now.month, now.day, 22, 00, 00) if now > cacheTime cacheTime = cacheTime + (60 * 60 * 24) end cacheTime.httpdate end 

The getLastModified function returns 24 hours less than the getCacheTime function. It seems that setting this will overwhelm ETag (another validation cache header), at least in my current development environment.

0
source share

Why not add a before_filter to the application controller that sets etag to nil?

-one
source share

All Articles