Skip to content

[Icons] Improve cache warmup by looking at every string #1594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2024

Conversation

kbond
Copy link
Member

@kbond kbond commented Mar 9, 2024

Q A
Bug fix? no
New feature? yes
Issues n/a
License MIT

Improve working with dynamic icon names (inspired by https://v2.tailwindcss.com/docs/optimizing-for-production#writing-purgeable-html)

{# This will be cached #}
{{ ux_icon('flag-fr') }}

{# This will NOT be cached #}
{{ ux_icon('flag-' ~ locale) }}

{% set flags = {fr: 'flag-fr', de: 'flag-de'} %} {# both "flag-fr" and "flag-de" will be cached #}
{{ ux_icon(flags[locale]) }}

Closes #1601

@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Mar 9, 2024
@kbond kbond force-pushed the icons/improve-cache-warmup branch from 9d3aa17 to b992fb2 Compare March 9, 2024 01:23
@kbond
Copy link
Member Author

kbond commented Mar 9, 2024

@smnandre made some fair points about performance/security.

Regarding security: I want to clarify that only strings that match 'foo:bar' will be used to make http requests to iconify. 'foo:bar' will create an http request with this value in the request path but 'something-sensitive' will not. However, 'something:sensitive' would create an http request with this value in the request path.

I'd like to find a compromise that we're all ok with, some thoughts:

  • opt-in to this much looser string check
  • configure specific paths/globs (like tailwind does)

@smnandre
Copy link
Member

smnandre commented Mar 9, 2024

We can add a failsafe: store locally the list of available icon prefixes.

Then we reverse the logic:

  • collect all the icons as you do now
  • list the prefixes
  • download the list of icons for each prefix

--> finally download the icons.

It can be even faster in the end, because we can group parallelize + group icon downloads (up to 500 per request)

@smnandre
Copy link
Member

smnandre commented Mar 9, 2024

I'm reading the doc

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

Could we add a isFresh / mtime check about that ? What do you think ?

@kbond
Copy link
Member Author

kbond commented Mar 9, 2024

I believe the security concerns have been alleviated. I now only make the http request if the prefix is valid. 'something:sensitive' will no longer create a request.

Could we add a isFresh / mtime check about that ? What do you think ?

I'd prefer to avoid this complexity until this is determined to be a problem. I feel this situation is be rare.

if (!isset($this->sets()[$prefix])) {
throw new IconNotFoundException(sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
}

$response = $this->http->request('GET', sprintf('/%s.json?icons=%s', $prefix, $name));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If (i insist on if) this become the bottleneck, we have a card in hand: grouping icons per set https://iconify.design/docs/api/icon-data.html#query

@@ -38,11 +38,7 @@ public function icons(): array
foreach ($this->files($this->twig->getLoader()) as $file) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we exclude the Bundle ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What bundle?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, i think Twig loader has been registered with all the bundles of the app. So it contains all the files in "vendor/acme/*-bundle/Resources/templates" and we should (imho) not go there to extract things.

Imagine a twig file there with a list of icons (think any template using JS icons)... the probability to have 'bi:check' string is far from null.

We even could see this as a vector "attack", a single file in twig from a 3rd party bundle would make you downlaod icons locally / generate a lot of download.

Oh and i'll check something about that we may have a problem there.

So here i'd like us to only look in userland files :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you had a bundle of that uses icons that you wanted to use? I prefer to keep it simple and adjust when/if we come across these type of problems.

@smnandre
Copy link
Member

I love this!

@kbond kbond force-pushed the icons/improve-cache-warmup branch from c2c93d9 to c3f7627 Compare March 10, 2024 19:46
@kbond kbond merged commit dd85ab2 into symfony:2.x Mar 10, 2024
@kbond kbond deleted the icons/improve-cache-warmup branch March 10, 2024 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Review Needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants