Over a million developers have joined DZone.

Building a Hybrid Mobile app? Avoid Using CDNs for Your Libraries

In a world where functioning offline is critical for most apps, you should question your use of CDNs.

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

I’m reading an (otherwise fascinating) article now on Ionic and came across this snippet of code:

<script type="text/javascript" src="//code.angularjs.org/1.4.3/angular.js"></script>
<script type="text/javascript" src="//code.angularjs.org/1.4.3/angular-resource.js"></script>
<script type="text/javascript" src="js/services.js"></script>
<script type="text/javascript" src="js/index.js"></script>

What’s wrong with the above code? (And yes, the title mostly gives it away.)

By using a CDN for libraries in your hybrid mobile app, you’ve essentially ensured the application will be completely broken when offline. This is obvious, but like most of us doing client-side work, we’ve become accustomed to using CDNs for libraries.

Now–you may ask–what if the core functionality of the application requires you to be online? In that scenario, you still want to ensure you gracefully handle the user being offline. If your CDN libraries fail to load, most likely your application is going to crap the bed. If those libraries were stored locally within the application, you can at least still load up, detect the offline state, and then tell the user they can’t do anything while offline.

Even better, you can add a simple event listener for when they get back online and then start the application up again. (And of course, you’ll have an event listener for when they go back offline. Because that happens. A lot.)

For an example of this in action, see my earlier blog posts: PhoneGap Online/Offline Tip and PhoneGap Online/Offline Tip (2).

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}