Skip to content

Commit

Permalink
Separate turbo meta tag generation from provides
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
bradgessler committed Dec 14, 2023
1 parent 4eb4e92 commit 823b14f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
35 changes: 27 additions & 8 deletions app/helpers/turbo/drive_helper.rb
Original file line number Diff line number Diff line change
@@ -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
# <html><head><%= yield :head %></head><body><%= yield %></html>
# <html><head><%= turbo_meta_tags %></head><body><%= yield %></html>
#
# # app/views/trays/index.html.erb
# <% turbo_exempts_page_from_cache %>
Expand All @@ -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
2 changes: 1 addition & 1 deletion app/views/layouts/turbo_rails/frame.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head>
<%= yield :head %>
<%= turbo_meta_tags %>
</head>
<body>
<%= yield %>
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= csrf_meta_tags %>

<%= stylesheet_link_tag 'application', media: 'all' %>
<%= yield :head %>
<%= turbo_meta_tags %>
<%= javascript_importmap_tags %>
</head>

Expand Down
2 changes: 1 addition & 1 deletion test/frames/views/layouts/turbo_rails/frame.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<head>
<meta name="alternative" content="present" />
<%= yield :head %>
<%= turbo_meta_tags %>
</head>
<body>
<%= yield %>
Expand Down

0 comments on commit 823b14f

Please sign in to comment.