Skip to content

Commit b992fb2

Browse files
committed
[Icons] Improve cache warmup by looking at every string
1 parent 554ebba commit b992fb2

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

src/Icons/doc/index.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ In production, you can pre-warm the cache by running the following command:
115115
116116
This command looks in all your twig templates for ``ux_icon`` calls and caches the icons it finds.
117117

118+
.. caution::
119+
120+
Icons that have a name built dynamically will not be cached. It's advised to have the icon
121+
name as a string literal in your templates.
122+
123+
.. code-block:: twig
124+
125+
{# This will be cached #}
126+
{{ ux_icon('flag-fr') }}
127+
128+
{# This will NOT be cached #}
129+
{{ ux_icon('flag-' ~ locale) }}
130+
131+
{% set flags = {fr: 'flag-fr', de: 'flag-de'} %} {# both "flag-fr" and "flag-de" will be cached #}
132+
{{ ux_icon(flags[locale]) }}
133+
118134
.. note::
119135

120136
During development, if you modify an icon, you will need to clear the cache (``bin/console cache:clear``)

src/Icons/src/Command/WarmCacheCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4545
$io->writeln(sprintf(' Warmed icon <comment>%s</comment>.', $name));
4646
}
4747
},
48-
onFailure: fn (string $name) => $io->warning(sprintf('Icon <comment>%s</comment> not found.', $name))
4948
);
5049

5150
$io->success('Icon cache warmed.');

src/Icons/src/Twig/IconFinder.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ public function icons(): array
3838
foreach ($this->files($this->twig->getLoader()) as $file) {
3939
$contents = file_get_contents($file);
4040

41-
if (preg_match_all('#ux_icon\(["\']([\w:-]+)["\']#', $contents, $matches)) {
42-
$found[] = $matches[1];
43-
}
44-
45-
if (preg_match_all('#name=["\']([\w:-]+)["\']#', $contents, $matches)) {
41+
if (preg_match_all('#["\']([\w:-]+)["\']#', $contents, $matches)) {
4642
$found[] = $matches[1];
4743
}
4844
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{% set icon = 'flag:eu-4x3' %}
2+
13
{{ ux_icon('something:invalid') }}
24
{{ ux_icon('invalid') }}
35
{{ ux_icon('iconamoon:invalid') }}

src/Icons/tests/Integration/Command/WarmCacheCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function testCanWarmCache(): void
2929
->assertOutputContains('Warmed icon user.')
3030
->assertOutputContains('Warmed icon sub:check.')
3131
->assertOutputContains('Warmed icon iconamoon:3d-duotone.')
32+
->assertOutputContains('Warmed icon flag:eu-4x3.')
3233
->assertOutputContains('Icon cache warmed.')
3334
;
3435
}

0 commit comments

Comments
 (0)