Skip to content

Commit 4034375

Browse files
committed
Fast-path compute_asset_path when digests are off
When digests are turned off, asset path lookups have been asking for digest paths anyway, then ignoring them and returning the logical path. So the digest path lookup is acting as a presence check that unnecessarily compiles assets to calculate the digest path. Fix: When digests are turned off, check that the asset exists only. Introduced at 32738f9
1 parent 77098c5 commit 4034375

File tree

1 file changed

+58
-20
lines changed

1 file changed

+58
-20
lines changed

lib/sprockets/rails/helper.rb

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,23 @@ def assets_environment
6060
end
6161

6262
def compute_asset_path(path, options = {})
63-
if digest_path = asset_digest_path(path, options)
64-
path = digest_path if digest_assets
65-
path += "?body=1" if options[:debug] && !using_sprockets4?
66-
File.join(assets_prefix || "/", path)
63+
debug = options[:debug]
64+
65+
if asset_path = resolve_asset_path(path, debug)
66+
File.join(assets_prefix || "/", legacy_debug_path(asset_path, debug))
6767
else
6868
super
6969
end
7070
end
7171

72+
# Resolve the asset path against the Sprockets manifest or environment.
73+
# Returns nil if it's an asset we don't know about.
74+
def resolve_asset_path(path, allow_non_precompiled = false) #:nodoc:
75+
resolve_asset do |resolver|
76+
resolver.asset_path path, digest_assets, allow_non_precompiled
77+
end
78+
end
79+
7280
# Expand asset path to digested form.
7381
#
7482
# path - String path
@@ -91,7 +99,7 @@ def asset_integrity(path, options = {})
9199
path = path_with_extname(path, options)
92100

93101
resolve_asset do |resolver|
94-
resolver.integrity path, options[:debug]
102+
resolver.integrity path
95103
end
96104
end
97105

@@ -192,7 +200,7 @@ def lookup_debug_asset(path, options = {})
192200
path = path_with_extname(path, options)
193201

194202
resolve_asset do |resolver|
195-
resolver.find_debug_asset path, options[:debug]
203+
resolver.find_debug_asset path
196204
end
197205
end
198206

@@ -218,6 +226,15 @@ def asset_resolver_strategies
218226
HelperAssetResolvers[name].new(self)
219227
end
220228
end
229+
230+
# Append ?body=1 if debug is on and we're on old Sprockets.
231+
def legacy_debug_path(path, debug)
232+
if debug && !using_sprockets4?
233+
"#{path}?body=1"
234+
else
235+
path
236+
end
237+
end
221238
end
222239

223240
# Use a separate module since Helper is mixed in and we needn't pollute
@@ -240,23 +257,29 @@ def initialize(view)
240257
raise ArgumentError, 'config.assets.resolve_with includes :manifest, but app.assets_manifest is nil' unless @manifest
241258
end
242259

243-
def digest_path(path, debug = false)
260+
def asset_path(path, digest, allow_non_precompiled = false)
261+
if digest
262+
digest_path path, allow_non_precompiled
263+
end
264+
end
265+
266+
def digest_path(path, allow_non_precompiled = false)
244267
@manifest.assets[path]
245268
end
246269

247-
def integrity(path, debug = false)
248-
if meta = metadata(path, debug)
270+
def integrity(path)
271+
if meta = metadata(path)
249272
meta["integrity"]
250273
end
251274
end
252275

253-
def find_debug_asset(path, debug = false)
276+
def find_debug_asset(path)
254277
nil
255278
end
256279

257280
private
258-
def metadata(path, debug = false)
259-
if digest_path = digest_path(path, debug)
281+
def metadata(path)
282+
if digest_path = digest_path(path)
260283
@manifest.files[digest_path]
261284
end
262285
end
@@ -269,18 +292,31 @@ def initialize(view)
269292
@precompiled_asset_checker = view.precompiled_asset_checker
270293
end
271294

272-
def digest_path(path, debug = false)
295+
def asset_path(path, digest, allow_non_precompiled = false)
296+
# Digests enabled? Do the work to calculate the full asset path.
297+
if digest
298+
digest_path path, allow_non_precompiled
299+
300+
# Otherwise, ask the Sprockets environment whether the asset exists
301+
# and check whether it's also precompiled for production deploys.
302+
elsif find_asset(path)
303+
raise_unless_precompiled_asset path unless allow_non_precompiled
304+
path
305+
end
306+
end
307+
308+
def digest_path(path, allow_non_precompiled = false)
273309
if asset = find_asset(path)
274-
raise_unless_precompiled_asset asset.logical_path unless debug
310+
raise_unless_precompiled_asset path unless allow_non_precompiled
275311
asset.digest_path
276312
end
277313
end
278314

279-
def integrity(path, debug = false)
315+
def integrity(path)
280316
find_asset(path).try :integrity
281317
end
282318

283-
def find_debug_asset(path, options = {})
319+
def find_debug_asset(path)
284320
if asset = find_asset(path, pipeline: :debug)
285321
raise_unless_precompiled_asset asset.logical_path.sub('.debug', '')
286322
asset
@@ -292,10 +328,12 @@ def find_asset(path, options = {})
292328
@env[path, options]
293329
end
294330

295-
def raise_unless_precompiled_asset(logical_path)
296-
if !@precompiled_asset_checker.call(logical_path)
297-
raise Helper::AssetNotPrecompiled.new(logical_path)
298-
end
331+
def precompiled?(path)
332+
@precompiled_asset_checker.call path
333+
end
334+
335+
def raise_unless_precompiled_asset(path)
336+
raise Helper::AssetNotPrecompiled.new(path) unless precompiled?(path)
299337
end
300338
end
301339
end

0 commit comments

Comments
 (0)