Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate turbo meta tag generation from provides #542

Merged
merged 1 commit into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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