Project

General

Profile

Feature #44415 ยป 0001-Resolve-each-icon-sprite-path-only-once-per-request.patch

Go MAEDA, 2026-09-03 11:35

View differences:

app/helpers/icons_helper.rb
css_classes += " #{css_class}" unless css_class.nil?
css_classes += " icon-rtl" if rtl
# Resolve each sprite path only once per request: asset_path is not
# cheap and this helper runs for every icon on a page
@sprite_asset_paths ||= {}
path = @sprite_asset_paths[sprite] ||= asset_path(sprite)
content_tag(
:svg,
content_tag(:use, '', { 'href' => "#{asset_path(sprite)}#icon--#{icon_name}" }),
content_tag(:use, '', { 'href' => "#{path}#icon--#{icon_name}" }),
class: css_classes,
aria: {
hidden: true
test/helpers/icons_helper_test.rb
assert_match expected, icon
end
def test_sprite_icon_should_resolve_each_sprite_path_once
expects(:asset_path).with('icons.svg').once.returns('/assets/icons-abc.svg')
expects(:asset_path).with('custom.svg').once.returns('/assets/custom.svg')
3.times { sprite_icon('edit') }
2.times { sprite_icon('edit', sprite: 'custom') }
assert_match %r{href="/assets/icons-abc.svg#icon--edit"}, sprite_icon('edit')
assert_match %r{href="/assets/custom.svg#icon--edit"}, sprite_icon('edit', sprite: 'custom')
end
def test_sprite_icon_should_return_svg_with_custom_sprite
expected = %r{<svg class="s18 icon-svg" aria-hidden="true"><use href="/assets/custom.svg#icon--edit"></use></svg>$}
icon = sprite_icon('edit', sprite: 'custom')
    (1-1/1)