ilan-schemoul / meteor-service-worker

An universal service worker for meteor apps
MIT License
137 stars 26 forks source link

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

Closed sferoze closed 1 month ago

sferoze commented 6 years ago

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)); });
}
filipenevola commented 6 years ago

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();
sferoze commented 6 years ago

@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

filipenevola commented 6 years ago

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

sferoze commented 6 years ago

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