meteor-service-worker icon indicating copy to clipboard operation
meteor-service-worker copied to clipboard

Good idea to register sw.js under !Meteor.isCordova

Open sferoze opened this issue 6 years ago • 4 comments

If your meteor app is compiled to all devices using Cordova, would it be a good idea to nest the call to register sw.js inside a !Meteor.isCordova so it does NOT run on Cordova?

if (!Meteor.isCordova) {
    Meteor.startup(() => { navigator.serviceWorker.register('/sw.js') .then() .catch(error => console.log('ServiceWorker registration failed: ', err)); });
}

sferoze avatar Jul 12 '17 18:07 sferoze

Hello @sferoze,

I register with my code below in one of my apps that is a PWA, but if in the future I'll start using Cordova to this app to publish in stores it will depend, because if I want the offline part of my app working inside Cordova I still need to have my service worker registered:

const iOS = () => {
  const iDevices = [
    'iPad Simulator',
    'iPhone Simulator',
    'iPod Simulator',
    'iPad',
    'iPhone',
    'iPod',
  ];

  return !!navigator.platform && iDevices.indexOf(navigator.platform) !== -1;
};
const register = () => {
  if (!('serviceWorker' in navigator)) {
    // eslint-disable-next-line no-console
    console.log('serviceWorker is not in navigator!');
    return;
  }
  if (iOS()) {
    // eslint-disable-next-line no-console
    console.log('iOS device then not register sw (was with error)!');
    return;
  }
  navigator.serviceWorker
    .register('/sw.js')
    // eslint-disable-next-line no-console
    .then(() => console.log('serviceWorker registered with success!'))
    .catch(error => console.error('Error registering serviceWorker!', error));
};

register();

filipenevola avatar Oct 11 '17 19:10 filipenevola

@filipenevola my app works offline in cordova and I do not register service worker.

Cordova apps, all the app code is already local to the device... works offline by default

sferoze avatar Oct 11 '17 20:10 sferoze

Yeah yeah, I know, I'm talking about something offline provided by the service worker.

filipenevola avatar Oct 11 '17 20:10 filipenevola

@filipenevola ah i see, thank you for sharing your code, it is very likely I will find it useful in the future

sferoze avatar Oct 11 '17 20:10 sferoze