From 73f446acf9527a42993bc93eada228d5c63871ad Mon Sep 17 00:00:00 2001 From: Jose Galisteo Date: Fri, 11 Jun 2021 09:36:59 +0200 Subject: [PATCH 1/4] Add support for HotWire Turbo Drive --- lib/html/includes.js | 11 +++++++++++ lib/html/profile_handler.js | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/html/includes.js b/lib/html/includes.js index f704b43b..fadf6506 100644 --- a/lib/html/includes.js +++ b/lib/html/includes.js @@ -640,6 +640,17 @@ var _MiniProfiler = (function() { }; var bindDocumentEvents = function bindDocumentEvents() { + document.addEventListener("turbo:before-visit", e => { + window.MiniProfilerContainer = document.querySelector('body > .profiler-results') + if(!e.defaultPrevented) window.MiniProfiler.pageTransition() + }) + + document.addEventListener("turbo:load", e => { + if(window.MiniProfilerContainer) { + document.body.appendChild(window.MiniProfilerContainer) + } + }) + document.addEventListener("click", onClickEvents); document.addEventListener("keyup", onClickEvents); document.addEventListener("keyup", toggleShortcutEvent); diff --git a/lib/html/profile_handler.js b/lib/html/profile_handler.js index 1fa51d44..178f6fc2 100644 --- a/lib/html/profile_handler.js +++ b/lib/html/profile_handler.js @@ -1 +1 @@ - + From ba2fe9ba58d438fdb44610d39cdb8aa20e372bc6 Mon Sep 17 00:00:00 2001 From: Jose Galisteo Date: Mon, 14 Jun 2021 19:28:59 +0200 Subject: [PATCH 2/4] Implement callbacks as functions, unbind events and check Turbo constant --- lib/html/includes.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/html/includes.js b/lib/html/includes.js index fadf6506..806c33cd 100644 --- a/lib/html/includes.js +++ b/lib/html/includes.js @@ -495,6 +495,19 @@ var _MiniProfiler = (function() { }, 3000); }; + var onTurboBeforeVisit = function onTurboBeforeVisit(e) { + if(!e.defaultPrevented) { + window.MiniProfilerContainer = document.querySelector('body > .profiler-results') + window.MiniProfiler.pageTransition() + } + } + + var onTurboLoad = function onTurboLoad(e) { + if(window.MiniProfilerContainer) { + document.body.appendChild(window.MiniProfilerContainer) + } + } + var onClickEvents = function onClickEvents(e) { // this happens on every keystroke, and :visible is crazy expensive in IE <9 // and in this case, the display:none check is sufficient. @@ -640,17 +653,6 @@ var _MiniProfiler = (function() { }; var bindDocumentEvents = function bindDocumentEvents() { - document.addEventListener("turbo:before-visit", e => { - window.MiniProfilerContainer = document.querySelector('body > .profiler-results') - if(!e.defaultPrevented) window.MiniProfiler.pageTransition() - }) - - document.addEventListener("turbo:load", e => { - if(window.MiniProfilerContainer) { - document.body.appendChild(window.MiniProfilerContainer) - } - }) - document.addEventListener("click", onClickEvents); document.addEventListener("keyup", onClickEvents); document.addEventListener("keyup", toggleShortcutEvent); @@ -663,6 +665,11 @@ var _MiniProfiler = (function() { turbolinksSkipResultsFetch ); } + + if (typeof Turbo !== "undefined") { + document.addEventListener("turbo:before-visit", onTurboBeforeVisit) + document.addEventListener("turbo:load", onTurboLoad) + } }; var unbindDocumentEvents = function unbindDocumentEvents() { @@ -675,6 +682,8 @@ var _MiniProfiler = (function() { "turbolinks:request-start", turbolinksSkipResultsFetch ); + document.removeEventListener("turbo:before-visit", onTurboBeforeVisit); + document.removeEventListener("turbo:load", onTurboLoad); }; var initFullView = function initFullView() { From bca1ca9f83b57a5b1e2121ef50c15233382e3d12 Mon Sep 17 00:00:00 2001 From: Jose Galisteo Date: Tue, 15 Jun 2021 22:15:36 +0200 Subject: [PATCH 3/4] Refactor based on configuration setting --- lib/html/includes.js | 7 +++++-- lib/html/profile_handler.js | 2 +- lib/mini_profiler/asset_version.rb | 2 +- lib/mini_profiler/config.rb | 3 ++- lib/mini_profiler/profiler.rb | 1 + spec/integration/mini_profiler_spec.rb | 11 +++++++++++ 6 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/html/includes.js b/lib/html/includes.js index 806c33cd..805c2bb1 100644 --- a/lib/html/includes.js +++ b/lib/html/includes.js @@ -666,7 +666,7 @@ var _MiniProfiler = (function() { ); } - if (typeof Turbo !== "undefined") { + if (options.hotwireTurboDriveSupport) { document.addEventListener("turbo:before-visit", onTurboBeforeVisit) document.addEventListener("turbo:load", onTurboLoad) } @@ -1053,6 +1053,8 @@ var _MiniProfiler = (function() { .getAttribute("data-hidden-custom-fields") .toLowerCase() .split(","); + var hotwireTurboDriveSupport = script + .getAttribute('data-turbo-permanent') === "true"; return { ids: ids, path: path, @@ -1071,7 +1073,8 @@ var _MiniProfiler = (function() { collapseResults: collapseResults, htmlContainer: htmlContainer, cssUrl: cssUrl, - hiddenCustomFields: hiddenCustomFields + hiddenCustomFields: hiddenCustomFields, + hotwireTurboDriveSupport: hotwireTurboDriveSupport }; })(); diff --git a/lib/html/profile_handler.js b/lib/html/profile_handler.js index 178f6fc2..fa183ecf 100644 --- a/lib/html/profile_handler.js +++ b/lib/html/profile_handler.js @@ -1 +1 @@ - + diff --git a/lib/mini_profiler/asset_version.rb b/lib/mini_profiler/asset_version.rb index 1c4c346f..9d64ccd8 100644 --- a/lib/mini_profiler/asset_version.rb +++ b/lib/mini_profiler/asset_version.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true module Rack class MiniProfiler - ASSET_VERSION = '40756d3425f8c8e3f901fb5e850a065a' + ASSET_VERSION = 'ec6d5541ecbc11a48798626c16a61342' end end diff --git a/lib/mini_profiler/config.rb b/lib/mini_profiler/config.rb index 417b22ba..ce773ef1 100644 --- a/lib/mini_profiler/config.rb +++ b/lib/mini_profiler/config.rb @@ -57,6 +57,7 @@ def self.default @snapshots_transport_auth_key = nil @snapshots_redact_sql_queries = true @snapshots_transport_gzip_requests = false + @enable_hotwire_turbo_drive_support = false self } @@ -69,7 +70,7 @@ def self.default :skip_schema_queries, :storage, :storage_failure, :storage_instance, :storage_options, :user_provider, :enable_advanced_debugging_tools, :skip_sql_param_names, :suppress_encoding, :max_sql_param_length, - :content_security_policy_nonce + :content_security_policy_nonce, :enable_hotwire_turbo_drive_support # ui accessors attr_accessor :collapse_results, :max_traces_to_show, :position, diff --git a/lib/mini_profiler/profiler.rb b/lib/mini_profiler/profiler.rb index efd52484..3485981d 100644 --- a/lib/mini_profiler/profiler.rb +++ b/lib/mini_profiler/profiler.rb @@ -751,6 +751,7 @@ def get_profile_script(env) htmlContainer: @config.html_container, hiddenCustomFields: @config.snapshot_hidden_custom_fields.join(','), cspNonce: content_security_policy_nonce, + hotwireTurboDriveSupport: @config.enable_hotwire_turbo_drive_support, } if current && current.page_struct diff --git a/spec/integration/mini_profiler_spec.rb b/spec/integration/mini_profiler_spec.rb index b3b7ed4a..98645e51 100644 --- a/spec/integration/mini_profiler_spec.rb +++ b/spec/integration/mini_profiler_spec.rb @@ -257,6 +257,17 @@ def app expect(last_response.status).to equal(200) end + describe 'with hotwire turbo drive support enabled' do + before do + Rack::MiniProfiler.config.enable_hotwire_turbo_drive_support = true + end + + it 'should define data-turbo-permanent as true' do + get '/html' + expect(last_response.body).to include('data-turbo-permanent="true"') + end + end + describe 'with caching re-enabled' do before :each do Rack::MiniProfiler.config.disable_caching = false From 7c0bda7152d6351ce113e77a05b43ca2d12654b5 Mon Sep 17 00:00:00 2001 From: Jose Galisteo Date: Wed, 16 Jun 2021 10:48:04 +0200 Subject: [PATCH 4/4] Add documentation about Hotwire TurboDrive support --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 89723612..1924133f 100644 --- a/README.md +++ b/README.md @@ -413,6 +413,7 @@ snapshots_transport_auth_key|`nil`|`POST` requests made by the snapshots transpo snapshots_redact_sql_queries|`true`|When this is true, SQL queries will be redacted from sampling snapshots, but the backtrace and duration of each SQL query will be saved with the snapshot to keep debugging performance issues possible. snapshots_transport_gzip_requests|`false`|Make the snapshots transporter gzip the requests it makes to `snapshots_transport_destination_url`. content_security_policy_nonce|Rails: Current nonce
Rack: nil|Set the content security policy nonce to use when inserting MiniProfiler's script block. +enable_hotwire_turbo_drive_support| `false` | Enable support for Hotwire TurboDrive page transitions. ### Using MiniProfiler with `Rack::Deflate` middleware