Fix asset_hashing assets affected by Rack middleware or mounted apps (like sprockets). Fixes #558

This commit is contained in:
Ben Hollis 2012-09-10 21:56:12 -07:00
parent 4076666c19
commit 09ba0049d4
4 changed files with 17 additions and 7 deletions

View file

@ -99,10 +99,7 @@ module Middleman::Cli
# #
# @return [Rack::Test::Session] # @return [Rack::Test::Session]
def shared_rack def shared_rack
@_shared_rack ||= begin @_shared_rack ||= ::Rack::Test::Session.new(shared_server.to_rack_app)
mock = ::Rack::MockSession.new(shared_server.to_rack_app)
::Rack::Test::Session.new(mock)
end
end end
# Set the root path to the Middleman::Application's root # Set the root path to the Middleman::Application's root

View file

@ -37,7 +37,6 @@ module Middleman
# @private # @private
def reset! def reset!
@app = nil @app = nil
@prototype = nil
end end
# The shared Rack instance being build # The shared Rack instance being build
@ -94,7 +93,8 @@ module Middleman
# @private # @private
# @return [Rack::Builder] # @return [Rack::Builder]
def prototype def prototype
@prototype ||= to_rack_app reset!
to_rack_app
end end
# Call prototype, use in config.ru # Call prototype, use in config.ru

View file

@ -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" Given the Server is running at "asset-hash-app"
When I go to "/" When I go to "/"
Then I should see 'href="stylesheets/site-5770af52.css' 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'

View file

@ -36,7 +36,12 @@ module Middleman
next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) } next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) }
if resource.template? # if it's a template, render it out 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 else # if it's a static file, just hash it
digest = Digest::SHA1.file(resource.source_file).hexdigest[0..7] digest = Digest::SHA1.file(resource.source_file).hexdigest[0..7]
end end
@ -60,6 +65,9 @@ module Middleman
def call(env) def call(env)
status, headers, response = @rack_app.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"]) path = @middleman_app.full_path(env["PATH_INFO"])
dirpath = Pathname.new(File.dirname(path)) dirpath = Pathname.new(File.dirname(path))