Fix asset hash with query string or hash params. Closes #1411

This commit is contained in:
Thomas Reynolds 2015-12-21 13:07:01 -08:00
parent ba0416b330
commit a5821bccec
10 changed files with 79 additions and 27 deletions

View file

@ -64,9 +64,13 @@ Feature: Assets get file hashes appended to them and references to them are upda
When I go to "/" When I go to "/"
Then I should see 'href="apple-touch-icon.png"' Then I should see 'href="apple-touch-icon.png"'
And I should see 'href="stylesheets/site-d2959d87.css"' And I should see 'href="stylesheets/site-d2959d87.css"'
And I should see 'href="stylesheets/fragment-a06f0dfc.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"'
And I should see 'srcset="images/100px-5fd6fb90.jpg 1x, images/200px-c11eb203.jpg 2x, images/300px-59adce76.jpg 3x"' And I should see 'srcset="images/100px-5fd6fb90.jpg 1x, images/200px-c11eb203.jpg 2x, images/300px-59adce76.jpg 3x"'
And I should see 'src="images/100px-5fd6fb90.jpg?test"'
And I should see 'src="images/100px-5fd6fb90.jpg?#test"'
And I should see 'src="images/100px-5fd6fb90.jpg#test"'
When I go to "/subdir/" When I go to "/subdir/"
Then I should see 'href="../stylesheets/site-d2959d87.css"' Then I should see 'href="../stylesheets/site-d2959d87.css"'
And I should see 'src="../javascripts/application-1d8d5276.js"' And I should see 'src="../javascripts/application-1d8d5276.js"'
@ -75,6 +79,9 @@ Feature: Assets get file hashes appended to them and references to them are upda
Then I should see 'href="../stylesheets/site-d2959d87.css"' Then I should see 'href="../stylesheets/site-d2959d87.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"'
And I should see 'src="../images/100px-5fd6fb90.jpg?test"'
And I should see 'src="../images/100px-5fd6fb90.jpg?#test"'
And I should see 'src="../images/100px-5fd6fb90.jpg#test"'
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-d2959d87.css" When I go to "/stylesheets/site-d2959d87.css"
@ -87,6 +94,11 @@ Feature: Assets get file hashes appended to them and references to them are upda
Then I should see 'images/100px-5fd6fb90.gif' Then I should see 'images/100px-5fd6fb90.gif'
And I should see 'images/100px-5fd6fb90.jpg' And I should see 'images/100px-5fd6fb90.jpg'
And I should see 'images/100px-1242c368.png' And I should see 'images/100px-1242c368.png'
When I go to "/stylesheets/fragment-a06f0dfc.css"
And I should see 'url("../images/100px-5fd6fb90.jpg")'
And I should see 'url("../images/100px-5fd6fb90.jpg?test")'
And I should see 'url("../images/100px-5fd6fb90.jpg?#test")'
And I should see 'url("../images/100px-5fd6fb90.jpg#test")'
Scenario: Hashed assets work with Slim Scenario: Hashed assets work with Slim
Given the Server is running at "asset-hash-app" Given the Server is running at "asset-hash-app"
@ -99,13 +111,25 @@ Feature: Assets get file hashes appended to them and references to them are upda
Given the Server is running at "asset-hash-host-app" Given the Server is running at "asset-hash-host-app"
When I go to "/" When I go to "/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-e587b659.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/site-e587b659.css"'
Then I should see 'href="http://middlemanapp.com/stylesheets/fragment-7af0b5ab.css"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"'
When I go to "/subdir/" When I go to "/subdir/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-e587b659.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/site-e587b659.css"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
When I go to "/other/" When I go to "/other/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-e587b659.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/site-e587b659.css"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"'
When I go to "/stylesheets/fragment-7af0b5ab.css"
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg")'
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?test")'
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test")'
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg#test")'
Scenario: The asset hash should change when a SASS partial changes Scenario: The asset hash should change when a SASS partial changes
Given the Server is running at "asset-hash-app" Given the Server is running at "asset-hash-app"

View file

@ -4,6 +4,9 @@
<h2>Image url:</h2> <h2>Image url:</h2>
<img src="<%= image_path('100px.jpg') %>" srcset="<%= image_path('100px.jpg') %> 1x, <%= image_path('200px.jpg') %> 2x, <%= image_path('300px.jpg') %> 3x"> <img src="<%= image_path('100px.jpg') %>" srcset="<%= image_path('100px.jpg') %> 1x, <%= image_path('200px.jpg') %> 2x, <%= image_path('300px.jpg') %> 3x">
<%= image_tag('100px.jpg?test') %>
<%= image_tag('100px.jpg?#test') %>
<%= image_tag('100px.jpg#test') %>
<h2>Ignored path:</h2> <h2>Ignored path:</h2>
<link rel="apple-touch-icon" href="apple-touch-icon.png"> <link rel="apple-touch-icon" href="apple-touch-icon.png">

View file

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<%= stylesheet_link_tag "site" %> <%= stylesheet_link_tag "site", "fragment" %>
<%= javascript_include_tag "application" %> <%= javascript_include_tag "application" %>
<%= yield_content :head %> <%= yield_content :head %>
</head> </head>

View file

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

View file

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

View file

@ -4,3 +4,6 @@
<h2>Image url:</h2> <h2>Image url:</h2>
<%= image_tag('100px.jpg') %> <%= image_tag('100px.jpg') %>
<%= image_tag('100px.jpg?test') %>
<%= image_tag('100px.jpg?#test') %>
<%= image_tag('100px.jpg#test') %>

View file

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<%= stylesheet_link_tag "site" %> <%= stylesheet_link_tag "site", "fragment" %>
<%= yield_content :head %> <%= yield_content :head %>
</head> </head>

View file

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

View file

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

View file

@ -201,9 +201,11 @@ module Middleman
Contract IsA['Middleman::Application'], String, String, Hash => String Contract IsA['Middleman::Application'], String, String, Hash => String
def asset_url(app, path, prefix='', options={}) def asset_url(app, path, prefix='', options={})
# Don't touch assets which already have a full path # Don't touch assets which already have a full path
if path.include?('//') || path.start_with?('data:') return path if path.include?('//') || path.start_with?('data:')
path
else # rewrite paths to use their destination path uri = URI(path)
path = uri.path
result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path)) result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path))
resource.url resource.url
else else
@ -215,7 +217,7 @@ module Middleman
end end
end end
if options[:relative] != true final_result = if options[:relative] != true
result result
else else
unless options[:current_resource] unless options[:current_resource]
@ -225,7 +227,11 @@ module Middleman
current_dir = Pathname('/' + options[:current_resource].destination_path) current_dir = Pathname('/' + options[:current_resource].destination_path)
Pathname(result).relative_path_from(current_dir.dirname).to_s Pathname(result).relative_path_from(current_dir.dirname).to_s
end end
end
result_uri = URI(final_result)
result_uri.query = uri.query
result_uri.fragment = uri.fragment
result_uri.to_s
end end
# Given a source path (referenced either absolutely or relatively) # Given a source path (referenced either absolutely or relatively)