Class: Thoth::Minify
- Inherits:
-
Object
- Object
- Thoth::Minify
- Defined in:
- lib/thoth/middleware/minify.rb
Overview
Rack middleware that intercepts and minifies CSS and JavaScript responses, caching the minified content to speed up future requests.
Constant Summary collapse
- MINIFIERS =
{ 'application/javascript' => JSMin, 'text/css' => CSSMin, 'text/javascript' => JSMin }
- EXCLUDE =
[ /-min\.(?:css|js)$/i ]
Instance Method Summary collapse
- #call(env) ⇒ Object
- #each {|@body| ... } ⇒ Object
-
#initialize(app) ⇒ Minify
constructor
A new instance of Minify.
Constructor Details
#initialize(app) ⇒ Minify
Returns a new instance of Minify.
48 49 50 51 52 53 |
# File 'lib/thoth/middleware/minify.rb', line 48 def initialize(app) @app = app Ramaze::Cache.add(:minify) unless Ramaze::Cache.respond_to?(:minify) @cache = Ramaze::Cache.minify end |
Instance Method Details
#call(env) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/thoth/middleware/minify.rb', line 55 def call(env) @status, @headers, @body = @app.call(env) unless @status == 200 && @minifier = MINIFIERS[@headers['Content-Type']] return [@status, @headers, @body] end @path = Rack::Utils.unescape(env['PATH_INFO']) EXCLUDE.each {|ex| return [@status, @headers, @body] if @path =~ ex } @headers.delete('Content-Length') @headers['Cache-Control'] = 'max-age=3600,public' [@status, @headers, self] end |
#each {|@body| ... } ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/thoth/middleware/minify.rb', line 72 def each content = '' @body.each {|part| content << part.to_s } @body = @cache["minify_#{@path}"] ||= @minifier.minify(content) yield @body end |