Commit a068fe3f authored by Robert May's avatar Robert May

Memoize the rendered icon tags and partials

parent c0dfeca4
...@@ -24,9 +24,11 @@ module IconsHelper ...@@ -24,9 +24,11 @@ module IconsHelper
end end
def custom_icon(icon_name, size: DEFAULT_ICON_SIZE) def custom_icon(icon_name, size: DEFAULT_ICON_SIZE)
# We can't simply do the below, because there are some .erb SVGs. memoized_icon("#{icon_name}_#{size}") do
# File.read(Rails.root.join("app/views/shared/icons/_#{icon_name}.svg")).html_safe # We can't simply do the below, because there are some .erb SVGs.
render "shared/icons/#{icon_name}.svg", size: size # File.read(Rails.root.join("app/views/shared/icons/_#{icon_name}.svg")).html_safe
render "shared/icons/#{icon_name}.svg", size: size
end
end end
def sprite_icon_path def sprite_icon_path
...@@ -46,21 +48,23 @@ module IconsHelper ...@@ -46,21 +48,23 @@ module IconsHelper
end end
def sprite_icon(icon_name, size: DEFAULT_ICON_SIZE, css_class: nil) def sprite_icon(icon_name, size: DEFAULT_ICON_SIZE, css_class: nil)
if known_sprites&.exclude?(icon_name) memoized_icon("#{icon_name}_#{size}_#{css_class}") do
exception = ArgumentError.new("#{icon_name} is not a known icon in @gitlab-org/gitlab-svg") if known_sprites&.exclude?(icon_name)
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(exception) exception = ArgumentError.new("#{icon_name} is not a known icon in @gitlab-org/gitlab-svg")
end Gitlab::ErrorTracking.track_and_raise_for_dev_exception(exception)
end
css_classes = [] css_classes = []
css_classes << "s#{size}" if size css_classes << "s#{size}" if size
css_classes << "#{css_class}" unless css_class.blank? css_classes << "#{css_class}" unless css_class.blank?
content_tag( content_tag(
:svg, :svg,
content_tag(:use, '', { 'xlink:href' => "#{sprite_icon_path}##{icon_name}" } ), content_tag(:use, '', { 'xlink:href' => "#{sprite_icon_path}##{icon_name}" } ),
class: css_classes.empty? ? nil : css_classes.join(' '), class: css_classes.empty? ? nil : css_classes.join(' '),
data: { testid: "#{icon_name}-icon" } data: { testid: "#{icon_name}-icon" }
) )
end
end end
def loading_icon(container: false, color: 'orange', size: 'sm', css_class: nil) def loading_icon(container: false, color: 'orange', size: 'sm', css_class: nil)
...@@ -169,4 +173,12 @@ module IconsHelper ...@@ -169,4 +173,12 @@ module IconsHelper
@known_sprites ||= Gitlab::Json.parse(File.read(Rails.root.join('node_modules/@gitlab/svgs/dist/icons.json')))['icons'] @known_sprites ||= Gitlab::Json.parse(File.read(Rails.root.join('node_modules/@gitlab/svgs/dist/icons.json')))['icons']
end end
def memoized_icon(key)
@rendered_icons ||= {}
@rendered_icons[key] || (
@rendered_icons[key] = yield
)
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment