1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

Make the list of file extensions that get gzipped configurable as an option to the gzip_assets extension

This commit is contained in:
Ben Hollis 2012-02-11 00:22:10 -08:00
parent c9b7bf4b59
commit e4aa3da80a

View file

@ -7,22 +7,31 @@ module Middleman::Extensions
# Gzipped assets can be served directly by Apache or # Gzipped assets can be served directly by Apache or
# Nginx with the proper configuration, and pre-zipping means that we # Nginx with the proper configuration, and pre-zipping means that we
# can use a more agressive compression level at no CPU cost per request. # can use a more agressive compression level at no CPU cost per request.
#
# Use Nginx's gzip_static directive, or AddEncoding and mod_rewrite in Apache
# to serve your Gzipped files whenever the normal (non-.gz) filename is requested.
#
# Pass the :exts options to customize which file extensions get zipped (defaults
# to .js and .css.
#
module GzipAssets module GzipAssets
class << self class << self
def registered(app) def registered(app, options={})
exts = options[:exts] || %w(.js .css)
return unless app.inst.build? return unless app.inst.build?
app.after_configuration do app.after_configuration do
# Register a reroute transform that adds .gz to asset paths # Register a reroute transform that adds .gz to asset paths
sitemap.reroute do |destination, page| sitemap.reroute do |destination, page|
if %w(.js .css).include? page.ext if exts.include? page.ext
destination + '.gz' destination + '.gz'
else else
destination destination
end end
end end
use GzipRack use GzipRack, :exts => exts
end end
end end
alias :included :registered alias :included :registered
@ -36,6 +45,8 @@ module Middleman::Extensions
# @param [Hash] options # @param [Hash] options
def initialize(app, options={}) def initialize(app, options={})
@app = app @app = app
@exts = options[:exts]
@exts_regex = @exts.map {|e| Regexp.escape(e) }.join('|')
end end
# Rack interface # Rack interface
@ -44,7 +55,7 @@ module Middleman::Extensions
def call(env) def call(env)
status, headers, response = @app.call(env) status, headers, response = @app.call(env)
if env["PATH_INFO"].match(/\.(js|css).gz$/) if env["PATH_INFO"].match(/(#{@exts_regex}).gz$/)
contents = case(response) contents = case(response)
when String when String
response response