From 4076666c19b431d619a5fe11c3a892426b05a3ff Mon Sep 17 00:00:00 2001 From: Adam Luikart Date: Thu, 6 Sep 2012 14:30:47 -0500 Subject: [PATCH 1/2] Add test for asset_hashing rack-filtered items. --- middleman-more/features/asset_hash.feature | 16 +++++++++++++++- .../fixtures/asset-hash-app/lib/middleware.rb | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 middleman-more/fixtures/asset-hash-app/lib/middleware.rb diff --git a/middleman-more/features/asset_hash.feature b/middleman-more/features/asset_hash.feature index 34d9355f..b5945ba5 100644 --- a/middleman-more/features/asset_hash.feature +++ b/middleman-more/features/asset_hash.feature @@ -84,4 +84,18 @@ Feature: Assets get a file hash appended to their and references to them are upd font-size: 18px !important """ When I go to "/partials/" - Then I should see 'href="../stylesheets/uses_partials-e8c3d4eb.css' \ No newline at end of file + Then I should see 'href="../stylesheets/uses_partials-e8c3d4eb.css' + + Scenario: The asset hash should change when a Rack-based filter changes + Given a fixture app "asset-hash-app" + And a file named "config.rb" with: + """ + activate :asset_hash + activate :relative_assets + activate :directory_indexes + require 'lib/middleware.rb' + use Middleware + """ + Given the Server is running at "asset-hash-app" + When I go to "/" + Then I should see 'href="stylesheets/site-5770af52.css' diff --git a/middleman-more/fixtures/asset-hash-app/lib/middleware.rb b/middleman-more/fixtures/asset-hash-app/lib/middleware.rb new file mode 100644 index 00000000..8ee825fd --- /dev/null +++ b/middleman-more/fixtures/asset-hash-app/lib/middleware.rb @@ -0,0 +1,16 @@ +class Middleware + def initialize(app) + @app = app + end + + def call(env) + status, headers, response = @app.call(env) + body = '' + response.each {|part| body += part } + if (env["PATH_INFO"] =~ /css$/) + body += "\n/* Added by Rack filter */" + status, headers, response = Rack::Response.new(body, status, headers).finish + end + [status, headers, response] + end +end From 09ba0049d4e44af10604be9d0a77d54e01e6f363 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Mon, 10 Sep 2012 21:56:12 -0700 Subject: [PATCH 2/2] Fix asset_hashing assets affected by Rack middleware or mounted apps (like sprockets). Fixes #558 --- middleman-core/lib/middleman-core/cli/build.rb | 5 +---- .../lib/middleman-core/core_extensions/request.rb | 4 ++-- middleman-more/features/asset_hash.feature | 5 +++++ .../lib/middleman-more/extensions/asset_hash.rb | 10 +++++++++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 2d9e0ded..9f7f6024 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -99,10 +99,7 @@ module Middleman::Cli # # @return [Rack::Test::Session] def shared_rack - @_shared_rack ||= begin - mock = ::Rack::MockSession.new(shared_server.to_rack_app) - ::Rack::Test::Session.new(mock) - end + @_shared_rack ||= ::Rack::Test::Session.new(shared_server.to_rack_app) end # Set the root path to the Middleman::Application's root diff --git a/middleman-core/lib/middleman-core/core_extensions/request.rb b/middleman-core/lib/middleman-core/core_extensions/request.rb index 9f750e16..237eeb2e 100644 --- a/middleman-core/lib/middleman-core/core_extensions/request.rb +++ b/middleman-core/lib/middleman-core/core_extensions/request.rb @@ -37,7 +37,6 @@ module Middleman # @private def reset! @app = nil - @prototype = nil end # The shared Rack instance being build @@ -94,7 +93,8 @@ module Middleman # @private # @return [Rack::Builder] def prototype - @prototype ||= to_rack_app + reset! + to_rack_app end # Call prototype, use in config.ru diff --git a/middleman-more/features/asset_hash.feature b/middleman-more/features/asset_hash.feature index b5945ba5..622e09ba 100644 --- a/middleman-more/features/asset_hash.feature +++ b/middleman-more/features/asset_hash.feature @@ -99,3 +99,8 @@ Feature: Assets get a file hash appended to their and references to them are upd Given the Server is running at "asset-hash-app" When I go to "/" Then I should see 'href="stylesheets/site-5770af52.css' + When I go to "stylesheets/site-5770af52.css" + Then I should see 'background-image' + Then I should see 'Added by Rack filter' + When I go to "stylesheets/site-50eaa978.css" + Then I should see 'Not Found' diff --git a/middleman-more/lib/middleman-more/extensions/asset_hash.rb b/middleman-more/lib/middleman-more/extensions/asset_hash.rb index 7d03535c..6605c754 100644 --- a/middleman-more/lib/middleman-more/extensions/asset_hash.rb +++ b/middleman-more/lib/middleman-more/extensions/asset_hash.rb @@ -36,7 +36,12 @@ module Middleman next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) } if resource.template? # if it's a template, render it out - digest = Digest::SHA1.hexdigest(resource.render)[0..7] + # Render through the Rack interface so middleware and mounted apps get a shot + rack_client = ::Rack::Test::Session.new(@app.class) + response = rack_client.get(URI.escape(resource.destination_path), {}, { "bypass_asset_hash" => true }) + raise "#{resource.path} should be in the sitemap!" unless response.status == 200 + + digest = Digest::SHA1.hexdigest(response.body)[0..7] else # if it's a static file, just hash it digest = Digest::SHA1.file(resource.source_file).hexdigest[0..7] end @@ -60,6 +65,9 @@ module Middleman def call(env) status, headers, response = @rack_app.call(env) + # We don't want to use this middleware when rendering files to figure out their hash! + return [status, headers, response] if env["bypass_asset_hash"] + path = @middleman_app.full_path(env["PATH_INFO"]) dirpath = Pathname.new(File.dirname(path))