Skip to content

Commit

Permalink
Shorter digest length (#173)
Browse files Browse the repository at this point in the history
* Shorter digest length

* shorten digest via first
  • Loading branch information
wlipa authored Dec 29, 2023
1 parent c2a87b5 commit 40ce740
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/propshaft/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def length
end

def digest
@digest ||= Digest::SHA1.hexdigest("#{content}#{version}")
@digest ||= Digest::SHA1.hexdigest("#{content}#{version}").first(8)
end

def digested_path
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/.manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "one.txt": "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt" }
{ "one.txt": "one-f2e1ec14.txt" }
File renamed without changes.
10 changes: 5 additions & 5 deletions test/propshaft/asset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ class Propshaft::AssetTest < ActiveSupport::TestCase
end

test "digest" do
assert_equal "f2e1ec14d6856e1958083094170ca6119c529a73", find_asset("one.txt").digest
assert_equal "f2e1ec14", find_asset("one.txt").digest
end

test "fresh" do
assert find_asset("one.txt").fresh?("f2e1ec14d6856e1958083094170ca6119c529a73")
assert_not find_asset("one.txt").fresh?("e206c34fe404c8e2f25d60dd8303f61c02b8d381")
assert find_asset("one.txt").fresh?("f2e1ec14")
assert_not find_asset("one.txt").fresh?("e206c34f")

assert find_asset("file-already-abcdefVWXYZ0123456789_-.digested.css").fresh?(nil)
end

test "digested path" do
assert_equal "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt",
assert_equal "one-f2e1ec14.txt",
find_asset("one.txt").digested_path.to_s

assert_equal "file-already-abcdefVWXYZ0123456789_-.digested.css",
Expand All @@ -38,7 +38,7 @@ class Propshaft::AssetTest < ActiveSupport::TestCase
assert_equal "file-already-abcdefVWXYZ0123456789_-.digested.debug.css",
find_asset("file-already-abcdefVWXYZ0123456789_-.digested.debug.css").digested_path.to_s

assert_equal "file-not.digested-e206c34fe404c8e2f25d60dd8303f61c02b8d381.css",
assert_equal "file-not.digested-e206c34f.css",
find_asset("file-not.digested.css").digested_path.to_s
end

Expand Down
32 changes: 16 additions & 16 deletions test/propshaft/compiler/css_asset_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,62 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase

