Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/controllers/concerns/cached_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(", ")
Expand Down
9 changes: 9 additions & 0 deletions app/views/layouts/turbo_rails/frame.html.erb
Original file line number Diff line number Diff line change
@@ -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. %>
<html>
<head>
<%= yield :head %>
</head>
<body>
<%= yield %>
</body>
</html>
17 changes: 17 additions & 0 deletions test/controllers/cached_responses_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading