Offline Ionicons – Getting them working

ionicons-logo

We’ve heard of several folk having issues getting their Ionic powered mobile applications working offline correctly when using the Ionicons. We’ll show you 2 quick ways that should help you out if you’re having the same issue. Offline ionicons are possible, and not difficult to implement.

The Issue

When using the HTML application cache you define a list of assets to be cached locally, allowing for local serving of them. This gives you performance increases and also offline capabilities. These assets are listed under the CACHE MANIFEST header. This is the section where, if using Ionicons in your Ionic application, you could be tempted to list the following;

You’d then potentially use Sass to process the SCSS files supplied with Ionic and you’d soon see that your icons weren’t displaying offline.

This occurs due to the Ionic SCSS supplying a version number into a query param for the Ionicon font files.

What this means is that the URLs for the font files in the outputted CSS are actually like this;

What has happened here is that the the URLs now have ?v=1.5.2 appended to them… and this means that our CACHE MANIFEST entries will not match and our app cache will not cache them, and so our offline ionicons won’t be there. Sadly this quirk of app cache isn’t mentioned in either the pages over at w3schools.com or HTML5 Rocks, and is therefore quite commonly missed.

Solution 1 : Correct your CACHE MANIFEST

Update your CACHE MANIFEST to include the query parameters that are associated with the version of Ionicons that is being used.

Solution 2 : Modify the Ionicons SCSS

An alternative is to modify the ionicons-font.scss file to remove the code that inserts the query parameter. There can be several reasons to take this approach and for one of the Salesforce projects here at MobileCaddy this was just what we decided to do.

If you want to take this approach then your SCSS should be modified to look like this;

Due to us using bower to bring in Ionic as part of our development setup we also decided that instead of manually having to modify the SCSS (which wasn’t stored in out git repositories) we created a simple Grunt task to do this for us. Then we added this Grunt task into our standard workflow which meant our developers didn’t need to think about this at all. Our Grunt task looks something like this;

Using either of these approaches should help you to get offline ionicons working and hopefully assist you with creating more robust mobile applications.