Skip to content

Commit

Permalink
Merge pull request #542 from bradgessler/meta-tags
Browse files Browse the repository at this point in the history
Separate turbo meta tag generation from provides
  • Loading branch information
jorgemanrubia authored Dec 24, 2023
2 parents 36ab3ad + d966ddf commit d1b4201
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
38 changes: 31 additions & 7 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,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

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 d1b4201

Please sign in to comment.