Merge pull request #558 from bhollis/asset-hash-rack

Asset_hash doesn't play nice with Sprockets
This commit is contained in:
Thomas Reynolds 2012-09-23 18:41:11 -07:00
commit 2aec04db69
5 changed files with 48 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -85,3 +85,22 @@ Feature: Assets get a file hash appended to their and references to them are upd
"""
When I go to "/partials/"
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'
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

@ -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

View file

@ -37,7 +37,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
@ -61,6 +66,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))