From 823b14f9ddfb0158bad3897a5ab8e15604d2a6bf 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 This makes it possible to use the meta tag helpers from outside Rails rendering, such as an app built entirely from Phlex components. More importantly, this removes the requirement to have a `content_for :head` tag in the head of the Rails application. Instead the developer just adds the `turbo_meta_tags(method: :morph, scroll: :preserve)` to the top of their pages and it will setup the `yield :turbo_head` block that the helpers provide via `provide :turbo_head`. --- app/helpers/turbo/drive_helper.rb | 35 ++++++++++++++----- 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, 30 insertions(+), 11 deletions(-) diff --git a/app/helpers/turbo/drive_helper.rb b/app/helpers/turbo/drive_helper.rb index 66e5994d..9c9218dc 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,44 @@ 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 ]) - raise ArgumentError, "Invalid scroll option '#{scroll}'" unless scroll.in?(%i[ reset preserve ]) + tag.meta(name: "turbo-refresh-method", content: method) + end - provide :head, tag.meta(name: "turbo-refresh-method", content: method) - provide :head, tag.meta(name: "turbo-refresh-scroll", content: scroll) + 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 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 %>