1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Include the content of the flash in the auto-generated etag (#26250)

Include the content of the flash in the auto-generated etag
This commit is contained in:
David Heinemeier Hansson 2016-08-22 13:34:35 -07:00 committed by GitHub
parent 3bfd352452
commit debd774d63
5 changed files with 59 additions and 0 deletions

View file

@ -1,3 +1,15 @@
* Include the content of the flash in the auto-generated etag. This solves the following problem:
1. POST /messages
2. redirect_to messages_url, notice: 'Message was created'
3. GET /messages/1
4. GET /messages
Step 4 would before still include the flash message, even though it's no longer relevant,
because the etag cache was recorded with the flash in place and didn't change when it was gone.
*DHH*
* SSL: Changes redirect behavior for all non-GET and non-HEAD requests
(like POST/PUT/PATCH etc) to `http://` resources to redirect to `https://`
with a [307 status code](http://tools.ietf.org/html/rfc7231#section-6.4.7) instead of [301 status code](http://tools.ietf.org/html/rfc7231#section-6.4.2).

View file

@ -23,6 +23,7 @@ module ActionController
autoload :Cookies
autoload :DataStreaming
autoload :EtagWithTemplateDigest
autoload :EtagWithFlash
autoload :Flash
autoload :ForceSSL
autoload :Head

View file

@ -213,6 +213,7 @@ module ActionController
Renderers::All,
ConditionalGet,
EtagWithTemplateDigest,
EtagWithFlash,
Caching,
MimeResponds,
ImplicitRender,

View file

@ -0,0 +1,16 @@
module ActionController
# When you're using the flash, it's generally used as a conditional on the view.
# This means the content of the view depends on the flash. Which in turn means
# that the etag for a response should be computed with the content of the flash
# in mind. This does that by including the content of the flash as a component
# in the etag that's generated for a response.
module EtagWithFlash
extend ActiveSupport::Concern
include ActionController::ConditionalGet
included do
etag { flash unless flash.empty? }
end
end
end

View file

@ -263,6 +263,13 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
flash[:bar] = "for great justice"
head :ok
end
def set_flash_optionally
flash.now.notice = params[:flash]
if stale? etag: "abe"
render inline: "maybe flash"
end
end
end
def test_flash
@ -310,6 +317,28 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
end
end
def test_flash_factored_into_etag
with_test_route_set do
get "/set_flash_optionally"
no_flash_etag = response.etag
get "/set_flash_optionally", params: { flash: "hello!" }
hello_flash_etag = response.etag
assert_not_equal no_flash_etag, hello_flash_etag
get "/set_flash_optionally", params: { flash: "hello!" }
another_hello_flash_etag = response.etag
assert_equal another_hello_flash_etag, hello_flash_etag
get "/set_flash_optionally", params: { flash: "goodbye!" }
goodbye_flash_etag = response.etag
assert_not_equal another_hello_flash_etag, goodbye_flash_etag
end
end
private
# Overwrite get to send SessionSecret in env hash