Commit 4914291c authored by Stan Hu's avatar Stan Hu

Merge branch 'memoized_icon_helpers' into 'master'

Memoize the rendered icon tags and partials

See merge request gitlab-org/gitlab!47269
parents e41cddf1 a068fe3f
......@@ -24,10 +24,12 @@ module IconsHelper
end
def custom_icon(icon_name, size: DEFAULT_ICON_SIZE)
memoized_icon("#{icon_name}_#{size}") do
# We can't simply do the below, because there are some .erb SVGs.
# File.read(Rails.root.join("app/views/shared/icons/_#{icon_name}.svg")).html_safe
render "shared/icons/#{icon_name}.svg", size: size
end
end
def sprite_icon_path
@sprite_icon_path ||= begin
......@@ -46,6 +48,7 @@ module IconsHelper
end
def sprite_icon(icon_name, size: DEFAULT_ICON_SIZE, css_class: nil)
memoized_icon("#{icon_name}_#{size}_#{css_class}") do
if known_sprites&.exclude?(icon_name)
exception = ArgumentError.new("#{icon_name} is not a known icon in @gitlab-org/gitlab-svg")
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(exception)
......@@ -62,6 +65,7 @@ module IconsHelper
data: { testid: "#{icon_name}-icon" }
)
end
end
def loading_icon(container: false, color: 'orange', size: 'sm', css_class: nil)
css_classes = ['gl-spinner', "gl-spinner-#{color}", "gl-spinner-#{size}"]
......@@ -169,4 +173,12 @@ module IconsHelper
@known_sprites ||= Gitlab::Json.parse(File.read(Rails.root.join('node_modules/@gitlab/svgs/dist/icons.json')))['icons']
end
def memoized_icon(key)
@rendered_icons ||= {}
@rendered_icons[key] || (
@rendered_icons[key] = yield
)
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