Skip to content

Commit

Permalink
Do not generate digests if filename contains a digest
Browse files Browse the repository at this point in the history
  • Loading branch information
brenogazzola committed Sep 11, 2021
1 parent 24c9727 commit 1283b98
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/sprockets/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ def to_hash
#
# Returns String.
def digest_path
logical_path.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" }
if DigestUtils.already_digested?(@name)
logical_path
else
logical_path.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" }
end
end

# Public: Return load path + logical path with digest spliced in.
Expand Down
13 changes: 13 additions & 0 deletions lib/sprockets/digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ def hexdigest_integrity_uri(hexdigest)
integrity_uri(unpack_hexdigest(hexdigest))
end

# Internal: Checks an asset name for a valid digest
#
# name - The name of the asset
#
# Returns true if the name contains a digest recognized by one of the valid digest classes
def already_digested?(name)
return if name.nil?

if hexdigest = name.scan(/[a-fA-F0-9]+\z/).last
detect_digest_class(unpack_hexdigest(hexdigest))
end
end

private
def build_digest(obj)
digest = digest_class.new
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// =require "project"
// =require "users"

document.on('dom:loaded', function() {
$('search').focus();
});
20 changes: 20 additions & 0 deletions test/test_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,26 @@ def read(logical_path)
end
end

class PreDigestedAssetTest < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new
@env.append_path(fixture_path('asset'))
@env.cache = {}

@pipeline = nil
@asset = @env['application.js']
end

test "digest path" do
assert_equal "application-d41d8cd98f00b204e9800998ecf8427e.js",
asset("application-d41d8cd98f00b204e9800998ecf8427e.js").digest_path
end

def asset(logical_path, options = {})
@env.find_asset(logical_path, **{pipeline: @pipeline}.merge(options))
end
end


class DebugAssetTest < Sprockets::TestCase
def setup
Expand Down
18 changes: 18 additions & 0 deletions test/test_digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,22 @@ def test_hexdigest_integrity_uri
assert_equal "sha512-+uuYUxxe7oWIShQrWEmMn/fixz/rxDP4qcAZddXLDM3nN8/tpk1ZC2jXQk6N+mXE65jwfzNVUJL/qjA3y9KbuQ==",
hexdigest_integrity_uri(sha512)
end

def test_already_digested
md5 = Digest::MD5.new.hexdigest
sha1 = Digest::SHA1.new.hexdigest
sha256 = Digest::SHA256.new.hexdigest
sha384 = Digest::SHA384.new.hexdigest
sha512 = Digest::SHA512.new.hexdigest

refute already_digested?(nil)
refute already_digested?("application-d41d8cd98f00b204e9800998ecf8427e-")
refute already_digested?("application-d41d8cd98f00b204e9800998ecf8427z")

assert already_digested?("application-" + md5)
assert already_digested?("application-" + sha1)
assert already_digested?("application-" + sha256)
assert already_digested?("application-" + sha384)
assert already_digested?("application-" + sha512)
end
end

0 comments on commit 1283b98

Please sign in to comment.