Skip to content

Commit 7fa2b4d

Browse files
authored
Merge pull request #355 from kmcphillips/master
Silence asset requests with config.assets.quiet
2 parents b4f9006 + 830e2d9 commit 7fa2b4d

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Add additional assets to compile on deploy. Defaults to `application.js`, `appli
5959

6060
Add additional load paths to this Array. Rails includes `app/assets`, `lib/assets` and `vendor/assets` for you already. Plugins might want to add their custom paths to this.
6161

62+
**`config.assets.quiet`**
63+
64+
Suppresses logger output for asset requests. Uses the `config.assets.prefix` path to match and wraps the request in a `Rails.logger.silence{ }` which silences all but `ERROR` level. Defaults to `false`.
6265

6366
**`config.assets.version`**
6467

lib/sprockets/rails/quiet_assets.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Sprockets
2+
module Rails
3+
class QuietAssets
4+
def initialize(app)
5+
@app = app
6+
@assets_regex = %r(\A/{0,2}#{::Rails.application.config.assets.prefix})
7+
end
8+
9+
def call(env)
10+
if env['PATH_INFO'] =~ @assets_regex
11+
::Rails.logger.silence { @app.call(env) }
12+
else
13+
@app.call(env)
14+
end
15+
end
16+
end
17+
end
18+
end

lib/sprockets/railtie.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'sprockets'
77
require 'sprockets/rails/context'
88
require 'sprockets/rails/helper'
9+
require 'sprockets/rails/quiet_assets'
910
require 'sprockets/rails/route_wrapper'
1011
require 'sprockets/rails/version'
1112
require 'set'
@@ -97,6 +98,7 @@ def configure(&block)
9798
config.assets.precompile = []
9899
config.assets.prefix = "/assets"
99100
config.assets.manifest = nil
101+
config.assets.quiet = false
100102

101103
initializer :set_default_precompile do |app|
102104
if using_sprockets4?
@@ -107,6 +109,12 @@ def configure(&block)
107109
end
108110
end
109111

112+
initializer :quiet_assets do |app|
113+
if app.config.assets.quiet
114+
app.middleware.insert_before ::Rails::Rack::Logger, ::Sprockets::Rails::QuietAssets
115+
end
116+
end
117+
110118
config.assets.version = ""
111119
config.assets.debug = false
112120
config.assets.compile = true

test/test_quiet_assets.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'active_support'
2+
require 'active_support/testing/isolation'
3+
require 'active_support/log_subscriber/test_helper'
4+
require 'minitest/autorun'
5+
6+
require 'sprockets/railtie'
7+
require 'rails'
8+
9+
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
10+
11+
class TestQuietAssets < Minitest::Test
12+
include ActiveSupport::Testing::Isolation
13+
14+
ROOT_PATH = Pathname.new(File.expand_path("../../tmp/app", __FILE__))
15+
ASSET_PATH = ROOT_PATH.join("app","assets", "config")
16+
17+
def setup
18+
FileUtils.mkdir_p(ROOT_PATH)
19+
Dir.chdir(ROOT_PATH)
20+
21+
@app = Class.new(Rails::Application)
22+
@app.config.eager_load = false
23+
@app.config.logger = ActiveSupport::Logger.new("/dev/null")
24+
25+
FileUtils.mkdir_p(ASSET_PATH)
26+
File.open(ASSET_PATH.join("manifest.js"), "w") { |f| f << "" }
27+
28+
@app.initialize!
29+
30+
Rails.logger.level = Logger::DEBUG
31+
end
32+
33+
def test_silences_with_default_prefix
34+
assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/assets/stylesheets/application.css")
35+
end
36+
37+
def test_silencess_with_custom_prefix
38+
Rails.application.config.assets.prefix = "path/to"
39+
assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/path/to/thing")
40+
end
41+
42+
def test_does_not_silence_without_match
43+
assert_equal Logger::DEBUG, middleware.call("PATH_INFO" => "/path/to/thing")
44+
end
45+
46+
private
47+
48+
def middleware
49+
@middleware ||= Sprockets::Rails::QuietAssets.new(->(env) { Rails.logger.level })
50+
end
51+
end

test/test_railtie.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,25 @@ def test_direct_build_environment_call
384384
assert_equal ["#{ROOT}/app/assets/config", "#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
385385
env.paths.sort
386386
end
387+
388+
def test_quiet_assets_defaults_to_off
389+
app.initialize!
390+
app.load_tasks
391+
392+
assert_equal false, app.config.assets.quiet
393+
refute app.config.middleware.include?(Sprockets::Rails::QuietAssets)
394+
end
395+
396+
def test_quiet_assets_inserts_middleware
397+
app.configure do
398+
config.assets.quiet = true
399+
end
400+
app.initialize!
401+
app.load_tasks
402+
middleware = app.config.middleware
403+
404+
assert_equal true, app.config.assets.quiet
405+
assert middleware.include?(Sprockets::Rails::QuietAssets)
406+
assert middleware.each_cons(2).include?([Sprockets::Rails::QuietAssets, Rails::Rack::Logger])
407+
end
387408
end

0 commit comments

Comments
 (0)