From 9b73c9b010561eb3f9bf7a3e09df63b6a8f32ce2 Mon Sep 17 00:00:00 2001 From: OsamaSayegh Date: Tue, 14 Jul 2020 15:24:25 +0300 Subject: [PATCH 1/4] Allow assets to be precompiled with Sprockets --- lib/html/includes.js | 7 +++++-- lib/html/profile_handler.js | 2 +- lib/html/rack-mini-profiler.css | 3 +++ lib/html/rack-mini-profiler.js | 2 ++ lib/mini_profiler/config.rb | 3 ++- lib/mini_profiler/profiler.rb | 12 +++++++++++- lib/mini_profiler_rails/railtie.rb | 7 +++++++ 7 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 lib/html/rack-mini-profiler.css create mode 100644 lib/html/rack-mini-profiler.js diff --git a/lib/html/includes.js b/lib/html/includes.js index e5930d09..a9cc120d 100644 --- a/lib/html/includes.js +++ b/lib/html/includes.js @@ -980,6 +980,7 @@ var MiniProfiler = (function() { script.getAttribute("data-start-hidden") === "true" || sessionStorage["rack-mini-profiler-start-hidden"] === "true"; var htmlContainer = script.getAttribute("data-html-container"); + var cssUrl = script.getAttribute("data-css-url"); return { ids: ids, path: path, @@ -996,7 +997,8 @@ var MiniProfiler = (function() { toggleShortcut: toggleShortcut, startHidden: startHidden, collapseResults: collapseResults, - htmlContainer: htmlContainer + htmlContainer: htmlContainer, + cssUrl: cssUrl }; })(); @@ -1056,7 +1058,7 @@ var MiniProfiler = (function() { var init = function init() { if (options.authorized) { - var url = options.path + "includes.css?v=" + options.version; + var url = options.cssUrl; if (document.createStyleSheet) { document.createStyleSheet(url); @@ -1398,4 +1400,5 @@ var MiniProfiler = (function() { }; })(); +window.MiniProfiler = MiniProfiler; MiniProfiler.init(); diff --git a/lib/html/profile_handler.js b/lib/html/profile_handler.js index 59480857..d147c266 100644 --- a/lib/html/profile_handler.js +++ b/lib/html/profile_handler.js @@ -1 +1 @@ - + diff --git a/lib/html/rack-mini-profiler.css b/lib/html/rack-mini-profiler.css new file mode 100644 index 00000000..e156c7e0 --- /dev/null +++ b/lib/html/rack-mini-profiler.css @@ -0,0 +1,3 @@ +/* + *= require ./includes + */ diff --git a/lib/html/rack-mini-profiler.js b/lib/html/rack-mini-profiler.js new file mode 100644 index 00000000..37cd2247 --- /dev/null +++ b/lib/html/rack-mini-profiler.js @@ -0,0 +1,2 @@ +//= require ./includes +//= require ./vendor diff --git a/lib/mini_profiler/config.rb b/lib/mini_profiler/config.rb index 53211db2..1cc4edd4 100644 --- a/lib/mini_profiler/config.rb +++ b/lib/mini_profiler/config.rb @@ -60,7 +60,8 @@ def self.default :base_url_path, :disable_caching, :enabled, :flamegraph_sample_rate, :logger, :pre_authorize_cb, :skip_paths, :skip_schema_queries, :storage, :storage_failure, :storage_instance, - :storage_options, :user_provider, :enable_advanced_debugging_tools + :storage_options, :user_provider, :enable_advanced_debugging_tools, + :assets_url attr_accessor :skip_sql_param_names, :suppress_encoding, :max_sql_param_length # ui accessors diff --git a/lib/mini_profiler/profiler.rb b/lib/mini_profiler/profiler.rb index b16de3f7..97b3079d 100644 --- a/lib/mini_profiler/profiler.rb +++ b/lib/mini_profiler/profiler.rb @@ -628,10 +628,20 @@ def ids_comma_separated(env) # * you do not want script to be automatically appended for the current page. You can also call cancel_auto_inject def get_profile_script(env) path = "#{env['RACK_MINI_PROFILER_ORIGINAL_SCRIPT_NAME']}#{@config.base_url_path}" + version = MiniProfiler::ASSET_VERSION + if @config.assets_url + url = @config.assets_url.call('rack-mini-profiler.js', version, env) + css_url = @config.assets_url.call('rack-mini-profiler.css', version, env) + end + + url = "#{path}includes.js?v=#{version}" if !url + css_url = "#{path}includes.css?v=#{version}" if !css_url settings = { path: path, - version: MiniProfiler::ASSET_VERSION, + url: url, + cssUrl: css_url, + version: version, verticalPosition: @config.vertical_position, horizontalPosition: @config.horizontal_position, showTrivial: @config.show_trivial, diff --git a/lib/mini_profiler_rails/railtie.rb b/lib/mini_profiler_rails/railtie.rb index 1d5b0c86..9f892d80 100644 --- a/lib/mini_profiler_rails/railtie.rb +++ b/lib/mini_profiler_rails/railtie.rb @@ -6,6 +6,13 @@ module Rack::MiniProfilerRails extend Rack::MiniProfilerRailsMethods + class Engine < ::Rails::Engine + engine_name 'rack-mini-profiler' + config.assets.paths << File.expand_path('../../html', __FILE__) + config.assets.precompile << 'rack-mini-profiler.js' + config.assets.precompile << 'rack-mini-profiler.css' + end + # call direct if needed to do a defer init def self.initialize!(app) From 59e48118c86c1ec000329d565c5cbdb392d9437c Mon Sep 17 00:00:00 2001 From: OsamaSayegh Date: Wed, 15 Jul 2020 11:47:55 +0300 Subject: [PATCH 2/4] Don't create rails engine unless user opts in --- lib/mini_profiler/config.rb | 12 ++++++++++-- lib/mini_profiler_rails/railtie.rb | 18 +++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/mini_profiler/config.rb b/lib/mini_profiler/config.rb index 1cc4edd4..f137c0cb 100644 --- a/lib/mini_profiler/config.rb +++ b/lib/mini_profiler/config.rb @@ -60,8 +60,7 @@ def self.default :base_url_path, :disable_caching, :enabled, :flamegraph_sample_rate, :logger, :pre_authorize_cb, :skip_paths, :skip_schema_queries, :storage, :storage_failure, :storage_instance, - :storage_options, :user_provider, :enable_advanced_debugging_tools, - :assets_url + :storage_options, :user_provider, :enable_advanced_debugging_tools attr_accessor :skip_sql_param_names, :suppress_encoding, :max_sql_param_length # ui accessors @@ -72,6 +71,15 @@ def self.default # Deprecated options attr_accessor :use_existing_jquery + attr_reader :assets_url + + def assets_url=(lmbda) + if defined?(Rack::MiniProfilerRails) + Rack::MiniProfilerRails.create_engine + end + @assets_url = lmbda + end + def vertical_position position.include?('bottom') ? 'bottom' : 'top' end diff --git a/lib/mini_profiler_rails/railtie.rb b/lib/mini_profiler_rails/railtie.rb index 9f892d80..bf60aaaa 100644 --- a/lib/mini_profiler_rails/railtie.rb +++ b/lib/mini_profiler_rails/railtie.rb @@ -6,13 +6,6 @@ module Rack::MiniProfilerRails extend Rack::MiniProfilerRailsMethods - class Engine < ::Rails::Engine - engine_name 'rack-mini-profiler' - config.assets.paths << File.expand_path('../../html', __FILE__) - config.assets.precompile << 'rack-mini-profiler.js' - config.assets.precompile << 'rack-mini-profiler.css' - end - # call direct if needed to do a defer init def self.initialize!(app) @@ -125,6 +118,17 @@ def self.initialize!(app) @already_initialized = true end + def self.create_engine + return if defined?(Rack::MiniProfilerRails::Engine) + klass = Class.new(::Rails::Engine) do + engine_name 'rack-mini-profiler' + config.assets.paths << File.expand_path('../../html', __FILE__) + config.assets.precompile << 'rack-mini-profiler.js' + config.assets.precompile << 'rack-mini-profiler.css' + end + Rack::MiniProfilerRails.const_set("Engine", klass) + end + def self.subscribe(event, &blk) if ActiveSupport::Notifications.respond_to?(:monotonic_subscribe) ActiveSupport::Notifications.monotonic_subscribe(event) { |*args| blk.call(*args) } From bfd36a7f4dc921c295fcf6fe02dc7c65f02a5cad Mon Sep 17 00:00:00 2001 From: OsamaSayegh Date: Tue, 4 Aug 2020 18:31:24 +0300 Subject: [PATCH 3/4] Update ASSET_VERSION --- lib/html/includes.css | 13 +++++++++---- lib/mini_profiler/asset_version.rb | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/html/includes.css b/lib/html/includes.css index 62a19a02..3b283582 100644 --- a/lib/html/includes.css +++ b/lib/html/includes.css @@ -207,7 +207,8 @@ top: 0px; } .profiler-results.profiler-top.profiler-left { left: 0px; } - .profiler-results.profiler-top.profiler-left.profiler-no-controls .profiler-totals, .profiler-results.profiler-top.profiler-left.profiler-no-controls .profiler-result:last-child .profiler-button, + .profiler-results.profiler-top.profiler-left.profiler-no-controls .profiler-totals, + .profiler-results.profiler-top.profiler-left.profiler-no-controls .profiler-result:last-child .profiler-button, .profiler-results.profiler-top.profiler-left .profiler-controls { -webkit-border-bottom-right-radius: 10px; -moz-border-radius-bottomright: 10px; @@ -217,7 +218,8 @@ border-right: 1px solid #888; } .profiler-results.profiler-top.profiler-right { right: 0px; } - .profiler-results.profiler-top.profiler-right.profiler-no-controls .profiler-totals, .profiler-results.profiler-top.profiler-right.profiler-no-controls .profiler-result:last-child .profiler-button, + .profiler-results.profiler-top.profiler-right.profiler-no-controls .profiler-totals, + .profiler-results.profiler-top.profiler-right.profiler-no-controls .profiler-result:last-child .profiler-button, .profiler-results.profiler-top.profiler-right .profiler-controls { -webkit-border-bottom-left-radius: 10px; -moz-border-radius-bottomleft: 10px; @@ -229,7 +231,8 @@ bottom: 0px; } .profiler-results.profiler-bottom.profiler-left { left: 0px; } - .profiler-results.profiler-bottom.profiler-left.profiler-no-controls .profiler-totals, .profiler-results.profiler-bottom.profiler-left.profiler-no-controls .profiler-result:first-child .profiler-button, + .profiler-results.profiler-bottom.profiler-left.profiler-no-controls .profiler-totals, + .profiler-results.profiler-bottom.profiler-left.profiler-no-controls .profiler-result:first-child .profiler-button, .profiler-results.profiler-bottom.profiler-left .profiler-controls { -webkit-border-top-right-radius: 10px; -moz-border-radius-topright: 10px; @@ -239,7 +242,8 @@ border-right: 1px solid #888; } .profiler-results.profiler-bottom.profiler-right { right: 0px; } - .profiler-results.profiler-bottom.profiler-right.profiler-no-controls .profiler-totals, .profiler-results.profiler-bottom.profiler-right.profiler-no-controls .profiler-result:first-child .profiler-button, + .profiler-results.profiler-bottom.profiler-right.profiler-no-controls .profiler-totals, + .profiler-results.profiler-bottom.profiler-right.profiler-no-controls .profiler-result:first-child .profiler-button, .profiler-results.profiler-bottom.profiler-right .profiler-controls { -webkit-border-bottom-top-radius: 10px; -moz-border-radius-topleft: 10px; @@ -345,6 +349,7 @@ @media print { .profiler-results { display: none; } } + .profiler-queries-bg { z-index: 2147483642; display: none; diff --git a/lib/mini_profiler/asset_version.rb b/lib/mini_profiler/asset_version.rb index 3d7aa2d4..1ef9a784 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 = '015eebd28435014a417b0b8cf057054d' + ASSET_VERSION = '435a6923efca239f3ae0a09ac2d09acb' end end From 0a6504cec91e1f114c6a1f3b9f12a59b2b32afd0 Mon Sep 17 00:00:00 2001 From: OsamaSayegh Date: Tue, 4 Aug 2020 20:19:27 +0300 Subject: [PATCH 4/4] Add documentation --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 0349ded6..2aaa7285 100644 --- a/README.md +++ b/README.md @@ -329,6 +329,15 @@ _Note:_ The GUID (`data-version` and the `?v=` parameter on the `src`) will chan #### Using MiniProfiler's built in route for apps without HTML responses MiniProfiler also ships with a `/rack-mini-profiler/requests` route that displays the speed badge on a blank HTML page. This can be useful when profiling an application that does not render HTML. +#### Register MiniProfiler's assets in the Rails assets pipeline +MiniProfiler can be configured so it registers its assets in the assets pipeline. To do that, you'll need to provide a lambda (or proc) to the `assets_url` config (see the below section). The callback will receive 3 arguments which are: `name` represents asset name (currently it's either `rack-mini-profiling.js` or `rack-mini-profiling.css`), `assets_version` is a 32 characters long hash of MiniProfiler's assets, and `env` which is the `env` object of the request. MiniProfiler expects the `assets_url` callback to return a URL from which the asset can be loaded (the return value will be used as a `href`/`src` attribute in the DOM). If the `assets_url` callback is not set (the default) or it returns a non-truthy value, MiniProfiler will fallback to loading assets from its own middleware (`/mini-profiler-resources/*`). The following callback should work for most applications: + +```ruby +Rack::MiniProfiler.config.assets_url = ->(name, version, env) { + ActionController::Base.helpers.asset_path(name) +} +``` + ### Configuration Options You can set configuration options using the configuration accessor on `Rack::MiniProfiler`. @@ -360,6 +369,7 @@ max_traces_to_show|20|Maximum number of mini profiler timing blocks to show on o html_container|`body`|The HTML container (as a jQuery selector) to inject the mini_profiler UI into show_total_sql_count|`false`|Displays the total number of SQL executions. enable_advanced_debugging_tools|`false`|Enables sensitive debugging tools that can be used via the UI. In production we recommend keeping this disabled as memory and environment debugging tools can expose contents of memory that may contain passwords. +assets_url|`nil`|See the "Register MiniProfiler's assets in the Rails assets pipeline" section above. ### Using MiniProfiler with `Rack::Deflate` middleware