From 3b3712d4f32dd4c7f42261086e8975d2fdeee755 Mon Sep 17 00:00:00 2001 From: Steve Polito Date: Fri, 8 Mar 2024 10:14:47 -0500 Subject: [PATCH] Disable Turbo's InstantClick (#1164) 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 metrics. [InstantClick]: https://turbo.hotwired.dev/handbook/drive#prefetching-links-on-hover [not debounced]: https://github.com/hotwired/turbo/pull/1181#issuecomment-1936505362 --- README.md | 5 ++++- lib/generators/suspenders/views_generator.rb | 3 ++- test/generators/suspenders/views_generator_test.rb | 8 ++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0f1a43568..43f43af16 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/generators/suspenders/views_generator.rb b/lib/generators/suspenders/views_generator.rb index 9fb392a2b..2c3d14a0d 100644 --- a/lib/generators/suspenders/views_generator.rb +++ b/lib/generators/suspenders/views_generator.rb @@ -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 @@ -20,6 +20,7 @@ def update_application_layout insert_into_file "app/views/layouts/application.html.erb", " <%= render \"flashes\" -%>\n", after: "\n" gsub_file "app/views/layouts/application.html.erb", //, "\">" gsub_file "app/views/layouts/application.html.erb", /.*<\/title>/, "<title><%= title %>" + insert_into_file "app/views/layouts/application.html.erb", " \n", after: "" end end end diff --git a/test/generators/suspenders/views_generator_test.rb b/test/generators/suspenders/views_generator_test.rb index 398d7ff2a..db661942c 100644 --- a/test/generators/suspenders/views_generator_test.rb +++ b/test/generators/suspenders/views_generator_test.rb @@ -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(/.*<\/title>\s{4}/m, file) + end + end + private def prepare_destination