test "basic" do
compiled = compile_asset_with_content(%({ background: url(file.jpg); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "blank spaces around name" do
compiled = compile_asset_with_content(%({ background: url( file.jpg ); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "quotes around name" do
compiled = compile_asset_with_content(%({ background: url("file.jpg"); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "single quotes around name" do
compiled = compile_asset_with_content(%({ background: url('file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "root directory" do
compiled = compile_asset_with_content(%({ background: url('/file.jpg'); }))
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "same directory" do
compiled = compile_asset_with_content(%({ background: url('./file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "subdirectory" do
compiled = compile_asset_with_content(%({ background: url('./images/file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/images\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/images\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "parent directory" do
compiled = compile_asset_with_content(%({ background: url('../file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "grandparent directory" do
compiled = compile_asset_with_content(%({ background: url('../../file.jpg'); }))
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "sibling directory" do
compiled = compile_asset_with_content(%({ background: url('../sibling/file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/sibling\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/sibling\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "mixed" do
compiled = compile_asset_with_content(%({ mask-image: image(url(file.jpg), skyblue, linear-gradient(rgba(0, 0, 0, 1.0), transparent)); }))
assert_match(/{ mask-image: image\(url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\), skyblue, linear-gradient\(rgba\(0, 0, 0, 1.0\), transparent\)\); }/, compiled)
assert_match(/{ mask-image: image\(url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\), skyblue, linear-gradient\(rgba\(0, 0, 0, 1.0\), transparent\)\); }/, compiled)
end

test "multiple" do
compiled = compile_asset_with_content(%({ content: url(file.svg) url(file.svg); }))
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg"\) url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg"\); }/, compiled)
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg"\) url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg"\); }/, compiled)
end

test "url" do
Expand All @@ -95,12 +95,12 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase

test "fingerprint" do
compiled = compile_asset_with_content(%({ background: url('/file.jpg?30af91bf14e37666a085fb8a161ff36d'); }))
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{40}.jpg\?30af91bf14e37666a085fb8a161ff36d"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{8}.jpg\?30af91bf14e37666a085fb8a161ff36d"\); }/, compiled)
end

test "svg anchor" do
compiled = compile_asset_with_content(%({ content: url(file.svg#rails); }))
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg#rails"\); }/, compiled)
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg#rails"\); }/, compiled)
end

test "svg mask encoded anchor" do
Expand All @@ -110,7 +110,7 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase

test "non greedy anchors" do
compiled = compile_asset_with_content(%({ content: url(file.svg#demo) url(file.svg#demo); }))
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg#demo"\) url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg#demo"\); }/, compiled)
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg#demo"\) url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg#demo"\); }/, compiled)
end

test "missing asset" do
Expand All @@ -122,7 +122,7 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase
@options.relative_url_root = "/url-root"

compiled = compile_asset_with_content(%({ background: url(file.jpg); }))
assert_match(/{ background: url\("\/url-root\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/url-root\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

private
Expand Down
12 changes: 6 additions & 6 deletions test/propshaft/compiler/source_mapping_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase
end

test "matching source map" do
assert_match %r{//# sourceMappingURL=/assets/source.js-[a-z0-9]{40}\.map},
assert_match %r{//# sourceMappingURL=/assets/source.js-[a-z0-9]{8}\.map},
compile_asset(find_asset("source.js", fixture_path: "mapped"))
assert_match %r{/\*# sourceMappingURL=/assets/source.css-[a-z0-9]{40}\.map},
assert_match %r{/\*# sourceMappingURL=/assets/source.css-[a-z0-9]{8}\.map},
compile_asset(find_asset("source.css", fixture_path: "mapped"))
end

test "matching nested source map" do
assert_match %r{//# sourceMappingURL=/assets/nested/another-source.js-[a-z0-9]{40}\.map},
assert_match %r{//# sourceMappingURL=/assets/nested/another-source.js-[a-z0-9]{8}\.map},
compile_asset(find_asset("nested/another-source.js", fixture_path: "mapped"))
end

Expand All @@ -38,9 +38,9 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase
end

test "sourceMappingURL not at the beginning of the line, but at end of file, is processed" do
assert_match %r{//# sourceMappingURL=/assets/sourceMappingURL-not-at-start.js-[a-z0-9]{40}\.map},
assert_match %r{//# sourceMappingURL=/assets/sourceMappingURL-not-at-start.js-[a-z0-9]{8}\.map},
compile_asset(find_asset("sourceMappingURL-not-at-start.js", fixture_path: "mapped"))
assert_match %r{/\*# sourceMappingURL=/assets/sourceMappingURL-not-at-start.css-[a-z0-9]{40}\.map \*/},
assert_match %r{/\*# sourceMappingURL=/assets/sourceMappingURL-not-at-start.css-[a-z0-9]{8}\.map \*/},
compile_asset(find_asset("sourceMappingURL-not-at-start.css", fixture_path: "mapped"))
end

Expand All @@ -56,7 +56,7 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase
test "relative url root" do
@options.relative_url_root = "/url-root"

assert_match %r{//# sourceMappingURL=/url-root/assets/source.js-[a-z0-9]{40}\.map},
assert_match %r{//# sourceMappingURL=/url-root/assets/source.js-[a-z0-9]{8}\.map},
compile_asset(find_asset("source.js", fixture_path: "mapped"))
end

Expand Down
2 changes: 1 addition & 1 deletion test/propshaft/compilers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Propshaft::CompilersTest < ActiveSupport::TestCase

test "replace asset-path function in css with digested url" do
@assembly.compilers.register "text/css", Propshaft::Compiler::CssAssetUrls
assert_match(/"\/assets\/archive-[a-z0-9]{40}.svg/, @assembly.compilers.compile(find_asset("another.css")))
assert_match(/"\/assets\/archive-[a-z0-9]{8}.svg/, @assembly.compilers.compile(find_asset("another.css")))
end

private
Expand Down
8 changes: 4 additions & 4 deletions test/propshaft/load_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ class Propshaft::LoadPathTest < ActiveSupport::TestCase

test "manifest" do
@load_path.manifest.tap do |manifest|
assert_equal "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt", manifest["one.txt"]
assert_equal "nested/three-6c2b86a0206381310375abdd9980863c2ea7b2c3.txt", manifest["nested/three.txt"]
assert_equal "one-f2e1ec14.txt", manifest["one.txt"]
assert_equal "nested/three-6c2b86a0.txt", manifest["nested/three.txt"]
end
end

test "manifest with version" do
@load_path = Propshaft::LoadPath.new(@load_path.paths, version: "1")
@load_path.manifest.tap do |manifest|
assert_equal "one-c9373b685d5a63e4a1de7c6836a73239df552e2b.txt", manifest["one.txt"]
assert_equal "nested/three-a41a5d38da5afe428eca74b243f50405f28a6b54.txt", manifest["nested/three.txt"]
assert_equal "one-c9373b68.txt", manifest["one.txt"]
assert_equal "nested/three-a41a5d38.txt", manifest["nested/three.txt"]
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/propshaft/output_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class Propshaft::OutputPathTest < ActiveSupport::TestCase
setup do
@manifest = {
".manifest.json": ".manifest.json",
"one.txt": "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt"
"one.txt": "one-f2e1ec14.txt"
}.stringify_keys
@output_path = Propshaft::OutputPath.new(Pathname.new("#{__dir__}/../fixtures/output"), @manifest)
end

test "files" do
files = @output_path.files

file = files["one-f2e1ec14d6856e1958083094170ca6119c529a73.txt"]
file = files["one-f2e1ec14.txt"]
assert_equal "one.txt", file[:logical_path]
assert_equal "f2e1ec14d6856e1958083094170ca6119c529a73", file[:digest]
assert_equal "f2e1ec14", file[:digest]
assert file[:mtime].is_a?(Time)
end

Expand Down
6 changes: 3 additions & 3 deletions test/propshaft/processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ class Propshaft::ProcessorTest < ActiveSupport::TestCase

test "manifest is written" do
processed do |processor|
assert_equal "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt",
assert_equal "one-f2e1ec14.txt",
JSON.parse(processor.output_path.join(".manifest.json").read)["one.txt"]
end
end

test "assets are copied" do
processed do |processor|
digested_asset_name = "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt"
digested_asset_name = "one-f2e1ec14.txt"
assert processor.output_path.join(digested_asset_name).exist?

nested_digested_asset_name = "nested/three-6c2b86a0206381310375abdd9980863c2ea7b2c3.txt"
nested_digested_asset_name = "nested/three-6c2b86a0.txt"
assert processor.output_path.join(nested_digested_asset_name).exist?
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/propshaft/resolver/dynamic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Propshaft::Resolver::DynamicTest < ActiveSupport::TestCase
end

test "resolving present asset returns uri path" do
assert_equal "/assets/one-f2e1ec14d6856e1958083094170ca6119c529a73.txt",
assert_equal "/assets/one-f2e1ec14.txt",
@resolver.resolve("one.txt")
end

Expand Down
2 changes: 1 addition & 1 deletion test/propshaft/resolver/static_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Propshaft::Resolver::StaticTest < ActiveSupport::TestCase

test "resolving present asset returns uri path" do
assert_equal \
"/assets/one-f2e1ec14d6856e1958083094170ca6119c529a73.txt",
"/assets/one-f2e1ec14.txt",
@resolver.resolve("one.txt")
end

Expand Down
4 changes: 2 additions & 2 deletions test/propshaft/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class Propshaft::ServerTest < ActiveSupport::TestCase
get "/#{asset.digested_path}"

assert_equal 200, last_response.status
assert_equal "94", last_response.headers['content-length']
assert_equal "62", last_response.headers['content-length']
assert_equal "text/css", last_response.headers['content-type']
assert_equal "Accept-Encoding", last_response.headers['vary']
assert_equal asset.digest, last_response.headers['etag']
assert_equal "public, max-age=31536000, immutable", last_response.headers['cache-control']
assert_equal ".hero { background: url(\"/foobar/source/file-3e6a129785ee3caf8eff23db339997e85334bfa9.jpg\") }\n",
assert_equal ".hero { background: url(\"/foobar/source/file-3e6a1297.jpg\") }\n",
last_response.body
end

Expand Down
6 changes: 3 additions & 3 deletions test/propshaft_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class PropshaftIntegrationTest < ActionDispatch::IntegrationTest

assert_response :success

assert_select 'link[href="/assets/hello_world-4137140a1298c3924d5f7135617c23e23fb167a8.css"]'
assert_select 'link[href="/assets/goodbye-b1dc9940e9800d8bc96f7434617c043e58277419.css"]'
assert_select 'link[href="/assets/hello_world-4137140a.css"]'
assert_select 'link[href="/assets/goodbye-b1dc9940.css"]'

assert_select 'script[src="/assets/hello_world-888761f849ba63a95a56f6ef898a9eb70ca4c46e.js"]'
assert_select 'script[src="/assets/hello_world-888761f8.js"]'
end

test "should raise an exception when resolving nonexistent assets" do
Expand Down

0 comments on commit 40ce740

Please sign in to comment.