1

I've got a service worker that I am using for push notifications in chrome and firefox, how can I exclude the service worker from running on Safari since it's causing exceptions. I've tried a couple of different ways of detecting if the user agent is safari, but it was always detecting as false even though it is Safari.

The last detection method I've used is as following:

// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';

// Safari 3.0+ "[object HTMLElementConstructor]" 
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { 
return p.toString() === "[object SafariRemoteNotification]"; })
(!window['safari'] || (typeof safari !== 'undefined' && 
safari.pushNotification));

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;

// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;

// Chrome 1+
var isChrome = !!window.chrome && !!window.chrome.webstore;

// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;

var ua = navigator.userAgent.toLowerCase();
var safariWindows = ua.indexOf("safari/") !== -1 && ua.indexOf("windows") 
!== -1 && ua.indexOf("chrom") === -1;


if (!isSafari && !safariWindows) {
 //run service worker
}
threesixnine
  • 1,733
  • 7
  • 30
  • 56
  • 1
    Use feature detection, not browser detection: `if ('serviceWorker' in navigator) { ... }` (from [Modernizr](https://github.com/Modernizr/Modernizr/blob/master/feature-detects/serviceworker.js)) – Andreas Aug 01 '17 at 14:49
  • I think he's saying service workers are enabled, but he has some weird exception in his app code while the worker is being ran on safari...? Right @NaughtNinja ? – Sean Newell Aug 01 '17 at 14:53
  • 1
    @SeanNewell Service Workers are not supported at all (yet) by Safari: http://caniuse.com/#feat=serviceworkers – Andreas Aug 01 '17 at 14:58
  • @Andreas I am aware that service workers are not supported at all by safari and this is why it's really odd that still passes through if ('serviceWorker' in navigator) check? – threesixnine Aug 01 '17 at 14:59
  • My B - I thought they were. Since they aren't suported and the feature detection check passes, I'd consider that a bug in Safari. I'd report it! – Sean Newell Aug 01 '17 at 15:30
  • @Andreas you could post an answer using feature detection, and then maybe try to use service workers and catch the exception if it actually isn't supported. I think a `try{ } catch() { }` is what would be needed if safari is being cray cray. – Sean Newell Aug 01 '17 at 15:36
  • They did a partial roll out of workers for a nightly version. May be that is the OP's version? – Schrodinger's cat Aug 02 '17 at 08:42
  • 1
    ServiceWorker is coming, baby!! – Spock Aug 28 '17 at 14:01

0 Answers0