Skip to content

Commit 4cc0d15

Browse files
committed
Merge pull request #57 from Shopify/fix-auto-compression
Do not gzip assets that are already gzipped
2 parents 6718994 + 0f76ab6 commit 4cc0d15

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

lib/sprockets/asset.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ def stale?(environment)
139139
# Save asset to disk.
140140
def write_to(filename, options = {})
141141
# Gzip contents if filename has '.gz'
142-
options[:compress] ||= File.extname(filename) == '.gz'
142+
unless options.key?(:compress)
143+
options[:compress] = File.extname(filename) == '.gz' && File.extname(logical_path) != '.gz'
144+
end
143145

144146
FileUtils.mkdir_p File.dirname(filename)
145147

lib/sprockets/static_asset.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def to_path
2121
# Save asset to disk.
2222
def write_to(filename, options = {})
2323
# Gzip contents if filename has '.gz'
24-
options[:compress] ||= File.extname(filename) == '.gz'
24+
unless options.key?(:compress)
25+
options[:compress] = File.extname(filename) == '.gz' && File.extname(logical_path) != '.gz'
26+
end
2527

2628
FileUtils.mkdir_p File.dirname(filename)
2729

test/fixtures/asset/archive.tar.gz

170 Bytes
Binary file not shown.

test/test_asset.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ def self.test(name, &block)
7777
assert !File.exist?(target)
7878
end
7979
end
80+
81+
test "do not gzip assets that are already gzipped" do
82+
@asset = @env.find_asset('archive.tar.gz')
83+
target = fixture_path('asset/tmp.tar.gz')
84+
begin
85+
@asset.write_to(target)
86+
assert File.exist?(target)
87+
assert_equal Digest::MD5.hexdigest(@asset.to_s), Digest::MD5.hexdigest(File.read(target))
88+
ensure
89+
FileUtils.rm(target) if File.exist?(target)
90+
assert !File.exist?(target)
91+
end
92+
end
8093
end
8194

8295
module FreshnessTests

0 commit comments

Comments
 (0)