Skip to content

Commit dadddf4

Browse files
Disable Turbo's InstantClick
Turbo 8 introduced [InstantClick][]. An unintended side-effect of this feature is that page requests are [not debounced][] when a user hovers over a link. This is a concern for two reasons: 1. It creates an unnecessary burden on the server, which could affect rate limiting. 2. It can artificially inflate our metrics. [InstantClick]: https://turbo.hotwired.dev/handbook/drive#prefetching-links-on-hover [not debounced]: hotwired/turbo#1181 (comment)
1 parent 9f624b1 commit dadddf4

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ Configures the default Rake task to audit and lint the codebase with
129129
### Views
130130

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

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

137140
### Setup
138141

lib/generators/suspenders/views_generator.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Generators
33
class ViewsGenerator < Rails::Generators::Base
44
include Suspenders::Generators::APIAppUnsupported
55

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

99
def install_gems
@@ -20,6 +20,7 @@ def update_application_layout
2020
insert_into_file "app/views/layouts/application.html.erb", " <%= render \"flashes\" -%>\n", after: "<body>\n"
2121
gsub_file "app/views/layouts/application.html.erb", /<html>/, "<html lang=\"<%= I18n.locale %>\">"
2222
gsub_file "app/views/layouts/application.html.erb", /<title>.*<\/title>/, "<title><%= title %></title>"
23+
insert_into_file "app/views/layouts/application.html.erb", " <meta name=\"turbo-prefetch\" content=\"false\">\n", after: "</title>"
2324
end
2425
end
2526
end

test/generators/suspenders/views_generator_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ class ViewsGeneratorTest < Rails::Generators::TestCase
8787
assert_no_match(/Description:\n/, generator_class.desc)
8888
end
8989

90+
test "disables InstantClick" do
91+
run_generator
92+
93+
assert_file app_root("app/views/layouts/application.html.erb") do |file|
94+
assert_match(/<head>.*<\/title>\s{4}<meta\s*name="turbo-prefetch"\s*content=\s*"false">/m, file)
95+
end
96+
end
97+
9098
private
9199

92100
def prepare_destination

0 commit comments

Comments
 (0)