Skip to content

Commit

Permalink
FEATURE: Add support for Hotwire Turbo Drive (#498)
Browse files Browse the repository at this point in the history
We have a new config option `enable_hotwire_turbo_drive_support` to enable support for Hotwire Turbo Drive. If your application uses Hotwire Turbo Drive, set that config option to `true`. See the README for more details.
  • Loading branch information
ceritium committed Jun 16, 2021
1 parent 6cadc08 commit 1f8b391
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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<br>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

Expand Down
25 changes: 24 additions & 1 deletion lib/html/includes.js
Expand Up @@ -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.
Expand Down Expand Up @@ -652,6 +665,11 @@ var _MiniProfiler = (function() {
turbolinksSkipResultsFetch
);
}

if (options.hotwireTurboDriveSupport) {
document.addEventListener("turbo:before-visit", onTurboBeforeVisit)
document.addEventListener("turbo:load", onTurboLoad)
}
};

var unbindDocumentEvents = function unbindDocumentEvents() {
Expand All @@ -664,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() {
Expand Down Expand Up @@ -1033,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,
Expand All @@ -1051,7 +1073,8 @@ var _MiniProfiler = (function() {
collapseResults: collapseResults,
htmlContainer: htmlContainer,
cssUrl: cssUrl,
hiddenCustomFields: hiddenCustomFields
hiddenCustomFields: hiddenCustomFields,
hotwireTurboDriveSupport: hotwireTurboDriveSupport
};
})();

Expand Down
2 changes: 1 addition & 1 deletion lib/html/profile_handler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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
3 changes: 2 additions & 1 deletion lib/mini_profiler/config.rb
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/mini_profiler/profiler.rb
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions spec/integration/mini_profiler_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 1f8b391

Please sign in to comment.