Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow assets to be precompiled with Sprockets #456

Merged
merged 4 commits into from Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -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`.
Expand Down Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions lib/html/includes.css
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -345,6 +349,7 @@
@media print {
.profiler-results {
display: none; } }

.profiler-queries-bg {
z-index: 2147483642;
display: none;
Expand Down
7 changes: 5 additions & 2 deletions lib/html/includes.js
Expand Up @@ -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,
Expand All @@ -996,7 +997,8 @@ var MiniProfiler = (function() {
toggleShortcut: toggleShortcut,
startHidden: startHidden,
collapseResults: collapseResults,
htmlContainer: htmlContainer
htmlContainer: htmlContainer,
cssUrl: cssUrl
};
})();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1398,4 +1400,5 @@ var MiniProfiler = (function() {
};
})();

window.MiniProfiler = MiniProfiler;
MiniProfiler.init();
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.

3 changes: 3 additions & 0 deletions lib/html/rack-mini-profiler.css
@@ -0,0 +1,3 @@
/*
*= require ./includes
*/
2 changes: 2 additions & 0 deletions lib/html/rack-mini-profiler.js
@@ -0,0 +1,2 @@
//= require ./includes
//= require ./vendor
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 = '015eebd28435014a417b0b8cf057054d'
ASSET_VERSION = '435a6923efca239f3ae0a09ac2d09acb'
end
end
9 changes: 9 additions & 0 deletions lib/mini_profiler/config.rb
Expand Up @@ -71,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
Expand Down
12 changes: 11 additions & 1 deletion lib/mini_profiler/profiler.rb
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions lib/mini_profiler_rails/railtie.rb
Expand Up @@ -118,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) }
Expand Down