Reminder - You don't need AppCache for PhoneGap/Cordova

This post is more than 2 years old.

I've seen this come up a few times recently, and it was mentioned in a presentation I attended yesterday on PhoneGap, but just as a reminder, stop worrying about AppCache and PhoneGap/Cordova. It may not be entirely clear to people new to hybrid mobile development, but your application is on the device itself. AppCache makes no sense in this regard. It is a bit like having an HTML file on your desktop. Whether you are online or not does not matter!

However...

You obviously do need to care about if the user is offline or not. First - if you are using a CDN for jQuery or some other library - stop. Just download the bits locally to your application and point to them there.

Secondly, if you are using any remote APIs, then certainly you need to check the device's online/offline status. Make use of the Network Information plugin (*) and spend time to ensure your application responds correctly to different network conditions. I covered this way back in 2013 (Building "Robust" PhoneGap Applications) and I discuss it in my Cordova book.

  • As just a quick aside, I know some folks have had reliability issues with the Network Information plugin. I've seen some people actually write a simple XHR utility to hit a known URL and check the result. I've not had this issue myself, but I wanted to bring it up as just a warning that the plugin may not be perfect. Even if a user is reported as being online, I would hope you build your API calls with error handling. Heck, the API provider may be down themselves.
Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Dawson Loudon posted on 2/27/2015 at 3:04 PM

PREACH! I see far too many people out there asking about loading an external site and then using appcache because they 1) don't understand the concept described above or 2) think that they should be able to update their `app` without using the proper channels for an update.

Comment 2 (In reply to #1) by Raymond Camden posted on 2/27/2015 at 3:27 PM

To be fair - I can see some need for updating parts of the app via network stuff. Things like core data for example. But not functionality. That's my opinion fwiw. :)

Comment 3 by Sahkan posted on 4/21/2015 at 1:24 PM

What if i want to use an app i already have on desktop and get updates using the appcache ( use phonegap to port it to IPAD ), do i need to write an other code to do that separately for phonegap ?

Comment 4 (In reply to #3) by Raymond Camden posted on 4/24/2015 at 8:52 PM

If you want a web site, then use a web site. DOn't just 'wrap' it in PhoneGap. While you *can*, it almost never makes sense to do.

Comment 5 by Newbie Mobile Dev posted on 6/11/2015 at 12:04 PM

I have a similar question. I'm building a phonegap app using phonegap build, and I tried Googling, but can't find an example of how to update a mobile app, or any explanation of the concept of sending updates to mobile apps. I need help with this, please!!!

Comment 6 (In reply to #5) by Newbie Mobile Dev posted on 6/11/2015 at 1:07 PM

Nevermind, I just read your blog on ContentSync for Phonegap, but I might have a question about the HEAD method in your Javascript example. I'll do some Googling first, before I ask the question on your ContentSync blog. Thanks!

Comment 7 by Jesse Monroy posted on 1/5/2016 at 6:00 AM

AWESOME I added it to my FAQ!! See the end of the first paragraph!
https://github.com/jessemon...

Comment 8 by chadananda posted on 2/27/2016 at 10:08 PM

Unless you want your app to be able to update itself. Seems like that would be a pretty killer combination. You package up an initial state but let it update using something like appcache nanny. Anyone doing this?

Comment 9 (In reply to #8) by Raymond Camden posted on 2/27/2016 at 11:09 PM

Eh? AppCache doesn't have anything at all to do with that. AppCache lets a browser cache items locally when loading requests from a server. It also lets you provide a fallback if there isn't a network.

That is 100% not the case for a hybrid mobile application. You aren't using a server. You are loading your HTML/etc from the local file system in the app itself.

Comment 10 (In reply to #9) by chadananda posted on 2/28/2016 at 2:59 AM

But you don't have to. Why not load the files from a remote server and use AppCache. Then check for updates once a day (instead of every 30 seconds). That's a pretty neat way of pushing app updates. Otherwise, re-deploying to app stores is a major pain in the keister. Am I missing something?

Comment 11 (In reply to #10) by Raymond Camden posted on 2/28/2016 at 4:07 AM

What happens when your app is offline and you haven't been able to cache anything? Or what happens when network conditions are flakey? While you *can* do what you suggest, I strongly recommend against it. App store updates aren't necessarily a pain - every day people do them. Also, you can push some updates via services like Ionic Deploy.

Comment 12 (In reply to #11) by chadananda posted on 2/28/2016 at 4:47 AM

The user is typically online after having just downloaded the app so the initial app cache should be fine. Subsequent app updates would come whenever the app is able to fetch them. The cache is permanent so it doesn't matter if the user is offline for long periods or not.
And deployment is a major pain. That's why tools like Phonegap Build Hydration exist. Using the Appcache seems like it could be another way to accomplish the same thing.

Comment 13 (In reply to #12) by Raymond Camden posted on 2/28/2016 at 3:46 PM

Hydration wasn't built for this. It was built for the development/testing phase of your application, not for post-release updates.

Listen - you are correct in that you *can* do what you are suggesting. I absolutely do not recommend it and strongly urge you against doing it.

Comment 14 (In reply to #13) by chadananda posted on 2/29/2016 at 2:12 AM

I don't want to be disrespectful of your advice, but it would help me if you would give some of the reasons why you don't think cache-based application updating is a good idea. Making apps as easy to update as a web site still sounds pretty compelling to me.

Comment 15 (In reply to #14) by Raymond Camden posted on 2/29/2016 at 11:27 AM

It isn't "cache-based apps", but an app that is still on a web server that bothers me. You're not building a mobile app really but just a shell for your web server. Even with the cache in place, you'll still be doing a network request for things that should served from the device itself. And as I said, if you do not want to do updates via the app store, you have other alternatives like Ionic Deploy.

Comment 16 (In reply to #4) by Jude Naveen Raj posted on 5/22/2016 at 9:39 PM

That depends. There will always be reasons to use phonegap, for example, to obtain unlimited app storage through the use of cordova-sqlite-plugin, or maybe you need to utilize wkwebviews instead of uiwebviews (which cordova again helps with)... and a lot more reason probably.

And if you ever are involved in creating a project that demands the complexity, using appcache to auto-update your app seems like a very wise thing to do. However im expecting people to soon move to things like service worker, etc.

Comment 17 (In reply to #16) by Raymond Camden posted on 5/23/2016 at 12:01 AM

To your first paragraph, right away you're going beyond just 'wrapping', which means you aren't disagreeing with my comment at all. ;)

As for your second para, appcache isn't used to auto-update. It's used to help serve assets when your offline.

Comment 18 (In reply to #15) by Jam posted on 8/4/2016 at 6:49 PM

I think if you are using a javascript framework you could easily bundle your whole application into one .js file that includes the app shell and any pages needed. If there is a need for data storage as well one could use pouchDB to store data on the device for offline usage and sync that with a CouchDB database on the remote server.

Meteor does something similar to this when building its mobile applications with mongo, minimongo and a package called GroundDB. It also uses Appcaching and uses the cached files by default.

Comment 19 (In reply to #18) by Raymond Camden posted on 8/4/2016 at 7:14 PM

So everything you say here is true, but I don't see how it applies to the point of this article - that AppCache isn't needed (and doesn't make sense!) for PhoneGap/Cordova apps.

Comment 20 by Pier posted on 8/21/2016 at 12:11 AM

The problem with the network information plugin is that it doesn't tell you whether the device is online, it tells you if it's connected to a network. You could be connected to a Wifi network and be offline for example.

To really know if you are online the best way is to simply XHR or fetch() some small JSON on your server.

Comment 21 (In reply to #19) by Dave B posted on 4/12/2018 at 6:14 PM

Raymond, thank you for taking the time to share.

I also like the concept of simply posting a new application to the server and having all mobile applications automatically update to it. That being said, the real driving factor for us considering this pattern is for security reasons.

We have observed that a Cordova application that redirects to pages served from a server allows using httponly cookies even with a Cordova application. All other attempts to use cookies with Cordova have failed miserably. This is probably because the origin sent to the server is "file://", which I suspect the standards would never allow supporting cookies because of how insecure it would be. This leaves the only way to cache a security token is to either use local storage OR a Cordova extension, which has the exact same security limitation as local storage. That being your security token is exposed to XSS attacks and the WebView cannot offer a reliable origin to the web server.

Of course our goal is still to have an offline application/UI that presents well to a user if they cannot connect to the server. Having this setup on the first run and an entry/splash screen deployed local to cordova that makes it clear seems reasonable to me.

However, your insistence on this being a very bad idea is troubling, especially with the inability of Cordova to provide a secure origin.

I know this thread is over 2 years old, but I am curious if you still feel the same way and perhaps know of something we are missing. Perhaps our initial tests did not tell the entire story and we are asking for trouble. We do not yet have a full proof of concept working with this yet as we are evaluating all options in order create an application that can be easily updated, secure, and support both Android and IOS devices.

Comment 22 (In reply to #21) by Raymond Camden posted on 4/12/2018 at 7:41 PM

Honestly, I see little use for Cordova anymore with how strong the web is now (see PWA). To be clear, that isn't a slight on Cordova. Since day one it's mission was to help the web come to a point where it wouldn't be needed. I don't think we are 100% of the way there, but we're a heck of a lot closer now in 2018 than we were in 2015.

Comment 23 (In reply to #22) by Dave B posted on 4/12/2018 at 10:35 PM

That is very helpful. Thank you.