From 0563523a81aa1ca9aa80d0ce9614029dbb1ba55a Mon Sep 17 00:00:00 2001 From: Nick Muerdter Date: Fri, 8 Jan 2016 14:56:42 -0700 Subject: [PATCH 1/2] Add rewrite_ignore option to asset_hash extension. This allows for configuring the files that will be skipped when performing URL rewriting on the file content. This differs from the existing `ignore` option, which configures the files that won't have hashes added to the filename. --- middleman-core/features/asset_hash.feature | 39 +++++++++++++++++++ .../middleman-core/extensions/asset_hash.rb | 2 + .../middleware/inline_url_rewriter.rb | 12 +++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/middleman-core/features/asset_hash.feature b/middleman-core/features/asset_hash.feature index 78755818..0a05c1a6 100644 --- a/middleman-core/features/asset_hash.feature +++ b/middleman-core/features/asset_hash.feature @@ -203,6 +203,45 @@ Feature: Assets get file hashes appended to them and references to them are upda | javascripts/application-1d8d5276.js | | stylesheets/site-7474cadd.css | + Scenario: Hashed-asset files are not replaced for rewrite ignored paths + Given a fixture app "asset-hash-app" + And a file named "config.rb" with: + """ + is_stylesheet = proc { |path| path.start_with? '/stylesheets' } + activate :asset_hash, rewrite_ignore: [ + %r(javascripts/*), + '/subdir/*', + is_stylesheet + ] + activate :relative_assets + activate :directory_indexes + """ + And a successfully built app at "asset-hash-app" + When I cd to "build" + Then the following files should exist: + | index.html | + | subdir/index.html | + | images/100px-5fd6fb90.jpg | + | javascripts/application-1d8d5276.js | + | stylesheets/site-8bc55985.css | + And the following files should not exist: + | images/100px.jpg | + | javascripts/application.js | + | stylesheets/site.css | + And the file "javascripts/application-1d8d5276.js" should contain "img.src = '/images/100px.jpg'" + And the file "stylesheets/site-8bc55985.css" should contain: + """ + background-image: url("../images/100px.jpg") + """ + And the file "index.html" should contain 'href="stylesheets/site-8bc55985.css"' + And the file "index.html" should contain 'src="javascripts/application-1d8d5276.js"' + And the file "index.html" should contain 'src="images/100px-5fd6fb90.jpg"' + And the file "subdir/index.html" should contain: + """ +

Image url3:

+

+ """ + Scenario: Already minified files should still be hashed Given a successfully built app at "asset-hash-minified-app" When I cd to "build" diff --git a/middleman-core/lib/middleman-core/extensions/asset_hash.rb b/middleman-core/lib/middleman-core/extensions/asset_hash.rb index 8b9b606b..da84a23d 100644 --- a/middleman-core/lib/middleman-core/extensions/asset_hash.rb +++ b/middleman-core/lib/middleman-core/extensions/asset_hash.rb @@ -5,6 +5,7 @@ require 'middleman-core/rack' class Middleman::Extensions::AssetHash < ::Middleman::Extension option :exts, %w(.jpg .jpeg .png .gif .webp .js .css .otf .woff .woff2 .eot .ttf .svg .svgz), 'List of extensions that get asset hashes appended to them.' option :ignore, [], 'Regexes of filenames to skip adding asset hashes to' + option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites' def initialize(app, options_hash={}, &block) super @@ -23,6 +24,7 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension url_extensions: options.exts.sort.reverse, source_extensions: %w(.htm .html .php .css .js), ignore: @ignore, + rewrite_ignore: options.rewrite_ignore, middleman_app: app, proc: method(:rewrite_url) end diff --git a/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb b/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb index 34312060..b76c3285 100644 --- a/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb +++ b/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb @@ -34,13 +34,16 @@ module Middleman @source_exts_regex_text = Regexp.union(@source_exts).to_s @ignore = options.fetch(:ignore) + @rewrite_ignore = Array(options.fetch(:rewrite_ignore, [])) end def call(env) status, headers, response = @rack_app.call(env) - # Allow upstream request to skip all rewriting - return [status, headers, response] if env['bypass_inline_url_rewriter'] == 'true' + # Allow configuration or upstream request to skip all rewriting + if rewrite_ignore?(env['PATH_INFO']) || env['bypass_inline_url_rewriter'] == 'true' + return [status, headers, response] + end # Allow upstream request to skip this specific rewriting if @uid @@ -96,6 +99,11 @@ module Middleman false end end + + Contract String => Bool + def rewrite_ignore?(path) + @rewrite_ignore.any? { |ignore| Middleman::Util.path_match(ignore, path) } + end end end end From 37be89ed68321554f4aeeaf1f17250e15e8cceb7 Mon Sep 17 00:00:00 2001 From: Nick Muerdter Date: Sat, 9 Jan 2016 08:06:47 -0700 Subject: [PATCH 2/2] Add rewrite_ignore option to asset_host, cache_buster, relative_assets. --- middleman-core/features/asset_host.feature | 14 ++++++++++++++ middleman-core/features/cache_buster.feature | 16 +++++++++++++++- middleman-core/features/relative_assets.feature | 14 ++++++++++++++ .../lib/middleman-core/extensions/asset_host.rb | 2 ++ .../middleman-core/extensions/cache_buster.rb | 2 ++ .../middleman-core/extensions/relative_assets.rb | 2 ++ 6 files changed, 49 insertions(+), 1 deletion(-) diff --git a/middleman-core/features/asset_host.feature b/middleman-core/features/asset_host.feature index a8778d29..d223b997 100644 --- a/middleman-core/features/asset_host.feature +++ b/middleman-core/features/asset_host.feature @@ -35,3 +35,17 @@ Feature: Alternate between multiple asset hosts When I go to "/stylesheets/asset_host.css" Then I should see content matching %r{http://assets1.example.com/} Then I should not see content matching %r{http://assets1.example.com//} + + Scenario: Hosts are not rewritten for rewrite ignored paths + Given a fixture app "asset-host-app" + And a file named "config.rb" with: + """ + activate :asset_host, host: "http://assets1.example.com", rewrite_ignore: [ + '/stylesheets/asset_host.css', + ] + """ + And the Server is running + When I go to "/asset_host.html" + Then I should see content matching %r{http://assets1.example.com/} + When I go to "/stylesheets/asset_host.css" + Then I should not see content matching %r{http://assets1.example.com/} diff --git a/middleman-core/features/cache_buster.feature b/middleman-core/features/cache_buster.feature index d452234e..bea898e3 100644 --- a/middleman-core/features/cache_buster.feature +++ b/middleman-core/features/cache_buster.feature @@ -39,4 +39,18 @@ Feature: Generate mtime-based query string for busting browser caches And the Server is running at "cache-buster-app" When I go to "/cache-buster.html" Then I should see "site.css?" - Then I should see "blank.gif?" \ No newline at end of file + Then I should see "blank.gif?" + + Scenario: URLs are not rewritten for rewrite ignored paths + Given a fixture app "cache-buster-app" + And a file named "config.rb" with: + """ + activate :cache_buster, rewrite_ignore: [ + '/cache-buster.html', + ] + """ + And the Server is running at "cache-buster-app" + When I go to "/cache-buster.html" + Then I should see 'site.css"' + Then I should see 'empty-with-include.js"' + Then I should see 'blank.gif"' diff --git a/middleman-core/features/relative_assets.feature b/middleman-core/features/relative_assets.feature index a4b6fc68..ea6cbff0 100644 --- a/middleman-core/features/relative_assets.feature +++ b/middleman-core/features/relative_assets.feature @@ -132,3 +132,17 @@ Feature: Relative Assets And the Server is running at "relative-assets-app" When I go to "/sub/image_tag.html" Then I should see '' + + Scenario: URLs are not rewritten for rewrite ignored paths + Given a fixture app "relative-assets-app" + And a file named "config.rb" with: + """ + activate :relative_assets, rewrite_ignore: [ + '/stylesheets/fonts.css', + ] + """ + And the Server is running at "relative-assets-app" + When I go to "/stylesheets/relative_assets.css" + Then I should see 'url("../images/blank.gif' + When I go to "/stylesheets/fonts.css" + Then I should see 'url(/fonts/roboto/roboto-regular-webfont.eot' diff --git a/middleman-core/lib/middleman-core/extensions/asset_host.rb b/middleman-core/lib/middleman-core/extensions/asset_host.rb index d0487591..05b4509f 100644 --- a/middleman-core/lib/middleman-core/extensions/asset_host.rb +++ b/middleman-core/lib/middleman-core/extensions/asset_host.rb @@ -6,6 +6,7 @@ class Middleman::Extensions::AssetHost < ::Middleman::Extension option :exts, %w(.css .png .jpg .jpeg .webp .svg .svgz .js .gif), 'List of extensions that get cache busters strings appended to them.' option :sources, %w(.htm .html .php .css .js), 'List of extensions that are searched for bustable assets.' option :ignore, [], 'Regexes of filenames to skip adding query strings to' + option :rewrite_ignore, [], 'Regexes of filenames to skip processing for host rewrites' def ready app.use ::Middleman::Middleware::InlineURLRewriter, @@ -13,6 +14,7 @@ class Middleman::Extensions::AssetHost < ::Middleman::Extension url_extensions: options.exts, source_extensions: options.sources, ignore: options.ignore, + rewrite_ignore: options.rewrite_ignore, middleman_app: app, proc: method(:rewrite_url) end diff --git a/middleman-core/lib/middleman-core/extensions/cache_buster.rb b/middleman-core/lib/middleman-core/extensions/cache_buster.rb index 7cc41a36..8f1ceb64 100644 --- a/middleman-core/lib/middleman-core/extensions/cache_buster.rb +++ b/middleman-core/lib/middleman-core/extensions/cache_buster.rb @@ -3,6 +3,7 @@ class Middleman::Extensions::CacheBuster < ::Middleman::Extension option :exts, %w(.css .png .jpg .jpeg .webp .svg .svgz .js .gif), 'List of extensions that get cache busters strings appended to them.' option :sources, %w(.htm .html .php .css .js), 'List of extensions that are searched for bustable assets.' option :ignore, [], 'Regexes of filenames to skip adding query strings to' + option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites' def initialize(app, options_hash={}, &block) super @@ -16,6 +17,7 @@ class Middleman::Extensions::CacheBuster < ::Middleman::Extension url_extensions: options.exts, source_extensions: options.sources, ignore: options.ignore, + rewrite_ignore: options.rewrite_ignore, middleman_app: app, proc: method(:rewrite_url) end diff --git a/middleman-core/lib/middleman-core/extensions/relative_assets.rb b/middleman-core/lib/middleman-core/extensions/relative_assets.rb index 4de2f2d5..f232f6a4 100644 --- a/middleman-core/lib/middleman-core/extensions/relative_assets.rb +++ b/middleman-core/lib/middleman-core/extensions/relative_assets.rb @@ -5,6 +5,7 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension option :exts, %w(.css .png .jpg .jpeg .webp .svg .svgz .js .gif .ttf .otf .woff .woff2 .eot), 'List of extensions that get cache busters strings appended to them.' option :sources, %w(.htm .html .css), 'List of extensions that are searched for relative assets.' option :ignore, [], 'Regexes of filenames to skip adding query strings to' + option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites' def initialize(app, options_hash={}, &block) super @@ -18,6 +19,7 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension url_extensions: options.exts, source_extensions: options.sources, ignore: options.ignore, + rewrite_ignore: options.rewrite_ignore, middleman_app: app, proc: method(:rewrite_url) end