FIX: Better detection of Apple browsers (#7961)

FIX: Better detection of Apple browsers (#7961)

Introduces isIpadOS to our capabilities sniffer, which is currently compatible with latest iPad beta OS. Older iPad versions will match our isIOS detection.

Using this new feature, fixes the service worker being blocked in Google Chrome, Firefox and other browsers while running on Mac OS, where they work just fine. This enables Desktop PWA install in Mac OS.

diff --git a/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 b/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6
index b78044e..c0099d7 100644
--- a/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6
+++ b/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6
@@ -9,7 +9,8 @@ export default {
     const isSupported = isSecured && "serviceWorker" in navigator;
 
     if (isSupported) {
-      const isApple = !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i);
+      const caps = Discourse.__container__.lookup("capabilities:main");
+      const isApple = caps.isSafari || caps.isIOS || caps.isIpadOS;
 
       if (Discourse.ServiceWorkerURL && !isApple) {
         navigator.serviceWorker.getRegistrations().then(registrations => {
diff --git a/app/assets/javascripts/discourse/pre-initializers/sniff-capabilities.js.es6 b/app/assets/javascripts/discourse/pre-initializers/sniff-capabilities.js.es6
index a4decf5..0a41fba 100644
--- a/app/assets/javascripts/discourse/pre-initializers/sniff-capabilities.js.es6
+++ b/app/assets/javascripts/discourse/pre-initializers/sniff-capabilities.js.es6
@@ -37,6 +37,8 @@ export default {
 
       caps.isIOS =
         /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
+
+      caps.isIpadOS = ua.indexOf("Mac OS") !== -1 && touch;
     }
 
     // We consider high res a device with 1280 horizontal pixels. High DPI tablets like

GitHub sha: 272b38a5

1 Like