Skip to content

Commit

Permalink
Merge pull request #57 from Shopify/fix-auto-compression
Browse files Browse the repository at this point in the history
Do not gzip assets that are already gzipped
  • Loading branch information
rafaelfranca committed Jun 26, 2015
2 parents 6718994 + 0f76ab6 commit 4cc0d15
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/sprockets/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def stale?(environment)
# Save asset to disk.
def write_to(filename, options = {})
# Gzip contents if filename has '.gz'
options[:compress] ||= File.extname(filename) == '.gz'
unless options.key?(:compress)
options[:compress] = File.extname(filename) == '.gz' && File.extname(logical_path) != '.gz'
end

FileUtils.mkdir_p File.dirname(filename)

Expand Down
4 changes: 3 additions & 1 deletion lib/sprockets/static_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def to_path
# Save asset to disk.
def write_to(filename, options = {})
# Gzip contents if filename has '.gz'
options[:compress] ||= File.extname(filename) == '.gz'
unless options.key?(:compress)
options[:compress] = File.extname(filename) == '.gz' && File.extname(logical_path) != '.gz'
end

FileUtils.mkdir_p File.dirname(filename)

Expand Down
Binary file added test/fixtures/asset/archive.tar.gz
Binary file not shown.
13 changes: 13 additions & 0 deletions test/test_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ def self.test(name, &block)
assert !File.exist?(target)
end
end

test "do not gzip assets that are already gzipped" do
@asset = @env.find_asset('archive.tar.gz')
target = fixture_path('asset/tmp.tar.gz')
begin
@asset.write_to(target)
assert File.exist?(target)
assert_equal Digest::MD5.hexdigest(@asset.to_s), Digest::MD5.hexdigest(File.read(target))
ensure
FileUtils.rm(target) if File.exist?(target)
assert !File.exist?(target)
end
end
end

module FreshnessTests
Expand Down

0 comments on commit 4cc0d15

Please sign in to comment.