Loading Assets
One of the beauties of PhoneGap compared to native is its ability to
load and run remote code. As I mentioned above, for development this
is a complete time saver. But it also has the added benefit of being
able to update your app at any point you want without having to go
through App Store approval. Remote loading is certainly great, but
mobile apps are also expected to work when offline. To get the best of
both worlds, we decided to bundle our JS and CSS with the native
build, but before loading them check online to see if we have newer
versions available. To accomplish this, when our app first starts, it
remotely loads the manifest JSON file that Mott created to determine
the version numbers of our latest JS and CSS. If the remote versions
are different, it loads them and then saves them locally using
PhoneGap’s FileSystem API. If the remote versions are the same, it
loads them directly from the FileSystem thus saving time. If we are
offline, it goes directly to FileSystem. And if for whatever reason
FileSystem fails, it loads the JS and CSS versions that we bundled
inside the app when we uploaded to the store. This system has allowed
us to update our app often while also keeping load times down and
working offline.