Steven 28.10.2020
Ok, some words about using Fontawesome icons on your website.
When you first get started with the icons, their website suggests you to create a kit and import this kit in the header of your website.
Easy peasy, but it comes with 2 performance disadvantages:
This article demonstrates the performance impact, shows you how to host Fontawesome icons locally, and load only the ones you really use.
The default Fontawesome script is 10kB in size, and takes (when using gzip compression) about 3.8kB to transfer. It also triggers the load of free.min.css. This is the actual stylesheet, containing all icon references (current V5 version here). It is 59kB in size and takes about 12.7kB to transfer.
We measured impact on front page loading, by loading both versions multiple times and averaging out the results.
requests | transferred [kB] | resources [kB] | finish [ms] | DOMContentLoaded [ms] | Load [ms] | |
---|---|---|---|---|---|---|
impact of header script | +3 | +18kB | +72kB | +44ms | +34ms | +43ms |
Well, that’s not huge, right. Noting something strange? The request impact is +3? Well, when testing we noted that the free.min.css file get’s requested twice (and loaded once). We did not go in further details into why this is happening.
As for us, the impact on the loading time is minimal, we are mainly interested in hosting locally because we want to minimize the number of requests and external dependencies.
So, here we go. The base docs are excellent to get you started. Download the kit here:
We don’t need all these files, latest and greatest way to use them is with SVG and Javascript. These files are included in the js/ folder. Easy is including all.js, but this will again load all icons and is not what we are after in the end. We’ll load fontawesome.min.js and then selectively the (modified) icon files we need.
Copy the js/ folder to your wordpress theme. Here is how you can load them through your functions.php file;
function assistancy_load_fa() {
wp_enqueue_script('assistancy-fa', get_template_directory_uri() . '/js/fontawesome.min.js', array(), '', true );
wp_enqueue_script('assistancy-fa-brands', get_template_directory_uri() . '/js/brands.min.js', array(), '', true );
wp_enqueue_script('assistancy-fa-solid', get_template_directory_uri() . '/js/solid.min.js', array(), '', true );
}
add_action( 'wp_enqueue_scripts', 'assistancy_load_fa' );
Note that we’re only loading the brands and solid versions of the icons. In case you need the regular versions as well, add to the above function. Be sure to load the .min versions!
wp_enqueue_script('assistancy-fontawesome-regular', get_template_directory_uri() . '/js/regular.min.js', array(), '', true );
When you take a look at the .js files, you’ll note they come in big sizes.
Open up one of the icon files. Scroll down to the definition of the icon var. This is the point where we will remove all icons we’re not using on our website.
Go ahead, make a list of all icons used on your website, and delete all others directly in the .js file. We were able to downsize the total file size by 1 031kB!
Filename | Original size [kB] | Stripped size [kB] |
---|---|---|
all.min.js | 1167 | not used |
brands.min.js | 438 | 7 |
fontawesome.min.js | 38 | 38 |
regular.min.js | 101 | not used |
solid.min.js | 605 | 5 |
Only downside of this approach? When you want to use a previously-not-used icon on your website, you need to add the additional icon in the corresponding .js file.
Hosting icons locally is straighforward. In terms of website loading time, very little gain is to be expected (but as we all know, many little things…). More important is reducing the total number of requests, and making sure your website is as independent from external sources as possible.
The most confusing thing when you inspect responsive images is the fact that the browser takes into account the DPR (Device Pixel Ratio) in order to download the most appropriate image size. Find out here how to serve your visitors responsive images.
A very easy way to enforce strong passwords in Wordpress, is to disable the 'confirm weak password' checkbox. On top of this, we optimize the loading of the password strength estimator, zxcvbn.
Wordpress 403 Forbidden errors typically happen with corrupt .htaccess or faulty file/folder permissions. A third common cause is often overlooked: the use of the Wordpress app through XML-RPC.
Reed Sutton
13.03.2024 - 22:24With font awesome 6, hosting locally, I am having trouble getting icons to display using pesudo elements in CSS (including the new syntax here: https://docs.fontawesome.com/web/setup/upgrade/pseudo-elements).
Any idea why?