diff --git a/app/controllers/concerns/cached_responses.rb b/app/controllers/concerns/cached_responses.rb index b17e29a9b..09cf3e7ef 100644 --- a/app/controllers/concerns/cached_responses.rb +++ b/app/controllers/concerns/cached_responses.rb @@ -84,10 +84,12 @@ def cache_read_response def cache_completed_response(key, original_session, encoding) if response.status == 200 && response.media_type == "text/html" && session.to_hash == original_session && !response.headers["Content-Encoding"] - body = encoding == "gzip" ? Zlib.gzip(response.body) : response.body - unless body.empty? || response.headers["ETag"] || response.headers["Last-Modified"] - # Match Rack::ETag once, rather than hashing the same bytes on each hit. - response.headers["ETag"] = %(W/"#{Digest::SHA256.hexdigest(body).byteslice(0, 32)}") + html = response.body + body = encoding == "gzip" ? Zlib.gzip(html) : html + unless html.empty? || response.headers["ETag"] || response.headers["Last-Modified"] + # Match Rack::ETag once, rather than hashing the same bytes on each hit. Hash the HTML, as it + # does: gzip bytes carry the time they were compressed, so no render would ever match them. + response.headers["ETag"] = %(W/"#{Digest::SHA256.hexdigest(html).byteslice(0, 32)}") end response.headers["Content-Encoding"] = "gzip" if encoding == "gzip" response.headers["Vary"] = (response.headers["Vary"].to_s.split(/,\s*/) | [ "Accept-Encoding" ]).join(", ") diff --git a/app/views/layouts/turbo_rails/frame.html.erb b/app/views/layouts/turbo_rails/frame.html.erb new file mode 100644 index 000000000..eb1a2d248 --- /dev/null +++ b/app/views/layouts/turbo_rails/frame.html.erb @@ -0,0 +1,9 @@ +<%# Turbo's frame layout without its csrf_meta_tags. Header-only forgery protection needs no secret in cached HTML, and a token masked afresh on each render would give every frame a new ETag. %> + + + <%= yield :head %> + + + <%= yield %> + + diff --git a/test/controllers/cached_responses_test.rb b/test/controllers/cached_responses_test.rb index 9168f3bf2..9af865b59 100644 --- a/test/controllers/cached_responses_test.rb +++ b/test/controllers/cached_responses_test.rb @@ -178,6 +178,23 @@ class CachedResponsesTest < ActionDispatch::IntegrationTest assert_not_equal etag, response.headers["ETag"] end + test "a compressed page answers a browser's revalidation" do + get room_url(@room), headers: { "Accept-Encoding" => "gzip" } + assert_equal "gzip", response.headers["Content-Encoding"] + etag = response.headers["ETag"] + assert etag.present? + get room_url(@room), headers: { "Accept-Encoding" => "gzip", "If-None-Match" => etag } + assert_response :not_modified + end + + test "a frame answers a browser's revalidation" do + get user_sidebar_url(:me), headers: { "Turbo-Frame" => "user_sidebar" } + etag = response.headers["ETag"] + assert etag.present? + get user_sidebar_url(:me), headers: { "Turbo-Frame" => "user_sidebar", "If-None-Match" => etag } + assert_response :not_modified + end + test "origins and frame variants have separate entries" do get room_url(@room) ResponseCache.instance.expects(:write).twice