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

Add turbo_disable_prefetch helper #572

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions app/helpers/turbo/drive_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ def turbo_refresh_method_tag(method = :replace)
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

# Disable InstantClick (prefetch).
def turbo_disable_prefetch
provide :head, turbo_disable_prefetch_tag
end

# See +turbo_disable_prefetch+.
def turbo_disable_prefetch_tag
tag.meta(name: "turbo-prefetch", content: "false")
end
end

5 changes: 5 additions & 0 deletions test/drive/drive_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Turbo::DriveHelperTest < ActionDispatch::IntegrationTest
assert_match(/<meta name="turbo-refresh-method" content="morph">/, @response.body)
assert_match(/<meta name="turbo-refresh-scroll" content="preserve">/, @response.body)
end

test "opting out of the default prefetch" do
get trays_path
assert_match(/<meta name="turbo-prefetch" content="false">/, @response.body)
end
end

class Turbo::DriverHelperUnitTest < ActionView::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/views/trays/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<% turbo_exempts_page_from_cache %>
<% turbo_page_requires_reload %>
<%= turbo_refreshes_with method: :morph, scroll: :preserve %>
<%= turbo_disable_prefetch %>

<p>Not in the cache!</p>