Skip to content

Commit bbb44d2

Browse files
committed
Merge pull request #275 from jeremy/defer-asset-precompile-from-initialization
Speed up app boot by deferring asset precompile
2 parents 2462281 + 8fc9e0b commit bbb44d2

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

lib/sprockets/rails/helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def initialize(source)
2121
include Sprockets::Rails::Utils
2222

2323
VIEW_ACCESSORS = [:assets_environment, :assets_manifest,
24-
:assets_precompile, :precompiled_assets,
24+
:assets_precompile, :precompiled_asset_checker,
2525
:assets_prefix, :digest_assets, :debug_assets]
2626

2727
def self.included(klass)
@@ -82,7 +82,7 @@ def asset_digest_path(path, options = {})
8282
if environment = assets_environment
8383
if asset = environment[path]
8484
unless options[:debug]
85-
if !precompiled_assets.include?(asset.logical_path)
85+
if !precompiled_asset_checker.call(asset.logical_path)
8686
raise AssetNotPrecompiled.new(asset.logical_path)
8787
end
8888
end
@@ -220,7 +220,7 @@ def lookup_debug_asset(path, options = {})
220220

221221
if asset = env[path, pipeline: :debug]
222222
original_path = asset.logical_path.gsub('.debug', '')
223-
unless precompiled_assets.include?(original_path)
223+
unless precompiled_asset_checker.call(original_path)
224224
raise AssetNotPrecompiled.new(original_path)
225225
end
226226
end

lib/sprockets/rails/utils.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ module Utils
66
def using_sprockets4?
77
Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('4.0.0')
88
end
9-
10-
# Internal: Generate a Set of all precompiled assets logical paths.
11-
def build_precompiled_list(manifest, assets)
12-
manifest.find(assets || []).map(&:logical_path)
13-
end
149
end
1510
end
1611
end

lib/sprockets/railtie.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,18 @@ class Configuration
2727
# Returns Sprockets::Manifest for app config.
2828
attr_accessor :assets_manifest
2929

30-
# Returns array of already precompiled assets
31-
attr_accessor :precompiled_assets
30+
# Called from asset helpers to alert you if you reference an asset URL that
31+
# isn't precompiled and hence won't be available in production.
32+
def asset_precompiled?(logical_path)
33+
precompiled_assets.include? logical_path
34+
end
35+
36+
# Lazy-load the precompile list so we don't cause asset compilation at app
37+
# boot time, but ensure we cache the list so we don't recompute it for each
38+
# request or test case.
39+
def precompiled_assets
40+
@precompiled_assets ||= assets_manifest.find(config.assets.precompile).map(&:logical_path)
41+
end
3242
end
3343

3444
class Engine < Railtie
@@ -155,11 +165,8 @@ def self.build_manifest(app)
155165
app.routes.prepend do
156166
mount app.assets => config.assets.prefix
157167
end
158-
app.assets_manifest = build_manifest(app)
159-
app.precompiled_assets = build_precompiled_list(app.assets_manifest, config.assets.precompile)
160-
else
161-
app.assets_manifest = build_manifest(app)
162168
end
169+
app.assets_manifest = build_manifest(app)
163170

164171
ActionDispatch::Routing::RouteWrapper.class_eval do
165172
class_attribute :assets_prefix
@@ -184,7 +191,9 @@ def self.build_manifest(app)
184191

185192
self.assets_environment = app.assets
186193
self.assets_manifest = app.assets_manifest
187-
self.precompiled_assets = app.precompiled_assets
194+
195+
# Expose the app precompiled asset check to the view
196+
self.precompiled_asset_checker = -> logical_path { app.asset_precompiled? logical_path }
188197
end
189198
end
190199
end

test/test_helper.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def setup
2525
@view.assets_manifest = @manifest
2626
@view.assets_prefix = "/assets"
2727
@view.assets_precompile = %w( manifest.js )
28-
@view.precompiled_assets = @view.build_precompiled_list(@manifest, @view.assets_precompile)
28+
precompiled_assets = @manifest.find(@view.assets_precompile).map(&:logical_path)
29+
@view.precompiled_asset_checker = -> logical_path { precompiled_assets.include? logical_path }
2930
@view.request = ActionDispatch::Request.new({
3031
"rack.url_scheme" => "https"
3132
})
@@ -730,7 +731,8 @@ def test_stylesheet_link_tag_integrity
730731
class AssetUrlHelperLinksTarget < HelperTest
731732
def test_precompile_allows_links
732733
@view.assets_precompile = ["url.css"]
733-
@view.precompiled_assets = @view.build_precompiled_list(@manifest, @view.assets_precompile)
734+
precompiled_assets = @manifest.find(@view.assets_precompile).map(&:logical_path)
735+
@view.precompiled_asset_checker = -> logical_path { precompiled_assets.include? logical_path }
734736
assert @view.asset_path("url.css")
735737
assert @view.asset_path("logo.png")
736738

0 commit comments

Comments
 (0)