Fix asset hash with query string or hash params. Closes #1411
This commit is contained in:
parent
ba0416b330
commit
a5821bccec
10 changed files with 79 additions and 27 deletions
|
@ -201,31 +201,37 @@ module Middleman
|
|||
Contract IsA['Middleman::Application'], String, String, Hash => String
|
||||
def asset_url(app, path, prefix='', options={})
|
||||
# Don't touch assets which already have a full path
|
||||
if path.include?('//') || path.start_with?('data:')
|
||||
path
|
||||
else # rewrite paths to use their destination path
|
||||
result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path))
|
||||
return path if path.include?('//') || path.start_with?('data:')
|
||||
|
||||
uri = URI(path)
|
||||
path = uri.path
|
||||
|
||||
result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path))
|
||||
resource.url
|
||||
else
|
||||
path = File.join(prefix, path)
|
||||
if resource = app.sitemap.find_resource_by_path(path)
|
||||
resource.url
|
||||
else
|
||||
path = File.join(prefix, path)
|
||||
if resource = app.sitemap.find_resource_by_path(path)
|
||||
resource.url
|
||||
else
|
||||
File.join(app.config[:http_prefix], path)
|
||||
end
|
||||
end
|
||||
|
||||
if options[:relative] != true
|
||||
result
|
||||
else
|
||||
unless options[:current_resource]
|
||||
raise ArgumentError, '#asset_url must be run in a context with current_resource if relative: true'
|
||||
end
|
||||
|
||||
current_dir = Pathname('/' + options[:current_resource].destination_path)
|
||||
Pathname(result).relative_path_from(current_dir.dirname).to_s
|
||||
File.join(app.config[:http_prefix], path)
|
||||
end
|
||||
end
|
||||
|
||||
final_result = if options[:relative] != true
|
||||
result
|
||||
else
|
||||
unless options[:current_resource]
|
||||
raise ArgumentError, '#asset_url must be run in a context with current_resource if relative: true'
|
||||
end
|
||||
|
||||
current_dir = Pathname('/' + options[:current_resource].destination_path)
|
||||
Pathname(result).relative_path_from(current_dir.dirname).to_s
|
||||
end
|
||||
|
||||
result_uri = URI(final_result)
|
||||
result_uri.query = uri.query
|
||||
result_uri.fragment = uri.fragment
|
||||
result_uri.to_s
|
||||
end
|
||||
|
||||
# Given a source path (referenced either absolutely or relatively)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue