Merge pull request #333 from bhollis/asset_hash

Make asset_hash work with asset_host
This commit is contained in:
Thomas Reynolds 2012-04-02 13:42:46 -07:00
commit 4fc9492aae
15 changed files with 84 additions and 24 deletions

View file

@ -24,7 +24,14 @@ module Middleman::CoreExtensions::Assets
# @return [String] The fully qualified asset url # @return [String] The fully qualified asset url
def asset_url(path, prefix="") def asset_url(path, prefix="")
# Don't touch assets which already have a full path # Don't touch assets which already have a full path
path.include?("://") ? path : File.join(http_prefix, prefix, path) if path.include?("//")
path
else # rewrite paths to use their destination path
path = File.join(prefix, path)
path = sitemap.page(path).destination_path if sitemap.exists?(path)
File.join(http_prefix, path)
end
end end
end end
end end

View file

@ -8,7 +8,7 @@ Feature: Assets get a file hash appended to their and references to them are upd
| images/100px-5fd6fb90.jpg | | images/100px-5fd6fb90.jpg |
| images/100px-5fd6fb90.gif | | images/100px-5fd6fb90.gif |
| javascripts/application-1d8d5276.js | | javascripts/application-1d8d5276.js |
| stylesheets/site-92072d15.css | | stylesheets/site-8c28fde3.css |
| index.html | | index.html |
| subdir/index.html | | subdir/index.html |
| other/index.html | | other/index.html |
@ -20,35 +20,47 @@ Feature: Assets get a file hash appended to their and references to them are upd
| stylesheets/site.css | | stylesheets/site.css |
And the file "javascripts/application-1d8d5276.js" should contain "img.src = '/images/100px-5fd6fb90.jpg'" And the file "javascripts/application-1d8d5276.js" should contain "img.src = '/images/100px-5fd6fb90.jpg'"
# TODO: This stylesheet should use the SASS "image-url" helper but can't because of https://github.com/middleman/middleman/issues/283 And the file "stylesheets/site-8c28fde3.css" should contain "background-image: url('../images/100px-5fd6fb90.jpg')"
And the file "stylesheets/site-92072d15.css" should contain 'background-image: url("/images/100px-5fd6fb90.jpg")' And the file "index.html" should contain 'href="stylesheets/site-8c28fde3.css"'
And the file "index.html" should contain 'href="stylesheets/site-92072d15.css"'
And the file "index.html" should contain 'src="javascripts/application-1d8d5276.js"' 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 "index.html" should contain 'src="images/100px-5fd6fb90.jpg"'
And the file "subdir/index.html" should contain 'href="../stylesheets/site-92072d15.css"' And the file "subdir/index.html" should contain 'href="../stylesheets/site-8c28fde3.css"'
And the file "subdir/index.html" should contain 'src="../javascripts/application-1d8d5276.js"' And the file "subdir/index.html" should contain 'src="../javascripts/application-1d8d5276.js"'
And the file "subdir/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"' And the file "subdir/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"'
And the file "other/index.html" should contain 'href="../stylesheets/site-92072d15.css"' And the file "other/index.html" should contain 'href="../stylesheets/site-8c28fde3.css"'
And the file "other/index.html" should contain 'src="../javascripts/application-1d8d5276.js"' And the file "other/index.html" should contain 'src="../javascripts/application-1d8d5276.js"'
And the file "other/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"' And the file "other/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"'
Scenario: Hashed assets work in preview server Scenario: Hashed assets work in preview server
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-92072d15.css"' Then I should see 'href="stylesheets/site-8c28fde3.css"'
And I should see 'src="javascripts/application-1d8d5276.js"' And I should see 'src="javascripts/application-1d8d5276.js"'
And I should see 'src="images/100px-5fd6fb90.jpg"' And I should see 'src="images/100px-5fd6fb90.jpg"'
When I go to "/subdir/" When I go to "/subdir/"
Then I should see 'href="../stylesheets/site-92072d15.css"' Then I should see 'href="../stylesheets/site-8c28fde3.css"'
And I should see 'src="../javascripts/application-1d8d5276.js"' And I should see 'src="../javascripts/application-1d8d5276.js"'
And I should see 'src="../images/100px-5fd6fb90.jpg"' And I should see 'src="../images/100px-5fd6fb90.jpg"'
When I go to "/other/" When I go to "/other/"
Then I should see 'href="../stylesheets/site-92072d15.css"' Then I should see 'href="../stylesheets/site-8c28fde3.css"'
And I should see 'src="../javascripts/application-1d8d5276.js"' And I should see 'src="../javascripts/application-1d8d5276.js"'
And I should see 'src="../images/100px-5fd6fb90.jpg"' And I should see 'src="../images/100px-5fd6fb90.jpg"'
When I go to "/javascripts/application-1d8d5276.js" When I go to "/javascripts/application-1d8d5276.js"
Then I should see "img.src = '/images/100px-5fd6fb90.jpg'" Then I should see "img.src = '/images/100px-5fd6fb90.jpg'"
When I go to "/stylesheets/site-92072d15.css" When I go to "/stylesheets/site-8c28fde3.css"
Then I should see 'background-image: url("/images/100px-5fd6fb90.jpg")' Then I should see "background-image: url('../images/100px-5fd6fb90.jpg')"
# Scenario: Enabling an asset host still produces hashed files and references Scenario: Enabling an asset host still produces hashed files and references
Given the Server is running at "asset-hash-host-app"
When I go to "/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-8c28fde3.css"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
When I go to "/subdir/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-8c28fde3.css"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
When I go to "/other/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-8c28fde3.css"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
# Asset helpers don't appear to work from Compass right now
# When I go to "/stylesheets/site-8c28fde3.css"
# Then I should see "background-image: url('http://middlemanapp.com/images/100px-5fd6fb90.jpg')"

