Merge pull request #558 from bhollis/asset-hash-rack
Asset_hash doesn't play nice with Sprockets
This commit is contained in:
commit
2aec04db69
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -85,3 +85,22 @@ Feature: Assets get a file hash appended to their and references to them are upd
|
||||||
"""
|
"""
|
||||||
When I go to "/partials/"
|
When I go to "/partials/"
|
||||||
Then I should see 'href="../stylesheets/uses_partials-e8c3d4eb.css'
|
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'
|
||||||
|
|
16
middleman-more/fixtures/asset-hash-app/lib/middleware.rb
Normal file
16
middleman-more/fixtures/asset-hash-app/lib/middleware.rb
Normal 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
|
|
@ -37,7 +37,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
|
||||||
|
@ -61,6 +66,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))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue