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

Allow Custom assets to be cached (from webpack like tools) #342

Merged
merged 2 commits into from
May 11, 2017
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Nothing
- Allow customization of regexp of files on target bucket to be marked as 'Cacheable' so that browsers when serving the content would cache them.The value can be set by `cache_asset_regexps`

### Changed

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ AssetSync.configure do |config|
# Log silently. Default is `true`. But you can set it to false if more logging message are preferred.
# Logging messages are sent to `STDOUT` when `log_silently` is falsy
# config.log_silently = true
#
# Allow custom assets to be cacheable. Note: The base filename will be matched
# If you have an asset with name `app.0b1a4cd3.js`, only `app.0b1a4cd3` will need to be matched
# only one of `cache_asset_regexp` or `cache_asset_regexps` is allowed.
# config.cache_asset_regexp = /\.[a-f0-9]{8}$/i
# config.cache_asset_regexps = [ /\.[a-f0-9]{8}$/i, /\.[a-f0-9]{20}$/i ]
end
```

Expand Down Expand Up @@ -231,6 +237,9 @@ defaults: &defaults
# always_upload: ['application.js', 'application.css', !ruby/regexp '/application-/\d{32}\.css/']
# Ignored files. Useful if there are some files that are created dynamically on the server and you don't want to upload on deploy.
# ignored_files: ['ignore_me.js', !ruby/regexp '/ignore_some/\d{32}\.css/']
# Allow custom assets to be cacheable. Note: The base filename will be matched
# If you have an asset with name "app.0b1a4cd3.js", only "app.0b1a4cd3" will need to be matched
# cache_asset_regexps: ['cache_me.js', !ruby/regexp '/cache_some\.\d{8}\.css/']

development:
<<: *defaults
Expand Down Expand Up @@ -263,6 +272,7 @@ AssetSync.config.gzip_compression == ENV['ASSET_SYNC_GZIP_COMPRESSION']
* **manifest**: (`true, false`) when enabled, will use the `manifest.yml` generated by Rails to get the list of local files to upload. **experimental**. **default:** `'false'`
* **enabled**: (`true, false`) when false, will disable asset sync. **default:** `'true'` (enabled)
* **ignored\_files**: an array of files to ignore e.g. `['ignore_me.js', %r(ignore_some/\d{32}\.css)]` Useful if there are some files that are created dynamically on the server and you don't want to upload on deploy **default**: `[]`
* **cache\_asset\_regexps**: an array of files to add cache headers e.g. `['cache_me.js', %r(cache_some\.\d{8}\.css)]` Useful if there are some files that are added to sprockets assets list and need to be set as 'Cacheable' on uploaded server. Only rails compiled regexp is matched internally **default**: `[]`


#### Fog (Required)
Expand Down
7 changes: 7 additions & 0 deletions lib/asset_sync/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Invalid < StandardError; end
attr_accessor :run_on_precompile
attr_accessor :invalidate
attr_accessor :cdn_distribution_id
attr_accessor :cache_asset_regexps

# FOG configuration
attr_accessor :fog_provider # Currently Supported ['AWS', 'Rackspace']
Expand Down Expand Up @@ -65,6 +66,7 @@ def initialize
self.run_on_precompile = true
self.cdn_distribution_id = nil
self.invalidate = []
self.cache_asset_regexps = []
load_yml! if defined?(::Rails) && yml_exists?
end

Expand Down Expand Up @@ -114,6 +116,10 @@ def google?
fog_provider =~ /google/i
end

def cache_asset_regexp=(cache_asset_regexp)
self.cache_asset_regexps = [cache_asset_regexp]
end

def yml_exists?
defined?(::Rails.root) ? File.exist?(self.yml_path) : false
end
Expand Down Expand Up @@ -160,6 +166,7 @@ def load_yml!
self.run_on_precompile = yml["run_on_precompile"] if yml.has_key?("run_on_precompile")
self.invalidate = yml["invalidate"] if yml.has_key?("invalidate")
self.cdn_distribution_id = yml['cdn_distribution_id'] if yml.has_key?("cdn_distribution_id")
self.cache_asset_regexps = yml['cache_asset_regexps'] if yml.has_key?("cache_asset_regexps")

# TODO deprecate the other old style config settings. FML.
self.aws_access_key_id = yml["aws_access_key"] if yml.has_key?("aws_access_key")
Expand Down
5 changes: 4 additions & 1 deletion lib/asset_sync/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module AssetSync
class Storage
REGEXP_FINGERPRINTED_FILES = /^(.*)\/([^-]+)-[^\.]+\.([^\.]+)$/
REGEXP_ASSETS_TO_CACHE_CONTROL = /-[0-9a-fA-F]{32,}$/

class BucketNotFound < StandardError;
end
Expand Down Expand Up @@ -126,7 +127,9 @@ def upload_file(f)

uncompressed_filename = f.sub(/\.gz\z/, '')
basename = File.basename(uncompressed_filename, File.extname(uncompressed_filename))
if /-[0-9a-fA-F]{32,}$/.match(basename)

assets_to_cache_control = Regexp.union([REGEXP_ASSETS_TO_CACHE_CONTROL] | config.cache_asset_regexps).source
if basename.match(Regexp.new(assets_to_cache_control)).present?
file.merge!({
:cache_control => "public, max-age=#{one_year}",
:expires => CGI.rfc1123_date(Time.now + one_year)
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/asset_sync/templates/asset_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@
# Log silently. Default is `true`. But you can set it to false if more logging message are preferred.
# Logging messages are sent to `STDOUT` when `log_silently` is falsy
# config.log_silently = true
#
# Allow custom assets to be cacheable. Note: The base filename will be matched
# If you have an asset with name `app.0ba4d3.js`, only `app.0ba4d3` will need to be matched
# config.cache_asset_regexps = [ /\.[a-f0-9]{8}$/i, /\.[a-f0-9]{20}$/i ]
# config.cache_asset_regexp = /\.[a-f0-9]{8}$/i
end
2 changes: 2 additions & 0 deletions lib/generators/asset_sync/templates/asset_sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ defaults: &defaults
# gzip_compression: true
# Fail silently. Useful for environments such as Heroku
# fail_silently: true
# Allow custom assets to be cacheable. Note: The base filename will be matched
# cache_asset_regexps: ['cache_me.js', !ruby/regexp '/cache_some\.\d{8}\.css/']

development:
<<: *defaults
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/aws_with_yml/config/asset_sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defaults: &defaults
region: "eu-west-1"
run_on_precompile: false
fog_path_style: true
cache_asset_regexps: ['cache_me.js', !ruby/regexp '/cache_some\.\d{8}\.css/']

development:
<<: *defaults
Expand Down
26 changes: 26 additions & 0 deletions spec/unit/asset_sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
it "should default invalidate to empty array" do
expect(AssetSync.config.invalidate).to eq([])
end

it "should default asset_regexps to empty array" do
expect(AssetSync.config.cache_asset_regexps).to eq([])
end
end

describe 'from yml' do
Expand Down Expand Up @@ -138,6 +142,10 @@
it "should default manifest to false" do
expect(AssetSync.config.manifest).to be_falsey
end

it "should default asset_regexps to match regexps" do
expect(AssetSync.config.cache_asset_regexps).to eq(['cache_me.js', /cache_some\.\d{8}\.css/])
end
end

describe 'from yml, exporting to a mobile hybrid development directory' do
Expand Down Expand Up @@ -247,6 +255,24 @@
expect(AssetSync.config.manifest_path).to match(/public\/custom_assets\/manifest.yml/)
end
end

describe 'with cache_asset_regexps' do
before(:each) do
AssetSync.config = AssetSync::Config.new
end

it "config.cache_asset_regexp should set cache_asset_regexps" do
AssetSync.config.cache_asset_regexp = /\.[a-f0-9]{8}/i
expect(AssetSync.config.cache_asset_regexps.size).to eq(1)
expect(AssetSync.config.cache_asset_regexps[0]).to eq(/\.[a-f0-9]{8}/i)
end

it "set cache_asset_regexps" do
AssetSync.config.cache_asset_regexps = ["app.abc123.js", /\.[a-f0-9]{10}/i]
expect(AssetSync.config.cache_asset_regexps.size).to eq(2)
expect(AssetSync.config.cache_asset_regexps).to eq(["app.abc123.js", /\.[a-f0-9]{10}/i])
end
end

describe 'with invalid yml' do
before(:each) do
Expand Down
21 changes: 19 additions & 2 deletions spec/unit/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz'
]
local_files += [
'file3.png',
'file3.zabcde.png',
'file3.zab1cde2.png',
'file3.abcdef.jpg',
'file3.abc1def2.jpg',
'dir3/file3.abc123.jpg',
'dir3/file3.abcdf123.jpg'
]
remote_files = []
@config.cache_asset_regexps = [/\.[a-f0-9]{6}$/i, /\.[a-f0-9]{8}$/i]
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(local_files)
allow(storage).to receive(:get_remote_files).and_return(remote_files)
Expand All @@ -108,7 +118,10 @@
def check_file(file)
case file[:key]
when 'file1.jpg',
'dir1/dir2/file2.jpg'
'dir1/dir2/file2.jpg',
'file3.png',
'file3.zabcde.png',
'file3.zab1cde2.png'
!expect(file).not_to include(:cache_control, :expires)
when 'file1-1234567890abcdef1234567890abcdef.jpg',
'file1-1234567890abcdef1234567890abcdef.jpg.gz',
Expand All @@ -117,7 +130,11 @@ def check_file(file)
'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef.jpg.gz',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg',
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz'
'dir1/dir2/file2-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.jpg.gz',
'file3.abcdef.jpg',
'file3.abc1def2.jpg',
'dir3/file3.abc123.jpg',
'dir3/file3.abcdf123.jpg'
expect(file).to include(:cache_control, :expires)
else
fail
Expand Down