diff --git a/README.md b/README.md index 96d3ef8d..1023b5c9 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,24 @@ end ``` The blocks are run when local files are being scanned and uploaded +##### Config Method `file_ext_to_mime_type_overrides` +It's reported that `mime-types` 3.x returns `application/ecmascript` instead of `application/javascript` +Such change of mime type might cause some CDN to disable asset compression +So this gem has defined a default override for file ext `js` to be mapped to `application/javascript` by default + +To customize the overrides: +```ruby +AssetSync.configure do |config| + # Clear the default overrides + config.file_ext_to_mime_type_overrides.clear + + # Add/Edit overrides + # Will call `#to_s` for inputs + config.file_ext_to_mime_type_overrides.add(:js, :"application/x-javascript") +end +``` +The blocks are run when local files are being scanned and uploaded + #### Fog (Required) * **fog\_provider**: your storage provider *AWS* (S3) or *Rackspace* (Cloud Files) or *Google* (Google Storage) or *AzureRM* (Azure Blob) * **fog\_directory**: your bucket name diff --git a/asset_sync.gemspec b/asset_sync.gemspec index 56c15bb6..f34a46c1 100644 --- a/asset_sync.gemspec +++ b/asset_sync.gemspec @@ -24,7 +24,8 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec" s.add_development_dependency "bundler" - s.add_development_dependency "jeweler" + + s.add_development_dependency('mime-types', ">= 3.0") s.add_development_dependency "fog-aws" s.add_development_dependency "fog-azure-rm" diff --git a/lib/asset_sync/config.rb b/lib/asset_sync/config.rb index 125e8e3d..719d151f 100644 --- a/lib/asset_sync/config.rb +++ b/lib/asset_sync/config.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "active_model" require "erb" require "yaml" @@ -268,6 +270,11 @@ def additional_local_file_paths end end + #@api + def file_ext_to_mime_type_overrides + @file_ext_to_mime_type_overrides ||= FileExtToMimeTypeOverrides.new + end + private # This is a proc to get additional local files paths @@ -277,5 +284,43 @@ def additional_local_file_paths def default_manifest_directory File.join(::Rails.public_path, assets_prefix) end + + + # @api private + class FileExtToMimeTypeOverrides + def initialize + # The default is to prevent new mime type `application/ecmascript` to be returned + # which disables compression on some CDNs + @overrides = { + "js" => "application/javascript", + } + end + + # @api + def add(ext, mime_type) + # Symbol / Mime type object might be passed in + # But we want strings only + @overrides.store( + ext.to_s, mime_type.to_s, + ) + end + + # @api + def clear + @overrides = {} + end + + + # @api private + def key?(key) + @overrides.key?(key) + end + + # @api private + def fetch(key) + @overrides.fetch(key) + end + + end end end diff --git a/lib/asset_sync/multi_mime.rb b/lib/asset_sync/multi_mime.rb index eb73d392..6a01ee4e 100644 --- a/lib/asset_sync/multi_mime.rb +++ b/lib/asset_sync/multi_mime.rb @@ -4,6 +4,11 @@ module AssetSync class MultiMime def self.lookup(ext) + overrides = + ::AssetSync.config.file_ext_to_mime_type_overrides + if overrides.key?(ext) + return overrides.fetch(ext) + end if defined?(::MIME::Types) ::MIME::Types.type_for(ext).first diff --git a/spec/unit/multi_mime_spec.rb b/spec/unit/multi_mime_spec.rb index ccf16d76..3b7d2397 100644 --- a/spec/unit/multi_mime_spec.rb +++ b/spec/unit/multi_mime_spec.rb @@ -36,6 +36,8 @@ $".grep(/mime\//).each do |file_path| $".delete(file_path) end + + AssetSync.config = AssetSync::Config.new end after(:all) do @@ -69,4 +71,49 @@ end + describe "use of option file_ext_to_mime_type_overrides" do + before(:each) do + require 'mime/types' + end + + context "with default value" do + it "should return default value set by gem" do + expect( + AssetSync::MultiMime.lookup("js").to_s, + ).to eq("application/javascript") + end + end + context "with empty value" do + before(:each) do + AssetSync.config = AssetSync::Config.new + AssetSync.configure do |config| + config.file_ext_to_mime_type_overrides.clear + end + end + + it "should return value from mime-types gem" do + expect( + AssetSync::MultiMime.lookup("js").to_s, + ).to eq("application/ecmascript") + end + end + context "with custom value" do + before(:each) do + AssetSync.config = AssetSync::Config.new + AssetSync.configure do |config| + config.file_ext_to_mime_type_overrides.add( + :js, + :"application/x-javascript", + ) + end + end + + it "should return value from mime-types gem" do + expect( + AssetSync::MultiMime.lookup("js").to_s, + ).to eq("application/x-javascript") + end + end + end + end