Rails conditional get (development mode) still returning "Not Modified" (`#fresh_when ()`)

Why does the following (in development mode) incorrectly return "304 not changed" - shouldn't such functions be disabled by default in Rails when starting in development mode?

My controller is as follows:

class WidgetController < ApplicationController def show @widget = Widget.find(params[:id]) fresh_when(etag: etag_for(@widget), last_modified: @widget.updated_at) end private def etag_for(*args) args.flatten + [current_user, last_deploy] end def last_deploy `git log --pretty=format:%H`.chomp end end 

I don’t understand why in Development mode in my Rails application this will return β€œ304 Not Modified” headers, I thought that in accordance with the development mode such things were not included?

I am using a thin web server locally, which I suppose is a little unusual, otherwise it is a typical application without special conditions or cases running on Rails 3.1.1

+4
source share
2 answers

So the answer was simpler:

In ./config/environments/development.rb I added the line:

  config.middleware.delete(Rack::ConditionalGet) 

I filed this β€œerror” with Rails to get an official answer, the problem can be found here in my Github tracker store .

+2
source

fresh_when does not change environment. Per source for fresh_when

 def fresh_when(options) options.assert_valid_keys(:etag, :last_modified, :public) response.etag = options[:etag] if options[:etag] response.last_modified = options[:last_modified] if options[:last_modified] response.cache_control[:public] = true if options[:public] head :not_modified if request.fresh?(response) end 

And then the source of request.fresh?

 def fresh?(response) last_modified = if_modified_since etag = if_none_match return false unless last_modified || etag success = true success &&= not_modified?(response.last_modified) if last_modified success &&= etag_matches?(response.etag) if etag success end 

However, you can add a random number (or timestamp) to the stage in design mode to force each request to be fresh.

0
source

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


All Articles