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

Disable Turbo's InstantClick #1164

Merged
merged 1 commit into from
Mar 8, 2024
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,13 @@ Configures the default Rake task to audit and lint the codebase with
### Views

Configures flash messages, page titles via the [title][] gem, and sets the
document [lang][].
document [lang][]. Disables Turbo's [InstantClick][] to account for page
requests that are [not debounced][].

[title]: https://github.com/calebhearth/title
[lang]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang
[InstantClick]: https://turbo.hotwired.dev/handbook/drive#prefetching-links-on-hover
[not debounced]: https://github.com/hotwired/turbo/pull/1181#issuecomment-1936505362

### Setup

Expand Down
3 changes: 2 additions & 1 deletion lib/generators/suspenders/views_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Generators
class ViewsGenerator < Rails::Generators::Base
include Suspenders::Generators::APIAppUnsupported

desc "Configures flash messages, page titles and the document lang."
desc "Configures flash messages, page titles and the document lang. Disables Turbo's InstantClick."
source_root File.expand_path("../../templates/views", __FILE__)

def install_gems
Expand All @@ -20,6 +20,7 @@ def update_application_layout
insert_into_file "app/views/layouts/application.html.erb", " <%= render \"flashes\" -%>\n", after: "<body>\n"
gsub_file "app/views/layouts/application.html.erb", /<html>/, "<html lang=\"<%= I18n.locale %>\">"
gsub_file "app/views/layouts/application.html.erb", /<title>.*<\/title>/, "<title><%= title %></title>"
insert_into_file "app/views/layouts/application.html.erb", " <meta name=\"turbo-prefetch\" content=\"false\">\n", after: "</title>"
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/generators/suspenders/views_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ class ViewsGeneratorTest < Rails::Generators::TestCase
assert_no_match(/Description:\n/, generator_class.desc)
end

test "disables InstantClick" do
run_generator

assert_file app_root("app/views/layouts/application.html.erb") do |file|
assert_match(/<head>.*<\/title>\s{4}<meta\s*name="turbo-prefetch"\s*content=\s*"false">/m, file)
end
end

private

def prepare_destination
Expand Down