43

I want to use a service worker in my Cordova Android add as a convenient way to sync some images to and from the server. I notice that this question states that this is not possible, since service workers must be loaded via https:, whereas Cordova will load files via file:.

On the other hand, it seems that ionic supports service workers. That implies that they have figured out some way to make this work. However, I am not using ionic. There appears to be a cordova plugin for service workers for iOS, but as I said I am on Android.

What is the current best practices for using service workers in Cordova Android apps, if that's possible at all?

  • let me know if this link helps - http://forum.quasar-framework.org/topic/259/service-worker-on-cordova-app/2 – Gandhi Jul 29 '17 at 08:59
  • Hi, did that link gave any clue? – Gandhi Jul 31 '17 at 11:40
  • I think Ionic supports service-workers only if you publish your app as a PWA (Progressive Web App), when building for a native platform service-worker registration will fail because (as you already mentioned) the files are served locally via file://. – David Jul 31 '17 at 19:37
  • @torazaburo any update on this? – Gandhi Aug 02 '17 at 14:17
  • @Gandhi I tend to think your answer is correct. –  Aug 02 '17 at 14:27
  • @torazaburo Thanks for the response. If the answer was helpful, please accept the same before the bounty expires :) Cheers – Gandhi Aug 03 '17 at 08:33
  • @torazaburo HI Any update on this as time is running out? – Gandhi Aug 04 '17 at 11:57

2 Answers2

57

After digging deep into this issue, I conclude that there is no support for Service workers in Android as it blocks service workers from HTTP or file protocols.

Also the support of Service worker in Ionic framework do not clearly state that it is not supported in hybrid mobile apps. It's kind of misleading too as in this case. Ionic's Service Worker support comes into picture only in case of Progressive Web App and not in hybrid mobile app as mentioned in their official blog

Adding to the above info, most of the functionality that can be achieved by using Service Workers are already available as part of plugins like push notification plugin which should be suffice in most cases.

The bottom line is that the Service Workers are not supported in Cordova Android as well as in Ionic framework. For more info, check out the following link

Gandhi
  • 11,875
  • 4
  • 39
  • 63
  • 1
    just to be clear: Service Workers are not available for webview in android apps, so not avaible for android hybrid apps, but they are avaible in the Chrome and Firefox web browsers for android, for pwa (browser platform build in ionic). – felipeaf Nov 23 '17 at 14:42
  • @Gandhi Thanks for this information. Now that a year has passed, is there any update? What about running a little tiny HTTP server as part of my app? –  Jun 10 '18 at 06:40
  • 1
    @torazaburo I guess service workers are still not supported. Running http server is different from service workers. But if you just wanna run a tiny http server, then you gotta check out - https://github.com/floatinghotpot/cordova-httpd – Gandhi Jun 12 '18 at 05:22
  • 1
    As of cordova-android version 10 cordova apps on android are served from https://localhost (https://cordova.apache.org/announcements/2021/07/20/cordova-android-10.0.0.html) so it seems that servers may be an option now! – SuprMan Jul 27 '23 at 01:50
3

I couldn’t take “no” for an answer so went on a hunt to see if I could code my way out of this.

Note, I'm not sure I was having the same issue or even had the same req as the OP (apologies if not, but hopefully it was)... my scenario was this, for clarity I had initial luck with manually fetching and adding items (https://localhost/cordova.js and the plugin files) to the cache the Service Worker uses. I did this on first run up and the 2nd run up (even if offline) worked… so some good progress. Weirdly on subsequent run ups some (most) of the manually cached items were removed and so startups did not complete successfully.

I then looked into if I could bypass the SW completely for calls to localhost, and what do you know, it seems to work. Essentially in the SW’s fetch handler I have a conditional clause to do nothing if it spots localhost in the url. This has the affect that SW can’t find a fetch handler that takes care of the request and then the browser makes the call to fetch the asset just as if there were no SW at all.

I have tested this for both online/offline starts, and made sure I can access cordova and some of the plugins, and all seems to run just fine. Note, all testing so far is on Android.

An example of the code I used is as followa (left an explicit “do nothing” comment in for clarity);

 self.addEventListener('fetch', event => {
        console.log('Fetch event for ', event.request.url);
        if (event.request.url.includes("localhost")) {
            // Do Nothing
        } else {
        event.respondWith(
          caches.match(event.request, {
           // ... do the rest of the SW handling

I’d be interested to hear if other folk had tried this and then had any issues with it.