Skip to content
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

Load SVG icons using asset-data-url #4527

Merged
merged 1 commit into from
Jul 5, 2021
Merged

Load SVG icons using asset-data-url #4527

merged 1 commit into from
Jul 5, 2021

Conversation

javierm
Copy link
Member

@javierm javierm commented May 21, 2021

References

Background

Back in commit 925f04e from pull request #4206 we wrote about our way to load SVG icons:

Using this technique will result in one HTTP request per icon, which might affect performance

We considered using CSS with Data URIs, and wrote:

This method does not generate any extra HTTP requests and only downloads the icons we need. However, maintaining it is really hard, since we need to manually copy all the code for every icon we use, and do it again every time we upgrade Font Awesome.

Back when I wrote that, I didn't know Sass had a function named asset-data-url which generated Data URIs automatically given a filename. I searched for it, but somehow I only found Compass helpers doing a similar thing.

Objectives

  • Reduce the number of HTTP requests we get to download SVG icons
  • Make the icons load faster

Notes

After these changes, the size of the generated CSS increases from 475KB to 533KB. If we didn't use CSS variables, the generated CSS would use 591KB.

We believe this is acceptable because the SVG icons we use are very small files (about 1-1.5KB big) and, downloaded separately, they also amount to about 45KB, which is similar to the CSS file increase we get. Using asset-data-url we download them in one request instead of having one request per file (about 35 extra requests).

@javierm javierm self-assigned this May 21, 2021
@javierm javierm force-pushed the asset_data_url branch 5 times, most recently from 857f3af to e03ae9f Compare May 21, 2021 20:49
@javierm javierm added the UX label May 23, 2021
@javierm javierm force-pushed the reduce_font_awesome branch 2 times, most recently from b8aa9db to a0f2577 Compare June 21, 2021 18:17
@javierm javierm force-pushed the reduce_font_awesome branch 3 times, most recently from f9fde6b to 4d49ec8 Compare June 30, 2021 14:41
Base automatically changed from reduce_font_awesome to master July 2, 2021 11:49
@javierm javierm force-pushed the asset_data_url branch 2 times, most recently from 6fd161c to b95c9e8 Compare July 2, 2021 13:21
@Senen Senen self-assigned this Jul 2, 2021
Back in commit 925f04e from pull request #4206 we wrote about our way
to load SVG icons:

> Using this technique will result in one HTTP request per icon, which
> might affect performance

We considered using CSS with Data URIs, and wrote:

> This method does not generate any extra HTTP requests and only
> downloads the icons we need. However, maintaining it is really hard,
> since we need to manually copy all the <svg> code for every icon we
> use, and do it again every time we upgrade Font Awesome.

Back when I wrote that, I didn't know Sass had a function named
`asset-data-url` which generated Data URIs automatically given a
filename. I searched for it, but somehow I only found Compass helpers
doing a similar thing.

Note we're using CSS variables to reduce the size of the generated CSS.
If we used `mask-image: asset-data-url(...)`, the generated CSS would
include the value returned by `asset-data-url` twice: once for the
`mask-image` property and once for the `-webkit-image` property.

The percentage of browsers supporting `mask-image` and not supporting
CSS variables is really small (less than 1%), so in that regard things
remain more or less the same and unsupported browsers will render the
icons using the `font-family: "Font Awesome 5 Free` property.

After these changes, the size of the generated CSS increases from 475KB
to 533KB. If we didn't use CSS variables, the generated CSS would use
591KB.

We believe this is acceptable because the SVG icons we use are very
small files (about 1-1.5KB big) and, downloaded separately, they also
amount to about 45KB, which is similar to the CSS file increase we get.
Using `asset-data-url` we download them in one request instead of having
one request per file (about 35 extra requests).
@javierm javierm merged commit 3b71974 into master Jul 5, 2021
@javierm javierm deleted the asset_data_url branch July 5, 2021 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants