From d966ddf51435e9e4f666e5545c9bfad651a5fa4e Mon Sep 17 00:00:00 2001 From: Brad Gessler Date: Wed, 13 Dec 2023 11:15:24 -0800 Subject: [PATCH] Separate turbo meta tag generation from `provides :head` calls. This makes it possible to use the meta tag helpers from outside Rails rendering, such as an app built entirely from Phlex components. The `turbo_meta_tags` method was added to minimize confusion over the seemingly random `content_for :head` that has been observed in early Turbo 8 betas. This is a thin wrapper around `content_for :head`. --- app/helpers/turbo/drive_helper.rb | 38 +++++++++++++++---- app/views/layouts/turbo_rails/frame.html.erb | 2 +- .../app/views/layouts/application.html.erb | 2 +- .../views/layouts/turbo_rails/frame.html.erb | 2 +- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/app/helpers/turbo/drive_helper.rb b/app/helpers/turbo/drive_helper.rb index 66e5994d..18e4daa8 100644 --- a/app/helpers/turbo/drive_helper.rb +++ b/app/helpers/turbo/drive_helper.rb @@ -1,10 +1,10 @@ module Turbo::DriveHelper - # Note: These helpers require a +yield :head+ provision in the layout. + # Note: These helpers require a +turbo_meta_tags+ provision in the layout. # # ==== Example # # # app/views/application.html.erb - # <%= yield :head %><%= yield %> + # <%= turbo_meta_tags %><%= yield %> # # # app/views/trays/index.html.erb # <% turbo_exempts_page_from_cache %> @@ -13,25 +13,49 @@ module Turbo::DriveHelper # Pages that are more likely than not to be a cache miss can skip turbo cache to avoid visual jitter. # Cannot be used along with +turbo_exempts_page_from_preview+. def turbo_exempts_page_from_cache - provide :head, tag.meta(name: "turbo-cache-control", content: "no-cache") + provide :head, turbo_exempts_page_from_cache_tag + end + + def turbo_exempts_page_from_cache_tag + tag.meta(name: "turbo-cache-control", content: "no-cache") end # Specify that a cached version of the page should not be shown as a preview during an application visit. # Cannot be used along with +turbo_exempts_page_from_cache+. def turbo_exempts_page_from_preview - provide :head, tag.meta(name: "turbo-cache-control", content: "no-preview") + provide :head, turbo_exempts_page_from_preview_tag + end + + def turbo_exempts_page_from_preview_tag + tag.meta(name: "turbo-cache-control", content: "no-preview") end # Force the page, when loaded by Turbo, to be cause a full page reload. def turbo_page_requires_reload - provide :head, tag.meta(name: "turbo-visit-control", content: "reload") + provide :head, turbo_page_requires_reload_tag + end + + def turbo_page_requires_reload_tag + tag.meta(name: "turbo-visit-control", content: "reload") end def turbo_refreshes_with(method: :replace, scroll: :reset) + provide :head, turbo_refresh_method_tag(method) + provide :head, turbo_refresh_scroll_tag(scroll) + end + + def turbo_refresh_method_tag(method = :replace) raise ArgumentError, "Invalid refresh option '#{method}'" unless method.in?(%i[ replace morph ]) + tag.meta(name: "turbo-refresh-method", content: method) + end + + def turbo_refresh_scroll_tag(scroll = :reset) raise ArgumentError, "Invalid scroll option '#{scroll}'" unless scroll.in?(%i[ reset preserve ]) + tag.meta(name: "turbo-refresh-scroll", content: scroll) + end - provide :head, tag.meta(name: "turbo-refresh-method", content: method) - provide :head, tag.meta(name: "turbo-refresh-scroll", content: scroll) + def turbo_meta_tags + content_for :head end end + diff --git a/app/views/layouts/turbo_rails/frame.html.erb b/app/views/layouts/turbo_rails/frame.html.erb index 0171671e..d2509745 100644 --- a/app/views/layouts/turbo_rails/frame.html.erb +++ b/app/views/layouts/turbo_rails/frame.html.erb @@ -1,6 +1,6 @@ - <%= yield :head %> + <%= turbo_meta_tags %> <%= yield %> diff --git a/test/dummy/app/views/layouts/application.html.erb b/test/dummy/app/views/layouts/application.html.erb index ae914a89..af52d721 100644 --- a/test/dummy/app/views/layouts/application.html.erb +++ b/test/dummy/app/views/layouts/application.html.erb @@ -5,7 +5,7 @@ <%= csrf_meta_tags %> <%= stylesheet_link_tag 'application', media: 'all' %> - <%= yield :head %> + <%= turbo_meta_tags %> <%= javascript_importmap_tags %> diff --git a/test/frames/views/layouts/turbo_rails/frame.html.erb b/test/frames/views/layouts/turbo_rails/frame.html.erb index 0fbebe4b..c4a06274 100644 --- a/test/frames/views/layouts/turbo_rails/frame.html.erb +++ b/test/frames/views/layouts/turbo_rails/frame.html.erb @@ -1,7 +1,7 @@ - <%= yield :head %> + <%= turbo_meta_tags %> <%= yield %>