heroku_rails_deflate 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +20 -0
- data/README.md +18 -0
- data/Rakefile +47 -0
- data/heroku_rails_deflate.gemspec +58 -0
- data/lib/heroku_rails_deflate.rb +1 -0
- data/lib/heroku_rails_deflate/railtie.rb +16 -0
- data/lib/heroku_rails_deflate/serve_zipped_assets.rb +64 -0
- data/lib/heroku_rails_deflate/version.rb +10 -0
- data/test/helper.rb +18 -0
- data/test/test_heroku_rails_deflate.rb +7 -0
- metadata +113 -0
data/.document
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.8.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rdoc
|
10
|
+
json (1.7.7)
|
11
|
+
rack (1.5.2)
|
12
|
+
rake (10.0.4)
|
13
|
+
rdoc (4.0.1)
|
14
|
+
json (~> 1.4)
|
15
|
+
|
16
|
+
PLATFORMS
|
17
|
+
ruby
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
bundler
|
21
|
+
jeweler
|
22
|
+
rack (~> 1.5.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Matt Olson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# heroku\_rails\_deflate
|
2
|
+
|
3
|
+
Activate Rack::Deflate and serve up precompiled, gzipped assets on Heroku
|
4
|
+
|
5
|
+
## Contributing to heroku\_rails\_deflate
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
+
* Fork the project.
|
10
|
+
* Start a feature/bugfix branch.
|
11
|
+
* Commit and push until you are happy with your contribution.
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
## Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2013 Matt Olson. See LICENSE.txt for further details.
|
18
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
require './lib/heroku_rails_deflate/version'
|
16
|
+
Jeweler::Tasks.new do |gem|
|
17
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
18
|
+
gem.name = "heroku_rails_deflate"
|
19
|
+
gem.version = HerokuRailsDeflate::Version::STRING
|
20
|
+
gem.homepage = "http://github.com/mattolson/heroku_rails_deflate"
|
21
|
+
gem.license = "MIT"
|
22
|
+
gem.summary = %Q{Activate Rack::Deflate and serve up precompiled, gzipped assets on Heroku}
|
23
|
+
gem.description = %Q{Activate Rack::Deflate and serve up precompiled, gzipped assets on Heroku. This allows us to take advantage of higher compression ratios of prezipped files, and reduces CPU load at request time.}
|
24
|
+
gem.email = "matt@mattolson.com"
|
25
|
+
gem.authors = ["Matt Olson"]
|
26
|
+
# dependencies defined in Gemfile
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
require 'rake/testtask'
|
31
|
+
Rake::TestTask.new(:test) do |test|
|
32
|
+
test.libs << 'lib' << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
|
37
|
+
task :default => :test
|
38
|
+
|
39
|
+
require 'rdoc/task'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "heroku_rails_deflate #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "heroku_rails_deflate"
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Matt Olson"]
|
12
|
+
s.date = "2013-04-02"
|
13
|
+
s.description = "Activate Rack::Deflate and serve up precompiled, gzipped assets on Heroku. This allows us to take advantage of higher compression ratios of prezipped files, and reduces CPU load at request time."
|
14
|
+
s.email = "matt@mattolson.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"heroku_rails_deflate.gemspec",
|
27
|
+
"lib/heroku_rails_deflate.rb",
|
28
|
+
"lib/heroku_rails_deflate/railtie.rb",
|
29
|
+
"lib/heroku_rails_deflate/serve_zipped_assets.rb",
|
30
|
+
"lib/heroku_rails_deflate/version.rb",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/test_heroku_rails_deflate.rb"
|
33
|
+
]
|
34
|
+
s.homepage = "http://github.com/mattolson/heroku_rails_deflate"
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = "1.8.24"
|
38
|
+
s.summary = "Activate Rack::Deflate and serve up precompiled, gzipped assets on Heroku"
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_runtime_dependency(%q<rack>, ["~> 1.5.0"])
|
45
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
46
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<rack>, ["~> 1.5.0"])
|
49
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
50
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
51
|
+
end
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rack>, ["~> 1.5.0"])
|
54
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
55
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'heroku_rails_deflate/railtie'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rack/deflater'
|
2
|
+
require 'heroku_rails_deflate/serve_zipped_assets'
|
3
|
+
|
4
|
+
module HerokuRailsDeflate
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer "heroku_rails_deflate.middleware_initialization" do |app|
|
7
|
+
app.middleware.insert_before ActionDispatch::Static, Rack::Deflater
|
8
|
+
app.middleware.insert_before ActionDispatch::Static, HerokuRailsDeflate::ServeZippedAssets, app.paths["public"].first, app.config.assets.prefix, app.config.static_cache_control
|
9
|
+
end
|
10
|
+
|
11
|
+
# Set default Cache-Control headers to 365 days. Override in config/application.rb.
|
12
|
+
config.before_configuration do |app|
|
13
|
+
app.config.static_cache_control = 'public, max-age=31536000'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'action_dispatch/middleware/static'
|
2
|
+
|
3
|
+
# Adapted from https://gist.github.com/guyboltonking/2152663
|
4
|
+
module HerokuRailsDeflate
|
5
|
+
# Rails static middleware, but restricted to assets path
|
6
|
+
class FileHandler < ActionDispatch::FileHandler
|
7
|
+
def initialize(root, assets_path, cache_control)
|
8
|
+
@assets_path = assets_path.chomp('/') + '/'
|
9
|
+
super(root, cache_control)
|
10
|
+
end
|
11
|
+
|
12
|
+
def match?(path)
|
13
|
+
path.start_with?(@assets_path) && super(path)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class ServeZippedAssets
|
18
|
+
def initialize(app, path, assets_path, cache_control=nil)
|
19
|
+
@app = app
|
20
|
+
@file_handler = FileHandler.new(path, assets_path, cache_control)
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(env)
|
24
|
+
if env['REQUEST_METHOD'] == 'GET'
|
25
|
+
request = Rack::Request.new(env)
|
26
|
+
encoding = Rack::Utils.select_best_encoding(%w(gzip identity), request.accept_encoding)
|
27
|
+
|
28
|
+
if encoding == 'gzip'
|
29
|
+
# See if gzipped version exists in assets directory
|
30
|
+
pathgz = env['PATH_INFO'] + '.gz'
|
31
|
+
if match = @file_handler.match?(pathgz)
|
32
|
+
# Get the filehandler to serve up the gzipped file, then strip the .gz suffix
|
33
|
+
env["PATH_INFO"] = match
|
34
|
+
status, headers, body = @file_handler.call(env)
|
35
|
+
path = env["PATH_INFO"] = env["PATH_INFO"].chomp('.gz')
|
36
|
+
|
37
|
+
# Set the Vary HTTP header.
|
38
|
+
vary = headers["Vary"].to_s.split(",").map { |v| v.strip }
|
39
|
+
unless vary.include?("*") || vary.include?("Accept-Encoding")
|
40
|
+
headers["Vary"] = vary.push("Accept-Encoding").join(",")
|
41
|
+
end
|
42
|
+
|
43
|
+
# Add encoding and type
|
44
|
+
headers['Content-Encoding'] = 'gzip'
|
45
|
+
headers['Content-Type'] = Rack::Mime.mime_type(File.extname(path), 'text/plain')
|
46
|
+
headers.delete('Content-Length')
|
47
|
+
|
48
|
+
# Update cache-control to add directive telling Rack::Deflate to leave it alone.
|
49
|
+
cache_control = headers['Cache-Control'].try(:to_s).try(:downcase)
|
50
|
+
if cache_control.nil?
|
51
|
+
headers['Cache-Control'] = 'no-transform'
|
52
|
+
elsif !cache_control.include?('no-transform')
|
53
|
+
headers['Cache-Control'] += ', no-transform'
|
54
|
+
end
|
55
|
+
|
56
|
+
return [status, headers, body]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
@app.call(env)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'heroku_rails_deflate'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: heroku_rails_deflate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matt Olson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.5.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.5.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Activate Rack::Deflate and serve up precompiled, gzipped assets on Heroku.
|
63
|
+
This allows us to take advantage of higher compression ratios of prezipped files,
|
64
|
+
and reduces CPU load at request time.
|
65
|
+
email: matt@mattolson.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files:
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
files:
|
72
|
+
- .document
|
73
|
+
- Gemfile
|
74
|
+
- Gemfile.lock
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- heroku_rails_deflate.gemspec
|
79
|
+
- lib/heroku_rails_deflate.rb
|
80
|
+
- lib/heroku_rails_deflate/railtie.rb
|
81
|
+
- lib/heroku_rails_deflate/serve_zipped_assets.rb
|
82
|
+
- lib/heroku_rails_deflate/version.rb
|
83
|
+
- test/helper.rb
|
84
|
+
- test/test_heroku_rails_deflate.rb
|
85
|
+
homepage: http://github.com/mattolson/heroku_rails_deflate
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
hash: 2529553932566617148
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 1.8.24
|
110
|
+
signing_key:
|
111
|
+
specification_version: 3
|
112
|
+
summary: Activate Rack::Deflate and serve up precompiled, gzipped assets on Heroku
|
113
|
+
test_files: []
|