Skip to content

Commit

Permalink
Include layout for Turbo-Frame: requests
Browse files Browse the repository at this point in the history
The bandwidth benefits of optimizing `Turbo-Frame:` header responses to
omit the layout do not offset the HTTP-layer cache busting trade-offs.

Similarly, there's an opportunity to implement behavior changes related
to:

* [@hotwired/turbohotwired#361][]
* [@hotwired/turbohotwired#257][]

if both `<turbo-frame>` elements' `FrameController` instances and
`Session` instances were able to share `Visit` instances built from
fully formed `<html>` pages. By trimming the layout and outer portions
of the document, `turbo-rails` forces frames to deal with incomplete
fragments.

[@hotwired/turbohotwired#257]: hotwired/turbo#257
[@hotwired/turbohotwired#361]: hotwired/turbo#361
  • Loading branch information
seanpdoyle committed Sep 15, 2021
1 parent 8df11f5 commit 6777a72
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
13 changes: 1 addition & 12 deletions app/controllers/turbo/frames/frame_request.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
# Turbo frame requests are requests made from within a turbo frame with the intention of replacing the content of just
# that frame, not the whole page. They are automatically tagged as such by the Turbo Frame JavaScript, which adds a
# <tt>Turbo-Frame</tt> header to the request. When that header is detected by the controller, we ensure that any
# template layout is skipped (since we're only working on an in-page frame, thus can skip the weight of the layout), and
# that the etag for the page is changed (such that a cache for a layout-less request isn't served on a normal request
# and vice versa).
#
# This is merely a rendering optimization. Everything would still work just fine if we rendered everything including the layout.
# Turbo Frames knows how to fish out the relevant frame regardless.
# <tt>Turbo-Frame</tt> header to the request.
#
# This module is automatically included in <tt>ActionController::Base</tt>.
module Turbo::Frames::FrameRequest
extend ActiveSupport::Concern

included do
layout -> { false if turbo_frame_request? }
etag { :frame if turbo_frame_request? }
end

private
def turbo_frame_request?
request.headers["Turbo-Frame"].present?
Expand Down
6 changes: 3 additions & 3 deletions test/frames/frame_request_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require "turbo_test"

class Turbo::FrameRequestControllerTest < ActionDispatch::IntegrationTest
test "frame requests are rendered without a layout" do
test "frame requests are rendered with a layout" do
get tray_path(id: 1)
assert_select "title", count: 1

get tray_path(id: 1), headers: { "Turbo-Frame" => "true" }
assert_select "title", count: 0
assert_select "title", count: 1
end

test "frame requests get a unique etag" do
Expand All @@ -16,6 +16,6 @@ class Turbo::FrameRequestControllerTest < ActionDispatch::IntegrationTest
get tray_path(id: 1), headers: { "Turbo-Frame" => "true" }
etag_with_frame = @response.headers["ETag"]

assert_not_equal etag_with_frame, etag_without_frame
assert_equal etag_with_frame, etag_without_frame
end
end

0 comments on commit 6777a72

Please sign in to comment.