class Thoth::Minify

Rack middleware that intercepts and minifies CSS and JavaScript responses, caching the minified content to speed up future requests.

Constants

EXCLUDE
MINIFIERS

Public Class Methods

new(app) click to toggle source
# 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

Public Instance Methods

call(env) click to toggle source
# 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| ... } click to toggle source
# 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