Skip to content

Commit

Permalink
Fixed asset host to not cache objects [#1419 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
Aaron Batalion authored and josh committed Nov 19, 2008
1 parent dff4ab9 commit 24dbd4b
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions actionpack/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -151,7 +151,7 @@ def auto_discovery_link_tag(type = :rss, url_options = {}, tag_options = {})
# javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr.js
# javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js
def javascript_path(source)
JavaScriptTag.create(self, @controller, source).public_path
JavaScriptTag.new(self, @controller, source).public_path
end
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route

Expand Down Expand Up @@ -314,7 +314,7 @@ def self.reset_javascript_include_default #:nodoc:
# stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style.css
# stylesheet_path "http://www.railsapplication.com/css/style.js" # => http://www.railsapplication.com/css/style.css
def stylesheet_path(source)
StylesheetTag.create(self, @controller, source).public_path
StylesheetTag.new(self, @controller, source).public_path
end
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route

Expand Down Expand Up @@ -411,7 +411,7 @@ def stylesheet_link_tag(*sources)
# image_path("/icons/edit.png") # => /icons/edit.png
# image_path("http://www.railsapplication.com/img/edit.png") # => http://www.railsapplication.com/img/edit.png
def image_path(source)
ImageTag.create(self, @controller, source).public_path
ImageTag.new(self, @controller, source).public_path
end
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route

Expand Down Expand Up @@ -527,20 +527,6 @@ class AssetTag
Cache = {}
CacheGuard = Mutex.new

def self.create(template, controller, source, include_host = true)
CacheGuard.synchronize do
key = if controller.respond_to?(:request)
[self, controller.request.protocol,
ActionController::Base.asset_host,
ActionController::Base.relative_url_root,
source, include_host]
else
[self, ActionController::Base.asset_host, source, include_host]
end
Cache[key] ||= new(template, controller, source, include_host).freeze
end
end

ProtocolRegexp = %r{^[-a-z]+://}.freeze

def initialize(template, controller, source, include_host = true)
Expand All @@ -551,8 +537,16 @@ def initialize(template, controller, source, include_host = true)
@controller = controller
@source = source
@include_host = include_host
@cache_key = if controller.respond_to?(:request)
[controller.request.protocol,
ActionController::Base.asset_host,
ActionController::Base.relative_url_root,
source, include_host]
else
[ActionController::Base.asset_host, source, include_host]
end
end

def public_path
compute_public_path(@source)
end
Expand Down Expand Up @@ -585,20 +579,32 @@ def request?
# roots. Rewrite the asset path for cache-busting asset ids. Include
# asset host, if configured, with the correct request protocol.
def compute_public_path(source)
source += ".#{extension}" if missing_extension?(source)
unless source =~ ProtocolRegexp
source = "/#{directory}/#{source}" unless source[0] == ?/
source = rewrite_asset_path(source)
source = prepend_relative_url_root(source)
if source =~ ProtocolRegexp
source += ".#{extension}" if missing_extension?(source)
source = prepend_asset_host(source)
source
else
CacheGuard.synchronize do
Cache[@cache_key] ||= begin
source += ".#{extension}" if missing_extension?(source) || file_exists_with_extension?(source)
source = "/#{directory}/#{source}" unless source[0] == ?/
source = rewrite_asset_path(source)
source = prepend_relative_url_root(source)
source = prepend_asset_host(source)
source
end
end
end
source = prepend_asset_host(source)
source
end

def missing_extension?(source)
extension && (File.extname(source).blank? || File.exist?(File.join(ASSETS_DIR, directory, "#{source}.#{extension}")))
extension && File.extname(source).blank?
end


def file_exists_with_extension?(source)
extension && File.exist?(File.join(ASSETS_DIR, directory, "#{source}.#{extension}"))
end

def prepend_relative_url_root(source)
relative_url_root = ActionController::Base.relative_url_root
if request? && @include_host && source !~ %r{^#{relative_url_root}/}
Expand Down Expand Up @@ -735,7 +741,7 @@ def all_asset_files
end

def tag_sources
expand_sources.collect { |source| tag_class.create(@template, @controller, source, false) }
expand_sources.collect { |source| tag_class.new(@template, @controller, source, false) }
end

def joined_contents
Expand Down

0 comments on commit 24dbd4b

Please sign in to comment.