Skip to content

Commit

Permalink
Let fresh_when actually do the head(:not_modified). Cleaner and we ge…
Browse files Browse the repository at this point in the history
…t the filter halting for free then.
  • Loading branch information
dhh committed Oct 21, 2008
1 parent 68d84e2 commit 448e7e7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
18 changes: 8 additions & 10 deletions actionpack/lib/action_controller/base.rb
Expand Up @@ -1077,8 +1077,7 @@ def redirect_to_full_url(url, status)
# Sets the etag and/or last_modified on the response and checks it against
# the client request. If the request doesn't match the options provided, the
# request is considered stale and should be generated from scratch. Otherwise,
# it's fresh and we don't need to generate anything and can rely on the default
# reply of "304 Not Modified".
# it's fresh and we don't need to generate anything and a reply of "304 Not Modified" is sent.
#
# Example:
#
Expand All @@ -1102,9 +1101,8 @@ def fresh?(options)
!stale?(options)
end

# Sets the etag, last_modified, or both such that the request can be short-circuited
# with a "304 Not Modified" response instead of rendering a template when the request
# is already fresh.
# Sets the etag, last_modified, or both on the response and renders a
# "304 Not Modified" response if the request is already fresh.
#
# Example:
#
Expand All @@ -1120,6 +1118,10 @@ def fresh_when(options)

response.etag = options[:etag] if options[:etag]
response.last_modified = options[:last_modified] if options[:last_modified]

if request.fresh?(response)
head :not_modified
end
end

# Sets a HTTP 1.1 Cache-Control header. Defaults to issuing a "private" instruction, so that
Expand Down Expand Up @@ -1208,11 +1210,7 @@ def log_processing
end

def default_render #:nodoc:
if request.fresh?(response)
head :not_modified
else
render
end
render
end

def perform_action
Expand Down
4 changes: 1 addition & 3 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -41,9 +41,7 @@ def conditional_hello_with_bangs
before_filter :handle_last_modified_and_etags, :only=>:conditional_hello_with_bangs

def handle_last_modified_and_etags
if fresh?(:last_modified => Time.now.utc.beginning_of_day, :etag => [ :foo, 123 ])
head :not_modified
end
fresh_when(:last_modified => Time.now.utc.beginning_of_day, :etag => [ :foo, 123 ])
end

def render_hello_world
Expand Down

0 comments on commit 448e7e7

Please sign in to comment.