Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ Add config option file_ext_to_mime_type_overrides (ruby only) #374

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion asset_sync.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 45 additions & 0 deletions lib/asset_sync/config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "active_model"
require "erb"
require "yaml"
Expand Down Expand Up @@ -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
Expand All @@ -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
5 changes: 5 additions & 0 deletions lib/asset_sync/multi_mime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions spec/unit/multi_mime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
$".grep(/mime\//).each do |file_path|
$".delete(file_path)
end

AssetSync.config = AssetSync::Config.new
end

after(:all) do
Expand Down Expand Up @@ -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