From e4aa3da80a0ac0f2d5a6901c930512550bf89d02 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 11 Feb 2012 00:22:10 -0800 Subject: [PATCH] Make the list of file extensions that get gzipped configurable as an option to the gzip_assets extension --- .../middleman-more/extensions/gzip_assets.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/middleman-more/lib/middleman-more/extensions/gzip_assets.rb b/middleman-more/lib/middleman-more/extensions/gzip_assets.rb index 011b0986..cf1de12d 100644 --- a/middleman-more/lib/middleman-more/extensions/gzip_assets.rb +++ b/middleman-more/lib/middleman-more/extensions/gzip_assets.rb @@ -7,22 +7,31 @@ module Middleman::Extensions # Gzipped assets can be served directly by Apache or # Nginx with the proper configuration, and pre-zipping means that we # 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 class << self - def registered(app) + def registered(app, options={}) + exts = options[:exts] || %w(.js .css) + return unless app.inst.build? app.after_configuration do # Register a reroute transform that adds .gz to asset paths sitemap.reroute do |destination, page| - if %w(.js .css).include? page.ext + if exts.include? page.ext destination + '.gz' else destination end end - use GzipRack + use GzipRack, :exts => exts end end alias :included :registered @@ -36,6 +45,8 @@ module Middleman::Extensions # @param [Hash] options def initialize(app, options={}) @app = app + @exts = options[:exts] + @exts_regex = @exts.map {|e| Regexp.escape(e) }.join('|') end # Rack interface @@ -44,7 +55,7 @@ module Middleman::Extensions def 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) when String response