smart_assets 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/lib/smart_assets/rack.rb +54 -0
- data/lib/smart_assets/railtie.rb +23 -0
- data/lib/smart_assets/version.rb +3 -0
- data/lib/smart_assets.rb +3 -0
- data/smart_assets.gemspec +26 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8feafdbc7693ce1d631af2295f4b32fc24996fda
|
4
|
+
data.tar.gz: 3a6f4c22fba94c691916545b2db23a8df70ee672
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f086f505f8d919605399be2a504f9e644366048cd6af79c9da611df8874623c547da3c8fb00e8789f30647afdcaf6da65da1432bd6505c61513a88de6f90bc40
|
7
|
+
data.tar.gz: 77a34c803172f5ac8c7f7e18473488edf2103d6b6ec560ed6e2acc1a175e40578caa016a875df2e0d43fafc0f6784c5f4fcf2e02ae47e84f8e36bfc312963c50
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 thomas morgan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# SmartAssets
|
2
|
+
|
3
|
+
SmartAssets is a Rack Middleware for Rails that enables delivery of non-digest assets when using the default asset pipeline.
|
4
|
+
|
5
|
+
It solves the problem of production environment requests for assets (eg: application.css) returning a 404 because they do not contain a digest (eg: application-1c8db23293725a8857e5132d59211909.css).
|
6
|
+
|
7
|
+
In general, Rails' default behavior of requiring a digest is a good idea. Serving asset files without a digest can easily result in assets being cached for long periods of time, making them impossible to update. Consider yourself warned.
|
8
|
+
|
9
|
+
At the same time, there are legitimate reasons to serve assets without a digest in the name, among them using assets with 404 and other error pages and with 3rd-party applications and sites.
|
10
|
+
|
11
|
+
This middleware works by reading the sprockets manifest file and internally changing the HTTP request to reflect the digested name. If your manifest is out of date, you will still get 404s.
|
12
|
+
|
13
|
+
In order to minimize the risks of long caching while using this gem, requests where the requested filename is mutated will set Cache-Control: public, max-age=60.
|
14
|
+
|
15
|
+
This can configured in your environment files (or an initializer) using:
|
16
|
+
|
17
|
+
config.smart_assets.cache_control = 'public, max-age=60'
|
18
|
+
|
19
|
+
Max-age is in seconds. Caching by browsers and proxies can be disabled entirely with:
|
20
|
+
|
21
|
+
config.smart_assets.cache_control = 'max-age=0,no-cache,no-store'
|
22
|
+
|
23
|
+
This middleware may also be diabled on a per-environment basis with:
|
24
|
+
|
25
|
+
config.smart_assets.serve_non_digest_assets = false
|
26
|
+
|
27
|
+
Note that `serve_static_assets` or `assets.compile` must be true or the middleware will disable itself.
|
28
|
+
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
|
32
|
+
Add this line to your application's Gemfile:
|
33
|
+
|
34
|
+
gem 'smart_assets'
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
$ bundle
|
39
|
+
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it ( https://github.com/zarqman/smart_assets/fork )
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module SmartAssets
|
2
|
+
class Rack
|
3
|
+
|
4
|
+
def initialize(app, prefix, cache_control)
|
5
|
+
@app = app
|
6
|
+
@prefix = prefix
|
7
|
+
@cache_control = cache_control
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
unless is_asset?(env) && (digest_asset=digest_name(env))
|
12
|
+
return @app.call(env)
|
13
|
+
end
|
14
|
+
|
15
|
+
digest_path = File.join(manifest.dir, digest_asset)
|
16
|
+
if File.exists?(digest_path)
|
17
|
+
env['PATH_INFO'] = "#{@prefix}/#{digest_asset}"
|
18
|
+
end
|
19
|
+
status, headers, body = @app.call(env)
|
20
|
+
if [200, 206].include?(status)
|
21
|
+
headers['Cache-Control'] = @cache_control
|
22
|
+
headers['ETag'] ||= %("#{digest(digest_asset)}")
|
23
|
+
end
|
24
|
+
[status, headers, body]
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def is_asset?(env)
|
29
|
+
case env['REQUEST_METHOD']
|
30
|
+
when 'GET', 'HEAD'
|
31
|
+
env['PATH_INFO'].start_with?(@prefix)
|
32
|
+
else
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def base_path(env)
|
38
|
+
env['PATH_INFO'].sub(%r{^#{@prefix}/}, '')
|
39
|
+
end
|
40
|
+
|
41
|
+
def digest_name(env)
|
42
|
+
manifest.assets[base_path(env)]
|
43
|
+
end
|
44
|
+
|
45
|
+
def digest(name)
|
46
|
+
manifest.files[name]['digest']
|
47
|
+
end
|
48
|
+
|
49
|
+
def manifest
|
50
|
+
ActionView::Base.assets_manifest
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SmartAssets
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
|
4
|
+
config.smart_assets = ActiveSupport::OrderedOptions.new
|
5
|
+
config.smart_assets.cache_control = 'public, max-age=60'
|
6
|
+
config.smart_assets.prefix = nil
|
7
|
+
config.smart_assets.serve_non_digest_assets = true
|
8
|
+
|
9
|
+
initializer 'smart_assets.configure' do |app|
|
10
|
+
if app.config.smart_assets.serve_non_digest_assets
|
11
|
+
prefix = app.config.smart_assets.prefix || app.config.assets.prefix || '/assets'
|
12
|
+
cc = app.config.smart_assets.cache_control
|
13
|
+
|
14
|
+
if app.config.serve_static_assets || app.config.assets.compile
|
15
|
+
# app.middleware.insert_before(ActionDispatch::Static, SmartAssets::Rack, prefix, cc)
|
16
|
+
# elsif app.config.assets.compile
|
17
|
+
app.middleware.insert_after(::Rack::Sendfile, SmartAssets::Rack, prefix, cc)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/lib/smart_assets.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'smart_assets/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "smart_assets"
|
8
|
+
spec.version = SmartAssets::VERSION
|
9
|
+
spec.authors = ["thomas morgan"]
|
10
|
+
spec.email = ["tm@iprog.com"]
|
11
|
+
spec.summary = %q{Rails/Rack middleware to enable delivery of non-digest assets}
|
12
|
+
spec.description = %q{Rails/Rack middleware to enable delivery of non-digest assets.}
|
13
|
+
spec.homepage = "https://github.com/zarqman/smart_assets"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'rails', '~> 4.0'
|
22
|
+
spec.add_dependency 'sprockets-rails', '~> 2.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smart_assets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- thomas morgan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sprockets-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Rails/Rack middleware to enable delivery of non-digest assets.
|
70
|
+
email:
|
71
|
+
- tm@iprog.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/smart_assets.rb
|
82
|
+
- lib/smart_assets/rack.rb
|
83
|
+
- lib/smart_assets/railtie.rb
|
84
|
+
- lib/smart_assets/version.rb
|
85
|
+
- smart_assets.gemspec
|
86
|
+
homepage: https://github.com/zarqman/smart_assets
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.0.6
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Rails/Rack middleware to enable delivery of non-digest assets
|
110
|
+
test_files: []
|