View file

@ -5,22 +5,23 @@ Feature: Generate mtime-based query string for busting browser caches
Given "cache_buster" feature is "disabled" Given "cache_buster" feature is "disabled"
And the Server is running at "cache-buster-app" And the Server is running at "cache-buster-app"
When I go to "/stylesheets/relative_assets.css" When I go to "/stylesheets/relative_assets.css"
Then I should not see "?" Then I should see "blank.gif'"
Scenario: Rendering html with the feature disabled Scenario: Rendering html with the feature disabled
Given "cache_buster" feature is "disabled" Given "cache_buster" feature is "disabled"
And the Server is running at "cache-buster-app" And the Server is running at "cache-buster-app"
When I go to "/cache-buster.html" When I go to "/cache-buster.html"
Then I should not see "?" Then I should see 'site.css"'
Scenario: Rendering css with the feature enabled Scenario: Rendering css with the feature enabled
Given "cache_buster" feature is "enabled" Given "cache_buster" feature is "enabled"
And the Server is running at "cache-buster-app" And the Server is running at "cache-buster-app"
When I go to "/stylesheets/relative_assets.css" When I go to "/stylesheets/relative_assets.css"
Then I should see "?" Then I should see "blank.gif?"
Scenario: Rendering html with the feature enabled Scenario: Rendering html with the feature enabled
Given "cache_buster" feature is "enabled" Given "cache_buster" feature is "enabled"
And the Server is running at "cache-buster-app" And the Server is running at "cache-buster-app"
When I go to "/cache-buster.html" When I go to "/cache-buster.html"
Then I should see "?" Then I should see "site.css?"
Then I should see "blank.gif?"

View file

@ -1,6 +1,4 @@
#main { #main {
padding: 50px; padding: 50px;
// Can't use image-url until compass problem is fixed: https://github.com/middleman/middleman/issues/283 background-image: image-url('100px.jpg');
// background-image: image-url('100px.jpg');
background-image: url('/images/100px.jpg');
} }

View file

@ -0,0 +1,6 @@
activate :asset_hash
activate :directory_indexes
activate :asset_host
set :asset_host, 'http://middlemanapp.com'

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 B

View file

@ -0,0 +1,6 @@
<% content_for :head do %>
<title>The Middleman!</title>
<% end %>
<h2>Image url:</h2>
<%= image_tag('100px.jpg') %>

View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<%= stylesheet_link_tag "site" %>
<%= yield_content :head %>
</head>
<body class="<%= page_classes %>">
<div id="main" role="main">
<%= yield %>
</div>
</body>
</html>

View file

@ -0,0 +1,2 @@
<h2>Image url:</h2>
<img src="<%= image_path('100px.jpg') %>">

View file

@ -0,0 +1,4 @@
#main {
padding: 50px;
background-image: image-url('100px.jpg');
}

View file

@ -0,0 +1,6 @@
<% content_for :head do %>
<title>The Middleman!</title>
<% end %>
<h2>Image url:</h2>
<%= image_tag('100px.jpg') %>

View file

@ -1,2 +1,3 @@
<%= stylesheet_link_tag "site.css" %> <%= stylesheet_link_tag "site.css" %>
<%= javascript_include_tag "empty-with-include.js" %> <%= javascript_include_tag "empty-with-include.js" %>
<%= image_tag "blank.gif" %>

View file

@ -54,7 +54,7 @@ module Middleman::Extensions
elsif sitemap.exists?(real_path_static) elsif sitemap.exists?(real_path_static)
page = sitemap.page(real_path_static) page = sitemap.page(real_path_static)
if !page.template? if !page.template?
http_path << "?" + File.mtime(result[0]).strftime("%s") http_path << "?" + File.mtime(page.source_file).strftime("%s")
else else
# It's a template, possible with partials. We can't really know when # It's a template, possible with partials. We can't really know when
# it's updated, so generate fresh cache buster every time durin # it's updated, so generate fresh cache buster every time durin
@ -71,4 +71,4 @@ module Middleman::Extensions
# Register the extension # Register the extension
# register :cache_buster, CacheBuster # register :cache_buster, CacheBuster
end end