From 25eea058335ed0149621fb7470c9e8db5a24b481 Mon Sep 17 00:00:00 2001 From: Tim Zallmann Date: Thu, 9 Nov 2017 10:38:00 +0100 Subject: [PATCH] Icon Sprite URL is also local even if asset_host is set --- app/helpers/icons_helper.rb | 10 +++++++++- lib/gitlab/gon_helper.rb | 2 +- spec/helpers/icons_helper_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/helpers/icons_helper.rb b/app/helpers/icons_helper.rb index ec779c1c447..fcd41b798a1 100644 --- a/app/helpers/icons_helper.rb +++ b/app/helpers/icons_helper.rb @@ -1,5 +1,6 @@ module IconsHelper extend self + include Gitlab::CurrentSettings include FontAwesome::Rails::IconHelper # Creates an icon tag given icon name(s) and possible icon modifiers. @@ -23,10 +24,17 @@ module IconsHelper render "shared/icons/#{icon_name}.svg", size: size end + def sprite_icon_path + # SVG Sprites currently don't work across domains, so in the case of a CDN + # we have to set the current path deliberately to prevent addition of asset_host + sprite_base_url = ActionController::Base.asset_host.present? ? Gitlab.config.gitlab.url : nil + ActionController::Base.helpers.image_path('icons.svg', host: sprite_base_url) + end + def sprite_icon(icon_name, size: nil, css_class: nil) css_classes = size ? "s#{size}" : "" css_classes << " #{css_class}" unless css_class.blank? - content_tag(:svg, content_tag(:use, "", { "xlink:href" => "#{image_path('icons.svg')}##{icon_name}" } ), class: css_classes.empty? ? nil : css_classes) + content_tag(:svg, content_tag(:use, "", { "xlink:href" => "#{sprite_icon_path()}##{icon_name}" } ), class: css_classes.empty? ? nil : css_classes) end def audit_icon(names, options = {}) diff --git a/lib/gitlab/gon_helper.rb b/lib/gitlab/gon_helper.rb index 3a666c2268b..dfcdfc307b6 100644 --- a/lib/gitlab/gon_helper.rb +++ b/lib/gitlab/gon_helper.rb @@ -20,7 +20,7 @@ module Gitlab gon.gitlab_url = Gitlab.config.gitlab.url gon.revision = Gitlab::REVISION gon.gitlab_logo = ActionController::Base.helpers.asset_path('gitlab_logo.png') - gon.sprite_icons = ActionController::Base.helpers.asset_path('icons.svg') + gon.sprite_icons = IconsHelper.sprite_icon_path if current_user gon.current_user_id = current_user.id diff --git a/spec/helpers/icons_helper_spec.rb b/spec/helpers/icons_helper_spec.rb index 3d79dac284f..0b5d96aaa2b 100644 --- a/spec/helpers/icons_helper_spec.rb +++ b/spec/helpers/icons_helper_spec.rb @@ -16,6 +16,27 @@ describe IconsHelper do end end + describe 'sprite_icon' do + it 'returns relative path' do + expect(sprite_icon_path()) + .to eq "/image/icons.svg" + end + + context 'when an asset_host is set in the config it will return an absolute local URL' do + let(:asset_host) { 'http://assets' } + + before do + allow(ActionController::Base).to receive(:asset_host).and_return(asset_host) + end + + it 'returns an absolute URL on that asset host' do + gitlab_url = Gitlab.config.gitlab.url + expect(sprite_icon_path()) + .to eq "#{gitlab_url}/image/icons.svg" + end + end + end + describe 'sprite_icon' do icon_name = 'clock'