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

Limit Font Awesome imports to reduce CSS size #4516

Merged
merged 1 commit into from
Jul 2, 2021
Merged

Conversation

javierm
Copy link
Member

@javierm javierm commented May 16, 2021

References

Background

We were using Font Awesome fonts and selectors to support the browsers which don't support mask images (as the time of writing, about 5% of the browsers). However, we were only importing the selectors in order to extend them. This resulted in our compiled CSS including styles for every Font Awesome icon (currenty, more than a thousand), even if we only use a few of them.

Objectives

  • Make the page load faster by removing unused selectors in the compiled CSS

Notes

With these changes, the size of the application.css file is reduced from 539KB to 475KB.

Release notes

⚠️ CONSUL installations using Font Awesome HTML icon classes or extending Font Awesome selectors (like .fa-icon_name) will now have to add extra @import clauses in ther custom.scss file to import files like font-awesome/icons. Another option for these installations would be to remove these classes/selectors and use our has-fa-icon mixin.

@javierm javierm added this to Reviewing in Consul Democracy via automation May 16, 2021
@javierm javierm self-assigned this May 16, 2021
@javierm javierm force-pushed the application_css branch 2 times, most recently from 2845033 to b549eb4 Compare June 2, 2021 14:15
Base automatically changed from application_css to master June 2, 2021 15:04
@javierm javierm force-pushed the reduce_font_awesome branch 3 times, most recently from b8aa9db to a0f2577 Compare June 21, 2021 18:17
@Senen Senen self-assigned this Jun 30, 2021
@javierm javierm force-pushed the reduce_font_awesome branch 2 times, most recently from e4848e2 to f9fde6b Compare June 30, 2021 12:57
We were using Font Awesome fonts and selectors to support the browsers
which don't support mask images (at the time of writing, about 5% of the
browsers). However, we were only importing the selectors in order to
extend them. This resulted in our compiled CSS including styles for
every Font Awesome icon (currenty, more than a thousand), even if we
only use a few of them.

So we're using Font Awesome variables instead of using the selectors it
provides. Since variables are only compiled in the CSS if they're
actually used, this reduces the size of our compiled CSS considerably.
In production environments, the size is reduced from 539KB to 475KB,
meaning we reduce its size in about 12%.

The downside here is we can't easily use Font Awesome variables in our
Sass mixins because we can't use interpolation in variable names (that
is, we can't use `$fa-var-#{icon}`). So we're using a map containing all
Font Awesome variables in order to access it in the mixin.

Note installations using `.fa-*` selectors will now have to add extra
`@import` clauses.
Copy link
Member

@Senen Senen left a comment

Choose a reason for hiding this comment

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

Nice @javierm!

I checked the difference between the v1.3.1 and reduce_font_awesome branch, and in my case, I found a difference of ~38KB on a production server.

Before
Application CSS size before

After
Application CSS size after

Consul Democracy automation moved this from Reviewing to Testing Jul 2, 2021
@javierm javierm merged commit 0262104 into master Jul 2, 2021
Consul Democracy automation moved this from Testing to Release 1.4.0 Jul 2, 2021
@javierm javierm deleted the reduce_font_awesome branch July 2, 2021 11:49
@javierm javierm added the UX label Oct 23, 2021
@taitus taitus mentioned this pull request Nov 10, 2021
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.

None yet

2 participants